telemetry

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AttrTenantKey    = attribute.Key("tenant_id")
	AttrPartitionKey = attribute.Key("partition_id")
)

Attribute keys for multi-tenant context.

View Source
var (
	AttrMethodKey  = attribute.Key("frame_method")
	AttrPackageKey = attribute.Key("frame_package")
	AttrStatusKey  = attribute.Key("frame_status")
	AttrErrorKey   = attribute.Key("frame_error")
)

Common attribute keys used across the frame.

Functions

func BytesMeasure

func BytesMeasure(pkg string, meterName string, description string) metric.Int64Counter

BytesMeasure creates a counter for bytes measurements.

func CounterView

func CounterView(pkg string, meterName string, description string) []sdkmetric.View

CounterView returns summation views that add up individual measurements the counter takes.

func DimensionlessMeasure

func DimensionlessMeasure(pkg string, meterName string, description string) metric.Int64Counter

DimensionlessMeasure creates a simple counter specifically for dimensionless measurements.

func ErrorCode

func ErrorCode(err error) string

func LatencyMeasure

func LatencyMeasure(pkg string) metric.Float64Histogram

LatencyMeasure returns the measure for method call latency used by Go CDK APIs.

func NewTraceContextHandler

func NewTraceContextHandler(inner slog.Handler) slog.Handler

NewTraceContextHandler wraps an existing slog.Handler to inject trace context.

func TenantAttributes

func TenantAttributes(ctx context.Context) []attribute.KeyValue

TenantAttributes extracts tenant_id and partition_id from the context's security claims and returns them as OpenTelemetry attributes. When claims are absent or fields are empty, those attributes are omitted rather than recorded as blank strings. This makes the function safe to call unconditionally — unauthenticated or pre-auth code paths simply get an empty slice.

func TraceContextHandlerWrapper

func TraceContextHandlerWrapper() func(slog.Handler) slog.Handler

TraceContextHandlerWrapper returns a function suitable for util.WithLogHandlerWrapper that wraps any slog.Handler with trace context injection.

func Views

func Views(pkg string) []sdkmetric.View

func WithTenantAttributes

func WithTenantAttributes(ctx context.Context) metric.MeasurementOption

WithTenantAttributes returns a metric.MeasurementOption that attaches tenant_id and partition_id from the context. Services use this when recording custom product metrics so every data point is automatically attributed to the correct tenant and partition.

Usage:

counter.Add(ctx, 1,
    telemetry.WithTenantAttributes(ctx),
    metric.WithAttributes(attribute.String("login_method", "email")),
)

Types

type BusinessMetrics

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

BusinessMetrics is the standard factory for product/business metrics. Every instrument it creates is tenant-scoped TRANSPARENTLY: each measurement automatically carries tenant_id and partition_id derived from the context's security claims (see TenantAttributes), so call sites cannot forget tenant attribution. Unauthenticated/system paths simply record without tenant attributes.

Usage:

var bm = telemetry.NewBusinessMetrics("service-loans")
var loansCreated = bm.Counter("loans_created_total", "New loan accounts created")
...
loansCreated.Add(ctx, 1) // tenant_id/partition_id attached from ctx

func NewBusinessMetrics

func NewBusinessMetrics(meterName string) *BusinessMetrics

