Add HTTPMiddleware, HTTPTransport and LogHandler; extend TracerConfig with Insecure, Headers and Attributes; install W3C propagators; parent-based sampling; end-to-end OTLP export tests; update dependencies (Go 1.26). Breaking since v0.2.0: the OTLP connection uses TLS unless Insecure is set. Co-Authored-By: Claude <noreply@anthropic.com>
97 lines
3.0 KiB
Go
97 lines
3.0 KiB
Go
package obsx
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
|
"go.opentelemetry.io/otel/propagation"
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// TracerConfig configures the OpenTelemetry tracer.
|
|
type TracerConfig struct {
|
|
ServiceName string
|
|
ServiceVersion string
|
|
// Endpoint is the OTLP gRPC endpoint, e.g. "localhost:4317". When empty,
|
|
// the standard OTEL_EXPORTER_OTLP_* environment variables apply.
|
|
Endpoint string
|
|
// Insecure disables TLS for the OTLP connection (local collectors, sidecars).
|
|
Insecure bool
|
|
// Headers are sent with every export request, e.g. for authentication.
|
|
Headers map[string]string
|
|
// Sampler is the ratio of root traces to sample, default: 1.0 (everything).
|
|
// Child spans always follow the decision of their parent.
|
|
Sampler float64
|
|
// Attributes are extra resource attributes, e.g. deployment environment.
|
|
Attributes []attribute.KeyValue
|
|
}
|
|
|
|
func (c *TracerConfig) defaults() {
|
|
if c.Sampler <= 0 {
|
|
c.Sampler = 1.0
|
|
}
|
|
}
|
|
|
|
// SetupTracer initializes an OpenTelemetry tracer provider with an OTLP gRPC
|
|
// exporter and installs it, together with W3C TraceContext and Baggage
|
|
// propagators, as the OpenTelemetry globals.
|
|
// Returns a shutdown function that must be called on application exit to
|
|
// flush pending spans.
|
|
func SetupTracer(ctx context.Context, cfg TracerConfig) (shutdown func(context.Context) error, err error) {
|
|
cfg.defaults()
|
|
|
|
var opts []otlptracegrpc.Option
|
|
if cfg.Endpoint != "" {
|
|
opts = append(opts, otlptracegrpc.WithEndpoint(cfg.Endpoint))
|
|
}
|
|
if cfg.Insecure {
|
|
opts = append(opts, otlptracegrpc.WithInsecure())
|
|
}
|
|
if len(cfg.Headers) > 0 {
|
|
opts = append(opts, otlptracegrpc.WithHeaders(cfg.Headers))
|
|
}
|
|
exporter, err := otlptracegrpc.New(ctx, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
attrs := append([]attribute.KeyValue{semconv.ServiceName(cfg.ServiceName)}, cfg.Attributes...)
|
|
if cfg.ServiceVersion != "" {
|
|
attrs = append(attrs, semconv.ServiceVersion(cfg.ServiceVersion))
|
|
}
|
|
res, err := resource.New(ctx,
|
|
resource.WithFromEnv(), // OTEL_RESOURCE_ATTRIBUTES; config below wins
|
|
resource.WithTelemetrySDK(),
|
|
resource.WithAttributes(attrs...),
|
|
)
|
|
if err != nil {
|
|
_ = exporter.Shutdown(ctx)
|
|
return nil, err
|
|
}
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
sdktrace.WithBatcher(exporter),
|
|
sdktrace.WithResource(res),
|
|
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(cfg.Sampler))),
|
|
)
|
|
|
|
otel.SetTracerProvider(tp)
|
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
|
propagation.TraceContext{}, propagation.Baggage{},
|
|
))
|
|
|
|
return tp.Shutdown, nil
|
|
}
|
|
|
|
const tracerName = "git.codelab.vc/pkg/obsx"
|
|
|
|
// StartSpan starts a new span using the global tracer provider.
|
|
func StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
|
|
return otel.Tracer(tracerName).Start(ctx, name, opts...)
|
|
}
|