telemetry

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package telemetry provides OpenTelemetry tracing, metrics, and Prometheus exporter setup.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrServiceNameRequired indicates service name is missing.
	ErrServiceNameRequired = errors.New("service_name is required")
	// ErrServiceVersionRequired indicates service version is missing.
	ErrServiceVersionRequired = errors.New("service_version is required")
	// ErrEnvironmentRequired indicates environment is missing.
	ErrEnvironmentRequired = errors.New("environment is required")
	// ErrIntervalRequired indicates metrics interval must be positive.
	ErrIntervalRequired = errors.New("interval must be greater than 0")
	// ErrNoExportersEnabled indicates no telemetry exporters are enabled.
	ErrNoExportersEnabled = errors.New("at least one exporter (OTLP or Prometheus) must be enabled")
)
View Source
var (
	String  = attribute.String
	Int     = attribute.Int
	Int64   = attribute.Int64
	Float64 = attribute.Float64
	Bool    = attribute.Bool
)

Functions

This section is empty.

Types

type Attr

type Attr = attribute.KeyValue

Attr is an alias for attribute.KeyValue.

type Config

type Config struct {
	ServiceName       string        `toml:"service_name" env:"OTEL_SERVICE_NAME" env-default:"unknown_service"`
	ServiceVersion    string        `toml:"service_version" env:"OTEL_SERVICE_VERSION" env-default:"0.0.1"`
	Environment       string        `toml:"environment" env:"ENV" env-default:"production"`
	OTLPEndpoint      string        `toml:"otel_endpoint" env:"OTEL_EXPORTER_OTLP_ENDPOINT" env-default:"localhost:4317"`
	OTLPInsecure      bool          `toml:"otel_insecure" env:"OTEL_EXPORTER_OTLP_INSECURE" env-default:"true"`
	PrometheusEnabled bool          `toml:"prometheus_enabled" env:"METRICS_PROMETHEUS_ENABLED" env-default:"true"`
	Interval          time.Duration `toml:"interval" env:"OTEL_INTERVAL" env-default:"15s"`
}

Config defines OpenTelemetry and Prometheus configuration parameters.

func (*Config) Validate

func (c *Config) Validate() error

type Counter

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

Counter wraps an OpenTelemetry Int64Counter with dynamic MeterProvider resolution.

func MustCounter

func MustCounter(name, desc string) *Counter

MustCounter creates a Counter on the default scope or panics on failure.

func (*Counter) Add

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

Add adds value to the counter with explicit attributes.

func (*Counter) AddKV

func (c *Counter) AddKV(ctx context.Context, value int64, kv ...any)

AddKV adds value to the counter with key-value pair attributes.

func (*Counter) Inc

func (c *Counter) Inc(ctx context.Context, attrs ...attribute.KeyValue)

Inc increments the counter by 1 with explicit attributes.

func (*Counter) IncKV

func (c *Counter) IncKV(ctx context.Context, kv ...any)

IncKV increments the counter by 1 with key-value pair attributes.

type Gauge

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

Gauge wraps an OpenTelemetry Float64Gauge with dynamic MeterProvider resolution.

func MustGauge

func MustGauge(name, desc string) *Gauge

MustGauge creates a Gauge on the default scope or panics on failure.

func (*Gauge) Set

func (g *Gauge) Set(ctx context.Context, value float64, attrs ...attribute.KeyValue)

Set sets the gauge value with explicit attributes.

func (*Gauge) SetKV

func (g *Gauge) SetKV(ctx context.Context, value float64, kv ...any)

SetKV sets the gauge value with key-value pair attributes.

type Histogram

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

Histogram wraps an OpenTelemetry Float64Histogram with dynamic MeterProvider resolution.

func MustHistogram

func MustHistogram(name, desc string, opts ...HistogramOption) *Histogram

MustHistogram creates a Histogram on the default scope or panics on failure.

func (*Histogram) Observe

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

Observe records a value in the histogram with explicit attributes.

func (*Histogram) ObserveKV

func (h *Histogram) ObserveKV(ctx context.Context, value float64, kv ...any)

ObserveKV records a value in the histogram with key-value pair attributes.

type HistogramOption

type HistogramOption func(*histogramConfig)

HistogramOption configures histogram instrument parameters.

func WithBuckets

func WithBuckets(buckets []float64) HistogramOption

WithBuckets sets custom histogram bucket boundaries.

func WithUnit

func WithUnit(unit string) HistogramOption

WithUnit sets the histogram unit string.

