observability

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultNoopTracer  interfaces.Tracer  = &NoopTracer{}
	DefaultNoopMetrics interfaces.Metrics = &NoopMetrics{}
	DefaultNoopLogs    interfaces.Logs    = &NoopLogs{}
)

DefaultNoopTracer, DefaultNoopMetrics, and DefaultNoopLogs are shared no-op implementations for callers that omit explicit observability wiring (e.g. internal/runtime/temporal.buildTemporalRuntimeConfig).

Functions

func NewMetrics

func NewMetrics(opts ...Option) (interfaces.Metrics, error)

NewMetrics constructs a Metrics from the given options:

  1. Calls BuildConfig to validate and apply defaults.
  2. Builds an OTLP metrics exporter (gRPC or HTTP per [Config.Protocol]).
  3. Wraps it in a sdkmetric.PeriodicReader that pushes at [Config.MetricsInterval].
  4. Creates a sdkmetric.MeterProvider with an OTLP resource (service name, version, environment).
  5. Returns a Metrics scoped to [Config.Name].

func NewTracer

func NewTracer(opts ...Option) (interfaces.Tracer, error)

NewTracer constructs a Tracer from the given options:

  1. Calls BuildConfig to validate and apply defaults.
  2. Builds an OTLP span exporter (gRPC or HTTP per [Config.Protocol]).
  3. Wraps it in a sdktrace.BatchSpanProcessor for async, batched export.
  4. Creates a sdktrace.TracerProvider with an OTLP resource (service name, version, environment).
  5. Returns a Tracer scoped to [Config.Name].

Types

type Config

type Config struct {
	// Logger receives exporter and diagnostics messages when wiring fails or during shutdown.
	Logger logger.Logger
	// LogLevel is used when Logger is unset (same strings as [logger.DefaultLogger]: debug, info, warn, error).
	LogLevel string

	// Endpoint is the OTLP collector URL, e.g. "collector:4317" (gRPC) or
	// "http://collector:4318" (HTTP). Required.
	Endpoint string

	// Name is the service / scope name attached to all telemetry. Required.
	Name string

	// Protocol selects the OTLP wire transport. Defaults to [ProtocolGRPC].
	Protocol Protocol

	// Headers are extra gRPC metadata or HTTP headers sent with every export request
	// (e.g. {"Authorization": "Bearer <token>"} for SaaS backends). Optional.
	Headers map[string]string

	// Insecure disables TLS. Set true for local / dev collectors that have no cert.
	Insecure bool

	// ServiceVersion is added to the OTLP resource as "service.version". Optional.
	ServiceVersion string

	// DeploymentEnvironment is added to the OTLP resource as "deployment.environment"
	// (e.g. "production", "staging"). Optional.
	DeploymentEnvironment string

	// ExportTimeout is the per-export call deadline.
	// Defaults to [types.DefaultOTLPExportTimeout] (30 s).
	ExportTimeout time.Duration

	// BatchTimeout is the maximum delay before a trace batch is flushed.
	// Lower values reduce trace latency at the cost of throughput.
	// Defaults to [types.DefaultOTLPBatchTimeout] (5 s).
	BatchTimeout time.Duration

	// MetricsInterval is how often the metrics periodic reader pushes to the collector.
	// Defaults to [types.DefaultOTLPMetricsInterval] (60 s).
	MetricsInterval time.Duration

	// SamplingRatio controls trace sampling between 0.0 (drop all) and 1.0 (keep all).
	// Values ≤ 0 or > 1 fall back to AlwaysSample.
	SamplingRatio float64
}

Config holds all settings for constructing OTLP-backed Tracer and Metrics clients. Build it with BuildConfig after applying functional [Option]s.

All fields that affect exporter timing and behaviour are exposed here so that callers using this package directly have full control. When observability is configured through pkg/agent.ObservabilityConfig the agent SDK applies the [types.DefaultOTLP*] constants automatically.

