observability

package
v0.84.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const TracingHeader = "x-trace-id"

Variables

This section is empty.

Functions

func AddRequestAttributes added in v0.27.0

func AddRequestAttributes(ctx context.Context, attrs ...attribute.KeyValue)

AddRequestAttributes sets attributes on the current trace span, the finish log of the current request, and the activity track

func GrpcPeer added in v0.29.0

func GrpcPeer(ctx context.Context) string

GrpcPeer returns the client address, using the "real" IP passed by the load balancer if available.

func HTTPPeer added in v0.29.0

func HTTPPeer(r *http.Request) string

HTTPPeer returns the client address, using the "real" IP passed by the load balancer if available.

func LoggingMiddleware

func LoggingMiddleware(logger *zap.Logger, next http.Handler) http.Handler

LoggingMiddleware is a HTTP request logging middleware. Note: It also recovers from panics and handles them as internal errors. Also sends the trace ID in response headers to correlate with logs.

func LoggingStreamServerInterceptor

func LoggingStreamServerInterceptor(logger *zap.Logger) grpc.StreamServerInterceptor

LoggingStreamServerInterceptor is a gRPC streaming interceptor that logs requests. It also recovers from panics and returns them as internal errors. Also sends the trace ID in response headers to correlate with logs.

func LoggingUnaryServerInterceptor

func LoggingUnaryServerInterceptor(logger *zap.Logger) grpc.UnaryServerInterceptor

LoggingUnaryServerInterceptor is a gRPC unary interceptor that logs requests. It also recovers from panics and returns them as internal errors. Also sends the trace ID in response headers to correlate with logs.

func MCPMiddleware added in v0.78.0

func MCPMiddleware() mcp.Middleware

MCPMiddleware is a middleware for MCP servers that adds MCP-related observability attributes. It is expected to run on an MCP server that has already been wrapped with observability.Middleware(...).

func Middleware

func Middleware(serviceName string, logger *zap.Logger, next http.Handler) http.Handler

Middleware is HTTP middleware that combines all observability-related middlewares.

func Must

func Must[T any](val T, err error) T

func MuxHandle added in v0.42.0

func MuxHandle(mux *http.ServeMux, pattern string, handler http.Handler)

MuxHandle is a wrapper around http.ServeMux.Handle that adds route tags to the handler. It does NOT wrap the handler with observability.Middleware. The caller is expected to add that on the ServeMux itself.

func SearchTracesFile added in v0.60.7

func SearchTracesFile(ctx context.Context, traceID, resourceName string) ([]byte, error)

func TraceID added in v0.58.0

func TraceID(ctx context.Context) string

func TracingMiddleware

func TracingMiddleware(next http.Handler, serviceName string) http.Handler

TracingMiddleware is HTTP middleware that adds tracing to the request.

func WithRequestScopedCollector added in v0.84.0

func WithRequestScopedCollector(ctx context.Context, c *RequestScopedCollector) context.Context

WithRequestScopedCollector returns a new context with the given collector.

func ZapCtx

func ZapCtx(ctx context.Context) zap.Field

ZapCtx returns a Zap field that adds "trace_id" and "span_id" fields to the log message

Types

type Exporter

type Exporter string

Exporter lists available telemetry exporters

const (
	NoopExporter       Exporter = ""
	OtelExporter       Exporter = "otel"
	FileBasedExporter  Exporter = "file"
	PrometheusExporter Exporter = "prometheus"
)

type FileExporter added in v0.60.7

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

FileExporter writes OpenTelemetry traces to a rotating log file

func NewFileExporter added in v0.60.7

func NewFileExporter() (*FileExporter, error)

NewFileExporter initializes a file exporter with log rotation

func (*FileExporter) ExportSpans added in v0.60.7

func (fe *FileExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error

ExportSpans writes spans to the log file in JSON format

func (*FileExporter) Shutdown added in v0.60.7

func (fe *FileExporter) Shutdown(ctx context.Context) error

Shutdown closes the file (if needed)

type Options

type Options struct {
	MetricsExporter Exporter
	TracesExporter  Exporter
	ServiceName     string
	ServiceVersion  string
}

Options for configuring telemetry setup

type RequestScopedCollector added in v0.84.0

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

RequestScopedCollector accumulates span traces during a request.

func RequestScopedCollectorFromContext added in v0.84.0

func RequestScopedCollectorFromContext(ctx context.Context) (*RequestScopedCollector, bool)

RequestScopedCollectorFromContext extracts the collector from the context.

func (*RequestScopedCollector) Record added in v0.84.0

func (c *RequestScopedCollector) Record(e *runtimev1.Span)

Record adds a span trace entry.

func (*RequestScopedCollector) ToProto added in v0.84.0

func (c *RequestScopedCollector) ToProto() *runtimev1.Trace

ToProto returns the collected traces as Trace proto.

type RequestScopedSpanProcessor added in v0.84.0

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

RequestScopedSpanProcessor is a trace.SpanProcessor that captures spans and routes them to request-scoped collectors. This only works when a collector is present in the parent context, which is only set when req.trace is set on runtimev1 query requests.

func NewRequestScopedSpanProcessor added in v0.84.0

func NewRequestScopedSpanProcessor() *RequestScopedSpanProcessor

NewRequestScopedSpanProcessor creates a new RequestScopedSpanProcessor.

func (*RequestScopedSpanProcessor) ForceFlush added in v0.84.0

func (p *RequestScopedSpanProcessor) ForceFlush(ctx context.Context) error

ForceFlush is a no-op.

func (*RequestScopedSpanProcessor) OnEnd added in v0.84.0

OnEnd processes completed spans, building a generic Span proto and recording it to the collector.

func (*RequestScopedSpanProcessor) OnStart added in v0.84.0

OnStart extracts the collector from the parent context and stores it keyed by spanID.

func (*RequestScopedSpanProcessor) Shutdown added in v0.84.0

Shutdown is a no-op.

type ShutdownFunc

type ShutdownFunc func(context.Context) error

ShutdownFunc stops global telemetry

func Start

func Start(ctx context.Context, logger *zap.Logger, opts *Options) (ShutdownFunc, error)

Start configures traces and metrics globally. Use "go.opentelemetry.io/otel.Tracer" to access global tracers. Use "go.opentelemetry.io/otel.Meter" to access global meters. If using OtelExporter (otel collector), make sure to set the OTEL_EXPORTER_OTLP_ENDPOINT env var. For a full list of Otel env vars, see: https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace.

Jump to

Keyboard shortcuts

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