NewBusinessMetrics returns a factory bound to the named meter on the global OTel meter provider (frame's telemetry manager sets it up).

func (*BusinessMetrics) Counter

func (b *BusinessMetrics) Counter(name, description string, opts ...metric.Int64CounterOption) Counter

Counter creates a tenant-scoped counter. On instrument-creation error it returns a no-op-backed instrument (matching LatencyMeasure's fallback behaviour) — metrics must never break the service.

func (*BusinessMetrics) FloatCounter

func (b *BusinessMetrics) FloatCounter(name, description string, opts ...metric.Float64CounterOption) FloatCounter

FloatCounter creates a tenant-scoped float64 counter, suited to monetary amounts in major units.

func (*BusinessMetrics) FloatGauge

func (b *BusinessMetrics) FloatGauge(name, description string, opts ...metric.Float64GaugeOption) FloatGauge

FloatGauge creates a tenant-scoped float64 gauge.

func (*BusinessMetrics) Gauge

func (b *BusinessMetrics) Gauge(name, description string, opts ...metric.Int64GaugeOption) Gauge

Gauge creates a tenant-scoped int64 gauge.

func (*BusinessMetrics) Histogram

func (b *BusinessMetrics) Histogram(name, description string, opts ...metric.Float64HistogramOption) Histogram

Histogram creates a tenant-scoped histogram in milliseconds by default; pass metric.WithUnit to override.

type Counter

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

Counter is a tenant-scoped monotonic int64 counter.

func (Counter) Add

func (c Counter) Add(ctx context.Context, incr int64, attrs ...attribute.KeyValue)

Add increments the counter; tenant attributes come from ctx.

type FloatCounter

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

FloatCounter is a tenant-scoped monotonic float64 counter (amounts).

func (FloatCounter) Add

func (c FloatCounter) Add(ctx context.Context, incr float64, attrs ...attribute.KeyValue)

Add increments the counter; tenant attributes come from ctx.

type FloatGauge

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

FloatGauge is a tenant-scoped float64 gauge (last-value).

func (FloatGauge) Record

func (g FloatGauge) Record(ctx context.Context, value float64, attrs ...attribute.KeyValue)

Record sets the gauge; tenant attributes come from ctx.

type Gauge

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

Gauge is a tenant-scoped int64 gauge (last-value).

func (Gauge) Record

func (g Gauge) Record(ctx context.Context, value int64, attrs ...attribute.KeyValue)

Record sets the gauge; tenant attributes come from ctx.

type Histogram

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

Histogram is a tenant-scoped float64 histogram.

func (Histogram) Record

func (h Histogram) Record(ctx context.Context, value float64, attrs ...attribute.KeyValue)

Record records a value; tenant attributes come from ctx.

type Manager

type Manager interface {
	Init(ctx context.Context) error
	Disabled() bool
	LogHandler() slog.Handler
}

func NewManager

func NewManager(ctx context.Context, cfg config.ConfigurationTelemetry, opts ...Option) Manager

NewManager creates a new telemetry setup manager.

type Option

type Option func(ctx context.Context, m *manager)

func WithDisableTracing

func WithDisableTracing() Option

WithDisableTracing disable tracing for the service.

func WithMetricsReader

func WithMetricsReader(reader sdkmetrics.Reader) Option

WithMetricsReader specifies the metrics reader for the service.

func WithPropagationTextMap

func WithPropagationTextMap(carrier propagation.TextMapPropagator) Option

WithPropagationTextMap specifies the trace baggage carrier exporter to use.

func WithServiceEnvironment

func WithServiceEnvironment(env string) Option

WithServiceEnvironment sets the service environment for resource tagging.

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service name for resource tagging.

func WithServiceVersion

func WithServiceVersion(version string) Option

WithServiceVersion sets the service version for resource tagging.

func WithTraceExporter

func WithTraceExporter(exporter sdktrace.SpanExporter) Option

WithTraceExporter specifies the trace exporter to use.

func WithTraceLogsExporter

func WithTraceLogsExporter(exporter sdklogs.Exporter) Option

WithTraceLogsExporter specifies the trace logs exporter for the service.

func WithTraceSampler

func WithTraceSampler(sampler sdktrace.Sampler) Option

WithTraceSampler specifies the trace sampler to use.

type TraceContextHandler

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

TraceContextHandler is an slog.Handler that injects trace_id and span_id from the OTel span context into every log record. This enables correlation between stdout/stderr logs and distributed traces in production.

func (*TraceContextHandler) Enabled

func (h *TraceContextHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*TraceContextHandler) Handle

func (h *TraceContextHandler) Handle(ctx context.Context, r slog.Record) error

func (*TraceContextHandler) WithAttrs

func (h *TraceContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*TraceContextHandler) WithGroup

func (h *TraceContextHandler) WithGroup(name string) slog.Handler

type Tracer

type Tracer interface {
	Start(ctx context.Context, methodName string, options ...trace.SpanStartOption) (context.Context, trace.Span)
	End(ctx context.Context, span trace.Span, err error, options ...trace.SpanEndOption)
}

func NewTracer

func NewTracer(name string, options ...trace.TracerOption) Tracer

NewTracer creates a new tracer for a package.

Jump to

Keyboard shortcuts

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