func BuildConfig

func BuildConfig(opts ...Option) (*Config, error)

BuildConfig merges options into Config and applies defaults:

Returns an error when Endpoint or Name is empty.

type Logs

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

Logs wraps sdklog.LoggerProvider and implements interfaces.Logs. Construct it with NewLogs; the zero value is not usable. Call Logs.Shutdown when the agent or worker stops to flush buffered log records.

func NewLogs

func NewLogs(opts ...Option) (*Logs, error)

NewLogs constructs a Logs from the given options:

  1. Calls BuildConfig to validate and apply defaults.
  2. Builds an OTLP log exporter (gRPC or HTTP per [Config.Protocol]).
  3. Wraps it in a sdklog.BatchProcessor for async, batched export.
  4. Creates a sdklog.LoggerProvider with the same service resource as tracer/metrics.
  5. Returns a Logs whose Logs.Provider can be passed to the logger bridge.

func (*Logs) Provider

func (l *Logs) Provider() *sdklog.LoggerProvider

Provider returns the underlying sdklog.LoggerProvider. Pass it to pkg/logger.DefaultLoggerWithOtelProvider so the slog bridge sends records through this provider rather than the (likely-unset) global OTel LoggerProvider.

func (*Logs) Shutdown

func (l *Logs) Shutdown(ctx context.Context) error

Shutdown flushes buffered log records and releases the exporter connection.

type Metrics

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

Metrics implements interfaces.Metrics backed by an OTLP sdkmetric.MeterProvider.

Counters and histograms are created lazily on first use and cached for the lifetime of the Metrics instance. Construct it with NewMetrics; the zero value is not usable. Call Metrics.Shutdown when the agent or worker stops to flush pending metric data.

func (*Metrics) IncrementCounter

func (m *Metrics) IncrementCounter(ctx context.Context, name string, attrs ...interfaces.Attribute)

IncrementCounter adds 1 to the named Int64Counter, creating it lazily on first call. Attributes are converted from interfaces.Attribute to OTel key-value pairs.

func (*Metrics) RecordHistogram

func (m *Metrics) RecordHistogram(ctx context.Context, name string, value float64, attrs ...interfaces.Attribute)

RecordHistogram records value on the named Float64Histogram, creating it lazily on first call. Attributes are converted from interfaces.Attribute to OTel key-value pairs.

func (*Metrics) Shutdown

func (m *Metrics) Shutdown(ctx context.Context) error

Shutdown flushes all pending metric data and releases the exporter connection. It must be called once when the agent or worker exits.

type NoopLogs

type NoopLogs struct{}

NoopLogs is a no-op interfaces.Logs used when log export is disabled or [WithObservabilityConfig] is not set. Shutdown is a no-op so Agent.Close is always safe to call.

func (*NoopLogs) Shutdown

func (n *NoopLogs) Shutdown(ctx context.Context) error

type NoopMetrics

type NoopMetrics struct{}

func (*NoopMetrics) IncrementCounter

func (n *NoopMetrics) IncrementCounter(ctx context.Context, name string, attrs ...interfaces.Attribute)

func (*NoopMetrics) RecordHistogram

func (n *NoopMetrics) RecordHistogram(ctx context.Context, name string, value float64, attrs ...interfaces.Attribute)

func (*NoopMetrics) Shutdown

func (n *NoopMetrics) Shutdown(ctx context.Context) error

type NoopSpan

type NoopSpan struct{}

func (*NoopSpan) End

func (n *NoopSpan) End()

func (*NoopSpan) RecordError

func (n *NoopSpan) RecordError(err error)

func (*NoopSpan) SetAttribute

func (n *NoopSpan) SetAttribute(key string, value any)

type NoopTracer

type NoopTracer struct{}

func (*NoopTracer) Shutdown

func (n *NoopTracer) Shutdown(ctx context.Context) error

func (*NoopTracer) StartSpan

