OpenTelemetry Provider Examples
Code Source: https://github.com/gogf/examples/tree/main/observability/trace/provider
Description
This directory contains examples demonstrating different OpenTelemetry trace provider configurations in GoFrame applications. It includes:
-
gRPC Provider (
grpc/
)- Uses gRPC protocol for trace data transmission
- Configures trace provider with gRPC exporter
- Demonstrates resource and sampler configuration
-
HTTP Provider (
http/
)- Uses HTTP protocol for trace data transmission
- Configures trace provider with HTTP exporter
- Shows resource and sampler configuration
-
Internal Components (
internal/
)- Common provider initialization code
- Shared constants and configurations
- Utility functions for tracing setup
Directory Structure
.
├── grpc/ # gRPC provider example
│ └── main.go # gRPC provider implementation
├── http/ # HTTP provider example
│ └── main.go # HTTP provider implementation
├── internal/ # Shared components
│ ├── consts.go # Constants definition
│ ├── provider.go # Provider initialization
│ └── request.go # Request utilities
├── go.mod # Go module file
└── go.sum # Go module checksums
Requirements
- Go 1.22 or higher
- Git
- GoFrame
- OpenTelemetry
Features
The examples showcase the following features:
-
Provider Configuration
- Resource configuration
- Sampler configuration
- Exporter setup
- Context propagation
-
Resource Attributes
- Service name
- Host information
- Process details
- Custom attributes
-
Sampling Strategies
- Always sample
- Never sample
- Parent-based sampling
- Trace ID ratio based sampling
-
Span Processing
- Simple span processor
- Batch span processor
- Span limits configuration
Comparison of Providers
gRPC Provider (grpc/)
-
Features:
- High-performance streaming
- Bidirectional communication
- Connection multiplexing
- Compression support
-
Configuration:
- gRPC endpoint
- Authentication headers
- Compression options
- Connection security
-
Use Cases:
- High-volume tracing
- Performance-critical systems
- Streaming trace data
HTTP Provider (http/)
-
Features:
- Standard HTTP protocol
- Simple configuration
- Firewall friendly
- Compression support
-
Configuration:
- HTTP endpoint
- URL path
- Headers configuration
- Compression level
-
Use Cases:
- Basic tracing needs
- HTTP proxy environments
- Simple deployment
Usage
gRPC Provider Example
-
Navigate to gRPC example:
cd grpc
-
Run the example:
go run main.go
HTTP Provider Example
-
Navigate to HTTP example:
cd http
-
Run the example:
go run main.go
Implementation Details
Both examples demonstrate:
-
Provider Setup
- Resource configuration
- Sampler configuration
- Exporter initialization
- Context management
-
Resource Configuration
- Service identification
- Host information
- Process attributes
- Custom tags
-
Sampling Configuration
- Sampling strategy selection
- Sampling rate configuration
- Parent context handling
-
Error Handling
- Connection errors
- Export errors
- Shutdown handling
Troubleshooting
-
Provider Issues:
- Check endpoint configuration
- Verify authentication settings
- Review connection errors
- Check compression settings
-
Resource Issues:
- Verify service name
- Check host information
- Review attribute values
-
Sampling Issues:
- Check sampling strategy
- Verify sampling rate
- Review parent context
-
General Issues:
- Check network connectivity
- Verify dependencies
- Review error logs