telemetry package provides OpenTelemetry integration for collecting metrics and distributed traces from Go applications.
Features
- OpenTelemetry SDK: Full integration with OpenTelemetry standards
- Metrics Collection: Host metrics, runtime metrics, and custom metrics
- Distributed Tracing: Trace sampling and export via OTLP
- OTLP Export: gRPC-based export to OpenTelemetry collectors
- Resource Attributes: Configurable service identification
- Automatic Instrumentation: Host and runtime metrics out of the box
Installation
Quick Start
Configuration
Config
OpenTelemetryConfig
Initialization
Init
Metrics
The package automatically collects:- Host Metrics: CPU, memory, disk, network usage
- Runtime Metrics: Go runtime statistics (goroutines, GC, memory)
Custom Metrics
You can add custom metrics using the OpenTelemetry SDK:Histogram Example
Distributed Tracing
Creating Spans
HTTP Instrumentation
The telemetry package includes gRPC and HTTP client instrumentation in theotelgrpc and otelhhtpclient subpackages:
Trace Sampling
Control which traces are collected using theTraceSampleProbability setting:
1.0= 100% sampling (collect all traces) - good for development0.1= 10% sampling - good for production with high traffic0.01= 1% sampling - good for very high traffic services
Resource Attributes
By default, minimal resource attributes are included:service.nameservice.version
- Telemetry SDK version
- OS information
- Host information
- Process information
- Runtime name and version
Complete Example: Web Service with Telemetry
OpenTelemetry Collector Setup
To receive telemetry data, you need an OpenTelemetry Collector: docker-compose.yml:Best Practices
Use Appropriate Sampling Rates
Use Appropriate Sampling Rates
Adjust sampling based on traffic:
Add Meaningful Attributes
Add Meaningful Attributes
Include context-specific attributes:
Always Cleanup
Always Cleanup
Use defer to ensure proper shutdown:
Instrument Critical Paths
Instrument Critical Paths
Focus on business-critical operations: