telemetry

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package telemetry sets up OpenTelemetry tracing and metrics for hex applications.

One Setup call wires:

  • a tracer provider with the configured exporter,
  • a meter provider with the configured exporter,
  • a resource carrying service name/version,
  • propagation (tracecontext + baggage),
  • global registration so any library reading `otel.Tracer(...)` sees them.

The returned Provider carries a single Shutdown method that flushes and closes every configured exporter, ready to defer at the end of main.

See ADR-0014.

Example:

tp, err := telemetry.Setup(ctx, telemetry.Options{
    ServiceName:    "myapp",
    ServiceVersion: build.Version(),
    Exporter:       telemetry.ExporterStdout,
})
if err != nil { return err }
defer tp.Shutdown(context.Background())

tracer := telemetry.Tracer("myapp/http")
ctx, span := tracer.Start(ctx, "handle-request")
defer span.End()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Meter

func Meter(name string, opts ...metric.MeterOption) metric.Meter

Meter returns a Meter with the given instrumentation name.

func Tracer

func Tracer(name string, opts ...trace.TracerOption) trace.Tracer

Tracer returns a Tracer with the given instrumentation name. Backed by the global TracerProvider Setup installed.

Types

type Exporter

type Exporter int

Exporter selects the exporter backend.

const (
	// ExporterNone disables the exporter entirely — spans/metrics are
	// discarded. Useful in tests.
	ExporterNone Exporter = iota
	// ExporterStdout writes spans and metrics as JSON to stdout.
	// Default for dev.
	ExporterStdout
	// ExporterOTLP ships to an OTLP-compatible collector over gRPC.
	// Production default. Endpoint is picked from OTEL_EXPORTER_OTLP_ENDPOINT
	// environment variable per the OTel spec.
	ExporterOTLP
)

type Options

type Options struct {
	// ServiceName identifies the service in traces and metrics. Required.
	ServiceName string

	// ServiceVersion is optional but strongly recommended for correlating
	// telemetry to specific builds. Feed hex/build.Version() here.
	ServiceVersion string

	// Environment tags telemetry with the deployment environment (e.g.
	// "dev", "staging", "production"). Optional.
	Environment string

	// Exporter selects the exporter. Defaults to ExporterStdout.
	Exporter Exporter

	// ResourceAttributes are additional key/value pairs attached to every
	// emitted span and metric.
	ResourceAttributes map[string]string

	// TraceExporter, MetricExporter override the exporter for one signal.
	// Useful when a consumer wants OTLP for traces and stdout for metrics
	// (or a custom exporter neither shipped here supports).
	TraceExporter  sdktrace.SpanExporter
	MetricExporter sdkmetric.Exporter
}

Options configures Setup.

type Provider

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

Provider carries the tracer and meter providers plus a Shutdown method that flushes and closes them.

func Setup

func Setup(ctx context.Context, opts Options) (*Provider, error)

Setup configures OTel with opts and returns a Provider whose Shutdown method should be deferred at the end of main.

func (*Provider) MeterProvider

func (p *Provider) MeterProvider() *sdkmetric.MeterProvider

MeterProvider returns the underlying SDK meter provider.

func (*Provider) Resource

func (p *Provider) Resource() *resource.Resource

Resource returns the attached resource. Handy for asserting attributes in tests.

func (*Provider) Shutdown

func (p *Provider) Shutdown(ctx context.Context) error

Shutdown flushes and closes the tracer and meter providers. Idempotent — subsequent calls after the first are no-ops.

func (*Provider) TracerProvider

func (p *Provider) TracerProvider() *sdktrace.TracerProvider

TracerProvider returns the underlying SDK tracer provider. Escape hatch for consumers that need to add SpanProcessor, override sampling, etc.

Directories

Path Synopsis
Package provider is the default hex/telemetry service provider.
Package provider is the default hex/telemetry service provider.

Jump to

Keyboard shortcuts

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