-
Notifications
You must be signed in to change notification settings - Fork 4
Add OpenTelemetry integration for tracing and metrics #16
Description
Summary
Add OpenTelemetry (OTel) integration for distributed tracing and metrics collection.
Current State
- Basic metrics endpoint exists at `/metrics`
- No distributed tracing
- No standardized observability
Expected Outcome
Tracing
Add trace spans for:
- HTTP requests (middleware)
- Tool invocations
- Resource reads
- Prompt generation
```go
import "go.opentelemetry.io/otel"
func TracingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer("gomcp").Start(r.Context(), r.URL.Path)
defer span.End()
span.SetAttributes(
attribute.String("http.method", r.Method),
attribute.String("http.url", r.URL.String()),
)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
```
Metrics
Export metrics for:
- Request count by endpoint
- Request latency histogram
- Tool invocation count
- Error count by type
Configuration
```go
type Config struct {
// ... existing
OTelEnabled bool `mapstructure:"OTEL_ENABLED"`
OTelEndpoint string `mapstructure:"OTEL_EXPORTER_OTLP_ENDPOINT"`
OTelServiceName string `mapstructure:"OTEL_SERVICE_NAME"`
}
```
Dependencies
```
go.opentelemetry.io/otel
go.opentelemetry.io/otel/exporters/otlp/otlptrace
go.opentelemetry.io/otel/sdk/trace
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
```
Acceptance Criteria
- OTel SDK integrated
- Tracing middleware added
- Tool invocations traced
- Metrics exported in Prometheus format
- Configurable via environment variables
- Documentation for connecting to Jaeger/Grafana