Release v1.0.0: HTTP instrumentation, slog correlation, TLS by default
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>
This commit is contained in:
1 parent
362267470e
commit
2c83b28342
15 files changed
+695
-119
No files matched your search
@@ -4,7 +4,9 @@ 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"
|
||||
@@ -15,8 +17,18 @@ import (
|
||||
type TracerConfig struct {
|
||||
ServiceName string
|
||||
ServiceVersion string
|
||||
Endpoint string // OTLP gRPC endpoint, e.g. "localhost:4317"
|
||||
Sampler float64 // sampling ratio, default: 1.0 (sample everything)
|
||||
// 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() {
|
||||
@@ -25,43 +37,60 @@ func (c *TracerConfig) defaults() {
|
||||
}
|
||||
}
|
||||
|
||||
// SetupTracer initializes an OpenTelemetry tracer provider with OTLP gRPC exporter.
|
||||
// Returns a shutdown function that must be called on application exit.
|
||||
// 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()
|
||||
|
||||
exporter, err := otlptracegrpc.New(ctx,
|
||||
otlptracegrpc.WithEndpoint(cfg.Endpoint),
|
||||
otlptracegrpc.WithInsecure(),
|
||||
)
|
||||
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 := []resource.Option{
|
||||
resource.WithAttributes(semconv.ServiceName(cfg.ServiceName)),
|
||||
}
|
||||
attrs := append([]attribute.KeyValue{semconv.ServiceName(cfg.ServiceName)}, cfg.Attributes...)
|
||||
if cfg.ServiceVersion != "" {
|
||||
attrs = append(attrs, resource.WithAttributes(semconv.ServiceVersion(cfg.ServiceVersion)))
|
||||
attrs = append(attrs, semconv.ServiceVersion(cfg.ServiceVersion))
|
||||
}
|
||||
|
||||
res, err := resource.New(ctx, attrs...)
|
||||
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.TraceIDRatioBased(cfg.Sampler)),
|
||||
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("obsx").Start(ctx, name, opts...)
|
||||
return otel.Tracer(tracerName).Start(ctx, name, opts...)
|
||||
}
|
||||
Reference in new issue
Block a user