observability

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 3 Imported by: 0

README

internal/observability

Observability contracts (Tracer, Span, Meter, LogEmitter, Runtime) used by usecase, delivery, and infra. Implementations live in infra.

Pattern used

  • Framework-agnostic interfaces so app layers do not import OpenTelemetry.
  • Context helpers (WithRequestID, WithTrace, etc.) carry correlation IDs across layers.
  • No infra import in usecase/delivery for observability; only this package.

How to extend

  • Add new capabilities behind interfaces first.
  • Keep OTel/vendor details inside internal/infra/observability/otel.
  • Wire implementations in app; ensure shutdown is propagated.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

func TraceFromContext

func TraceFromContext(ctx context.Context) (traceID, spanID string)

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

func WithTrace

func WithTrace(ctx context.Context, traceID, spanID string) context.Context

Types

type Counter

type Counter interface {
	Add(ctx context.Context, value int64, fields ...Fields)
}

Counter records monotonically increasing integer values.

type Fields

type Fields map[string]any

Fields is an observability-agnostic key/value hashmap.

func FromPairs

func FromPairs(pairs ...any) Fields

FromPairs creates attributes from key/value pairs. If pairs length is odd, the last key is ignored.

func MergeFields

func MergeFields(fieldSets ...Fields) Fields

func OptionalFields

func OptionalFields(fieldSets ...Fields) Fields

OptionalFields returns the first variadic Fields argument, or nil if none provided.

type Histogram

type Histogram interface {
	Record(ctx context.Context, value float64, fields ...Fields)
}

Histogram records distribution samples.

type LogEmitter

type LogEmitter interface {
	Emit(ctx context.Context, severity Severity, message string, fields ...Fields)
}

LogEmitter is the contract for a secondary log sink.

type Meter

type Meter interface {
	Counter(name string, options ...MetricOption) Counter
	UpDownCounter(name string, options ...MetricOption) UpDownCounter
	Histogram(name string, options ...MetricOption) Histogram
}

Meter creates and owns metric instruments for app layers.

type MetricOption

type MetricOption struct {
	Description string
	Unit        string
}

MetricOption configures metric instruments.

type NoopLogEmitter

type NoopLogEmitter struct{}

NoopLogEmitter is a no-op implementation of LogEmitter.

func (NoopLogEmitter) Emit

type NoopMeter

type NoopMeter struct{}

NoopMeter is a no-op implementation of Meter.

func (NoopMeter) Counter

func (NoopMeter) Counter(string, ...MetricOption) Counter

func (NoopMeter) Histogram

func (NoopMeter) Histogram(string, ...MetricOption) Histogram

func (NoopMeter) UpDownCounter

func (NoopMeter) UpDownCounter(string, ...MetricOption) UpDownCounter

type NoopRuntime

type NoopRuntime struct{}

NoopRuntime is a no-op observability facade.

func (NoopRuntime) LogEmitter

func (NoopRuntime) LogEmitter() LogEmitter

func (NoopRuntime) Meter

func (NoopRuntime) Meter() Meter

func (NoopRuntime) Shutdown

func (NoopRuntime) Shutdown(context.Context) error

func (NoopRuntime) Tracer

func (NoopRuntime) Tracer() Tracer

type NoopTracer

type NoopTracer struct{}

func (NoopTracer) ExtractHTTP

func (NoopTracer) ExtractHTTP(ctx context.Context, _ http.Header) context.Context

func (NoopTracer) Start

func (NoopTracer) Start(ctx context.Context, _ string, _ string, _ ...Fields) (context.Context, Span)

func (NoopTracer) StartServer

func (NoopTracer) StartServer(ctx context.Context, _ string, _ string, _ ...Fields) (context.Context, Span)

type Runtime

type Runtime interface {
	LogEmitter() LogEmitter
	Tracer() Tracer
	Meter() Meter
	Shutdown(ctx context.Context) error
}

Runtime is the observability facade exposed to app wiring.

type Severity

type Severity uint8
const (
	SeverityDebug Severity = iota
	SeverityInfo
	SeverityWarn
	SeverityError
)

func (Severity) String

func (s Severity) String() string

type Span

type Span interface {
	SetAttributes(fields ...Fields)
	Finish(err error, description ...string)
	IDs() (traceID, spanID string, ok bool)
}

Span is an observability-agnostic span contract for app layers.

type Tracer

type Tracer interface {
	Start(ctx context.Context, scope, spanName string, fields ...Fields) (context.Context, Span)
	StartServer(ctx context.Context, scope, spanName string, fields ...Fields) (context.Context, Span)
	ExtractHTTP(ctx context.Context, headers http.Header) context.Context
}

Tracer is injected into app layers to avoid direct OTel dependency.

type UpDownCounter

type UpDownCounter interface {
	Add(ctx context.Context, value int64, fields ...Fields)
}

UpDownCounter records integer values that can increase or decrease.

Jump to

Keyboard shortcuts

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