func (n *NoopTracer) StartSpan(ctx context.Context, name string, attrs ...interfaces.Attribute) (context.Context, interfaces.Span)

type Option

type Option func(*Config)

Option mutates Config when passed to BuildConfig, NewTracer, or NewMetrics.

func WithBatchTimeout

func WithBatchTimeout(d time.Duration) Option

WithBatchTimeout sets the maximum delay before a trace span batch is flushed. Defaults to types.DefaultOTLPBatchTimeout (5 s).

func WithDeploymentEnvironment

func WithDeploymentEnvironment(env string) Option

WithDeploymentEnvironment attaches the deployment environment to the OTLP resource ("deployment.environment"), e.g. "production" or "staging".

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint sets the OTLP collector URL. Required.

func WithExportTimeout

func WithExportTimeout(d time.Duration) Option

WithExportTimeout sets the per-export call deadline. Defaults to types.DefaultOTLPExportTimeout (30 s).

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders sets extra per-request headers (gRPC metadata or HTTP headers). Common use: auth tokens for hosted OTLP backends.

func WithInsecure

func WithInsecure(insecure bool) Option

WithInsecure disables TLS for the OTLP connection. Use only in development.

func WithLogLevel

func WithLogLevel(level string) Option

WithLogLevel sets the level used when WithLogger is not set (same strings as logger.DefaultLogger: debug, info, warn, error). Empty defaults to "error" in BuildConfig.

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets structured logging for OTLP setup and lifecycle.

func WithMetricsInterval

func WithMetricsInterval(d time.Duration) Option

WithMetricsInterval sets how often the periodic metrics reader pushes to the collector. Defaults to types.DefaultOTLPMetricsInterval (60 s).

func WithName

func WithName(name string) Option

WithName sets the telemetry scope name (typically the agent name). Required.

func WithProtocol

func WithProtocol(p Protocol) Option

WithProtocol selects the OTLP wire transport. Defaults to ProtocolGRPC.

func WithSamplingRatio

func WithSamplingRatio(r float64) Option

WithSamplingRatio sets the trace sampling probability in [0.0, 1.0]. Values outside the range fall back to AlwaysSample (keep everything).

func WithServiceVersion

func WithServiceVersion(v string) Option

WithServiceVersion attaches the service version to the OTLP resource ("service.version").

type Protocol

type Protocol = types.OTLPProtocol

Protocol is an alias for types.OTLPProtocol re-exported so callers of this package do not need to import internal/types directly.

const (
	// ProtocolGRPC exports telemetry over gRPC (default; most collectors support it).
	ProtocolGRPC Protocol = types.OTLPProtocolGRPC
	// ProtocolHTTP exports telemetry over HTTP/protobuf (useful when gRPC is blocked).
	ProtocolHTTP Protocol = types.OTLPProtocolHTTP
)

type Tracer

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

Tracer implements interfaces.Tracer backed by an OTLP sdktrace.TracerProvider.

Construct it with NewTracer; the zero value is not usable. Call Tracer.Shutdown when the agent or worker stops to flush buffered spans.

func (*Tracer) OTelTracer

func (t *Tracer) OTelTracer() trace.Tracer

OTelTracer returns the underlying OpenTelemetry Tracer. This is an optional OTel specific extension to the interfaces.OTelTracer interface.

func (*Tracer) Shutdown

func (t *Tracer) Shutdown(ctx context.Context) error

Shutdown flushes buffered spans and releases the exporter connection. It must be called once when the agent or worker exits.

func (*Tracer) StartSpan

func (t *Tracer) StartSpan(ctx context.Context, name string, attrs ...interfaces.Attribute) (context.Context, interfaces.Span)

StartSpan begins a new span named name under ctx. Attributes are converted from interfaces.Attribute to OpenTelemetry key-value pairs. The returned context carries the active span so nested StartSpan calls form a parent-child tree.

Jump to

Keyboard shortcuts

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