type Int64Observer

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

Int64Observer wraps an asynchronous OpenTelemetry Int64Observer.

func (*Int64Observer) Observe

func (o *Int64Observer) Observe(value int64, attrs ...attribute.KeyValue)

Observe records a value with explicit attributes.

func (*Int64Observer) ObserveKV

func (o *Int64Observer) ObserveKV(value int64, kv ...any)

ObserveKV records a value with key-value pair attributes.

type Meter

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

Meter provides scoped metric instrument creation.

func NewMeter

func NewMeter(scopeName string) *Meter

NewMeter creates a Meter with the specified component scope.

func (*Meter) Counter

func (m *Meter) Counter(name, desc string) (*Counter, error)

Counter creates a new Counter.

func (*Meter) Gauge

func (m *Meter) Gauge(name, desc string) (*Gauge, error)

Gauge creates a new Gauge.

func (*Meter) Histogram

func (m *Meter) Histogram(name, desc string, opts ...HistogramOption) (*Histogram, error)

Histogram creates a new Histogram.

func (*Meter) Int64ObservableGauge

func (m *Meter) Int64ObservableGauge(name, desc string, cb ObservableGaugeCallback) error

Int64ObservableGauge registers an asynchronous gauge metric callback.

func (*Meter) MustCounter

func (m *Meter) MustCounter(name, desc string) *Counter

MustCounter creates a new Counter or panics on failure.

func (*Meter) MustGauge

func (m *Meter) MustGauge(name, desc string) *Gauge

MustGauge creates a new Gauge or panics on failure.

func (*Meter) MustHistogram

func (m *Meter) MustHistogram(name, desc string, opts ...HistogramOption) *Histogram

MustHistogram creates a new Histogram or panics on failure.

func (*Meter) MustUpDownCounter

func (m *Meter) MustUpDownCounter(name, desc string) *UpDownCounter

MustUpDownCounter creates a new UpDownCounter or panics on failure.

func (*Meter) UpDownCounter

func (m *Meter) UpDownCounter(name, desc string) (*UpDownCounter, error)

UpDownCounter creates a new UpDownCounter.

type ObservableGaugeCallback

type ObservableGaugeCallback func(ctx context.Context, obs *Int64Observer) error

ObservableGaugeCallback is invoked during asynchronous metric collection.

type Telemetry

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

Telemetry manages lifecycle for tracer and meter providers.

func Setup

func Setup(ctx context.Context, cfg Config) (*Telemetry, error)

Setup initializes global TracerProvider and MeterProvider instances based on configuration.

func (*Telemetry) Handler

func (t *Telemetry) Handler() http.Handler

Handler returns the HTTP handler exposing Prometheus metrics.

func (*Telemetry) Shutdown

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

Shutdown flushes and stops all telemetry exporters and servers.

func (*Telemetry) StartPrometheusServer

func (t *Telemetry) StartPrometheusServer(addr string) (*http.Server, error)

StartPrometheusServer starts a dedicated HTTP server for Prometheus scraping.

type UpDownCounter

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

UpDownCounter wraps an OpenTelemetry Int64UpDownCounter with dynamic MeterProvider resolution.

func MustUpDownCounter

func MustUpDownCounter(name, desc string) *UpDownCounter

MustUpDownCounter creates an UpDownCounter on the default scope or panics on failure.

func (*UpDownCounter) Add

func (g *UpDownCounter) Add(ctx context.Context, value int64, attrs ...attribute.KeyValue)

Add adds value to the counter with explicit attributes.

func (*UpDownCounter) AddKV

func (g *UpDownCounter) AddKV(ctx context.Context, value int64, kv ...any)

AddKV adds value to the counter with key-value pair attributes.

func (*UpDownCounter) Dec

func (g *UpDownCounter) Dec(ctx context.Context, attrs ...attribute.KeyValue)

Dec decrements the counter by 1 with explicit attributes.

func (*UpDownCounter) DecKV

func (g *UpDownCounter) DecKV(ctx context.Context, kv ...any)

DecKV decrements the counter by 1 with key-value pair attributes.

func (*UpDownCounter) Inc

func (g *UpDownCounter) Inc(ctx context.Context, attrs ...attribute.KeyValue)

Inc increments the counter by 1 with explicit attributes.

func (*UpDownCounter) IncKV

func (g *UpDownCounter) IncKV(ctx context.Context, kv ...any)

IncKV increments the counter by 1 with key-value pair attributes.

Jump to

Keyboard shortcuts

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