Skip to content

Commit a82c5e2

Browse files
etiennepclaude
andcommitted
Add OpenTelemetry OTLP exporter with full SDK
Implement production-ready OpenTelemetry Protocol (OTLP) exporter using the official OpenTelemetry SDK with support for both gRPC and HTTP transports. Features: - gRPC and HTTP/Protobuf protocol support - Full OTEL_* environment variable integration - Automatic resource detection (AWS, GCP, Azure, K8s) - Counter, Gauge, and Histogram metric types - Tag to attribute conversion - Thread-safe instrument caching - Proper gauge semantics via delta calculation Implementation: - Uses UpDownCounter for gauges with delta tracking to maintain absolute value semantics (workaround until stable SDK adds Gauge) - Background context for recording to avoid cancellation issues - Lock-free reads for instrument lookup in hot path - Comprehensive tests and benchmarks Documentation: - Complete README with configuration examples - Cloud resource detector usage guides - Implementation notes explaining design decisions - Example code for common use cases Bumps version to 5.9.0 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 63dda99 commit a82c5e2

11 files changed

Lines changed: 2327 additions & 33 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ _testmain.go
3131
/dogstatsd
3232

3333
/datadog/testdata/fuzz
34+
.claude/settings.local.json

HISTORY.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
# History
22

3+
### v5.9.0 (February 6, 2026)
4+
5+
Add full OpenTelemetry OTLP exporter support with official SDK integration.
6+
7+
**New Feature: OpenTelemetry OTLP Exporter**
8+
9+
The `otlp` package now includes a production-ready `SDKHandler` that uses the
10+
official OpenTelemetry SDK with comprehensive support for modern observability
11+
requirements:
12+
13+
- **Dual Transport Support**: Both gRPC and HTTP/Protobuf protocols
14+
- **Environment Variables**: Full support for all standard `OTEL_*` environment
15+
variables including `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_PROTOCOL`,
16+
`OTEL_RESOURCE_ATTRIBUTES`, etc.
17+
- **Automatic Resource Detection**: Built-in support for AWS (EC2, ECS, EKS, Lambda),
18+
GCP (Compute Engine), Azure (VM), Kubernetes, host, and process metadata
19+
- **All Metric Types**: Counter, Gauge, and Histogram with proper semantics
20+
- **Tag Preservation**: Automatic conversion of stats tags to OpenTelemetry attributes
21+
- **Production Ready**: Thread-safe instrument caching, proper context handling,
22+
and comprehensive error handling
23+
24+
**Usage Example:**
25+
26+
```go
27+
import (
28+
"context"
29+
"github.com/segmentio/stats/v5"
30+
"github.com/segmentio/stats/v5/otlp"
31+
)
32+
33+
// Simple usage with environment variables
34+
handler, err := otlp.NewSDKHandlerFromEnv(ctx)
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
defer handler.Shutdown(ctx)
39+
stats.Register(handler)
40+
41+
// Or with explicit configuration
42+
handler, err := otlp.NewSDKHandler(ctx, otlp.SDKConfig{
43+
Protocol: otlp.ProtocolGRPC,
44+
Endpoint: "localhost:4317",
45+
})
46+
```
47+
48+
**Implementation Details:**
49+
50+
- Gauges use `UpDownCounter` with delta calculation to maintain absolute value
51+
semantics (workaround until stable OTel SDK adds Gauge instrument)
52+
- Background context for metric recording to prevent context cancellation issues
53+
- Lock-free reads for instrument lookup in the hot path
54+
- Comprehensive documentation including cloud resource detector examples
55+
56+
See the [otlp package documentation](./otlp/README.md) for complete details and examples.
57+
358
### v5.8.0 (December 15, 2025)
459

560
When reporting go/stats versions, ensure that any user provided tags are

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,88 @@ func main() {
121121
}
122122
```
123123

124+
## Supported Backends
125+
126+
The stats package supports multiple metric backends out of the box:
127+
128+
### OpenTelemetry (OTLP)
129+
130+
The [github.com/segmentio/stats/v5/otlp](https://pkg.go.dev/github.com/segmentio/stats/v5/otlp) package provides full OpenTelemetry Protocol (OTLP) support using the official OpenTelemetry SDK.
131+
132+
**Features:**
133+
134+
- gRPC and HTTP/Protobuf transports
135+
- Full support for OTEL_* environment variables
136+
- Automatic resource detection (cloud, Kubernetes, host, process)
137+
- Production-ready with official OTel SDK exporters
138+
139+
```go
140+
import (
141+
"context"
142+
"github.com/segmentio/stats/v5"
143+
"github.com/segmentio/stats/v5/otlp"
144+
)
145+
146+
func main() {
147+
ctx := context.Background()
148+
149+
// Using gRPC (recommended)
150+
handler, err := otlp.NewSDKHandler(ctx, otlp.SDKConfig{
151+
Protocol: otlp.ProtocolGRPC,
152+
Endpoint: "localhost:4317",
153+
})
154+
if err != nil {
155+
panic(err)
156+
}
157+
defer handler.Shutdown(ctx)
158+
159+
stats.Register(handler)
160+
defer stats.Flush()
161+
162+
// Or use environment variables (simplest)
163+
// export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
164+
// export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
165+
handler, err = otlp.NewSDKHandlerFromEnv(ctx)
166+
}
167+
```
168+
169+
See the [otlp package documentation](./otlp/README.md) for complete details.
170+
171+
### Datadog
172+
173+
The [github.com/segmentio/stats/v5/datadog](https://godoc.org/github.com/segmentio/stats/v5/datadog) package provides support for sending metrics to Datadog via DogStatsD protocol over UDP or Unix Domain Sockets.
174+
175+
```go
176+
import "github.com/segmentio/stats/v5/datadog"
177+
178+
stats.Register(datadog.NewClient("localhost:8125"))
179+
```
180+
181+
### Prometheus
182+
183+
The [github.com/segmentio/stats/v5/prometheus](https://godoc.org/github.com/segmentio/stats/v5/prometheus) package exposes an HTTP handler that serves metrics in Prometheus format.
184+
185+
```go
186+
import (
187+
"net/http"
188+
"github.com/segmentio/stats/v5/prometheus"
189+
)
190+
191+
handler := prometheus.NewHandler()
192+
stats.Register(handler)
193+
http.Handle("/metrics", handler)
194+
```
195+
196+
### InfluxDB
197+
198+
The [github.com/segmentio/stats/v5/influxdb](https://godoc.org/github.com/segmentio/stats/v5/influxdb) package sends metrics to InfluxDB using the line protocol over HTTP.
199+
200+
```go
201+
import "github.com/segmentio/stats/v5/influxdb"
202+
203+
stats.Register(influxdb.NewClient("http://localhost:8086"))
204+
```
205+
124206
### Metrics
125207

126208
- [Gauges](https://godoc.org/github.com/segmentio/stats#Gauge)

0 commit comments

Comments
 (0)