otel

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package otel provides an OpenTelemetry Tracer adapter for Modulex.

It implements the modulex.Tracer interface using an OpenTelemetry TracerProvider, preserving span ancestry and recording errors with the appropriate span status.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPMiddleware added in v0.2.0

func HTTPMiddleware(tp oteltrace.TracerProvider, opts ...HTTPOption) func(http.Handler) http.Handler

HTTPMiddleware returns an HTTP middleware that creates a span for each request. It extracts W3C trace context from incoming headers, records HTTP attributes (method, path, status code), and marks 5xx responses as errors.

If tp is nil, the global OpenTelemetry TracerProvider is used.

func NewProviderFromEnv added in v0.5.1

func NewProviderFromEnv(serviceName string, opts ...ProviderOption) (*sdktrace.TracerProvider, func(context.Context) error, error)

NewProviderFromEnv builds an OTLP-exporting *sdktrace.TracerProvider driven by standard OTEL_EXPORTER_OTLP_* environment variables, plus serviceName as the service.name resource attribute.

This factors out the generic OTLP-provider-construction boilerplate (exporter protocol/endpoint resolution, resource attributes, sampling) that's otherwise hand-rolled per service. It deliberately does not cover app-specific concerns like PII redaction; wrap the returned provider's exporter/processor chain (via WithSpanProcessor) for that.

The returned shutdown func flushes and closes the exporter; callers should defer it. Pass the returned provider to NewTracer to adapt it to modulex.Tracer:

tp, shutdown, err := otel.NewProviderFromEnv("my-service")
if err != nil {
    return err
}
defer shutdown(context.Background())
tracer := otel.NewTracer(tp)

func NewTracer

func NewTracer(provider oteltrace.TracerProvider) modulex.Tracer

NewTracer creates a Modulex Tracer backed by OpenTelemetry.

If provider is nil, the global OpenTelemetry TracerProvider is used. The returned tracer is safe for concurrent use.

func SubscriberMiddleware added in v0.2.0

func SubscriberMiddleware(tp oteltrace.TracerProvider, opts ...SubscriberOption) func(topic string, handler modulex.EventHandler) modulex.EventHandler

SubscriberMiddleware wraps an EventHandler with automatic span creation. Each handler invocation gets a child span named after the topic, with the topic recorded as an attribute. Handler errors are recorded on the span.

If tp is nil, the global OpenTelemetry TracerProvider is used.

Types

type HTTPOption added in v0.2.0

type HTTPOption func(*httpConfig)

HTTPOption configures the HTTP middleware.

func WithHTTPSpanName added in v0.2.0

func WithHTTPSpanName(fn func(r *http.Request) string) HTTPOption

WithHTTPSpanName sets a custom span naming function. The default uses the HTTP method as the span name.

type ProviderOption added in v0.5.1

type ProviderOption func(*providerConfig)

ProviderOption configures NewProviderFromEnv.

func WithExporterEndpoint added in v0.5.1

func WithExporterEndpoint(endpoint string) ProviderOption

WithExporterEndpoint overrides the OTLP collector endpoint. Overrides OTEL_EXPORTER_OTLP_ENDPOINT.

func WithExporterProtocol added in v0.5.1

func WithExporterProtocol(protocol string) ProviderOption

WithExporterProtocol overrides the OTLP exporter protocol. Recognized values are "grpc" (the default), "http" (an alias for "http/protobuf"), and "none", which disables span export entirely (useful for local development). Overrides OTEL_EXPORTER_OTLP_PROTOCOL.

func WithResourceAttributes added in v0.5.1

func WithResourceAttributes(attrs ...attribute.KeyValue) ProviderOption

WithResourceAttributes adds extra resource attributes alongside the service.name attribute derived from NewProviderFromEnv's serviceName argument.

func WithSampleRatio added in v0.5.1

func WithSampleRatio(ratio float64) ProviderOption

WithSampleRatio overrides the trace sampling ratio, used as the ratio for a ParentBased(TraceIDRatioBased) sampler. Overrides OTEL_TRACES_SAMPLER_ARG.

func WithSpanProcessor added in v0.5.1

func WithSpanProcessor(sp sdktrace.SpanProcessor) ProviderOption

WithSpanProcessor attaches an additional sdktrace.SpanProcessor alongside the OTLP batch processor (or in place of it, when the exporter protocol is "none"). Use this to add a console/debug processor, or a test tracetest.SpanRecorder to observe the provider's output.

type Span

type Span struct {
	// contains filtered or unexported fields
}

Span adapts an OpenTelemetry span to modulex.Span.

func (*Span) End

func (s *Span) End()

End implements modulex.Span.

func (*Span) RecordError

func (s *Span) RecordError(err error)

RecordError implements modulex.Span.

func (*Span) SetAttributes

func (s *Span) SetAttributes(attrs map[string]any)

SetAttributes implements modulex.Span.

type SubscriberOption added in v0.2.0

type SubscriberOption func(*subscriberConfig)

SubscriberOption configures the subscriber middleware.

func WithSubscriberTopicAttr added in v0.2.0

func WithSubscriberTopicAttr(key string) SubscriberOption

WithSubscriberTopicAttr sets the attribute key used to record the topic name on subscriber spans. Default is "messaging.destination".

type Tracer

type Tracer struct {
	// contains filtered or unexported fields
}

Tracer adapts an OpenTelemetry TracerProvider to modulex.Tracer.

func (*Tracer) ContextWithSpanContext

func (t *Tracer) ContextWithSpanContext(ctx context.Context, sc modulex.SpanContext) context.Context

ContextWithSpanContext implements modulex.Tracer.

func (*Tracer) SpanContextFromContext

func (t *Tracer) SpanContextFromContext(ctx context.Context) modulex.SpanContext

SpanContextFromContext implements modulex.Tracer.

func (*Tracer) Start

func (t *Tracer) Start(ctx context.Context, spanName string, attrs map[string]any) (context.Context, modulex.Span)

Start implements modulex.Tracer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL