gocloud.devgocloud.dev/internal/otel Index | Files

package otel

import "gocloud.dev/internal/otel"

Package otel supports OpenTelemetry tracing and metrics for the Go Cloud Development Kit.

Index

Variables

var (
	MethodKey   = attribute.Key("gocdk.method")
	PackageKey  = attribute.Key("gocdk.package")
	ProviderKey = attribute.Key("gocdk.provider")
	StatusKey   = attribute.Key("gocdk.status")
	ErrorKey    = attribute.Key("gocdk.error")
)

Common attribute keys used across the Go CDK

Functions

func ConfigureMeterProvider

func ConfigureMeterProvider(ctx context.Context, exporter sdkmetric.Exporter, serviceName string) (func(context.Context) error, func(context.Context) error, error)

ConfigureMeterProvider sets up the given meter provider with the given exporter. It returns a function to collect and export metrics on demand, and a shutdown function.

func ConfigureTraceProvider

func ConfigureTraceProvider(ctx context.Context, exporter sdktrace.SpanExporter, serviceName string) (func(context.Context) error, error)

ConfigureTraceProvider sets up the global trace provider with the given exporter. It returns a function to shut down the exporter.

func InitMetrics

func InitMetrics(ctx context.Context, serviceName string) (func(context.Context) error, error)

InitMetrics initializes metrics with an OTLP exporter.

func MeterForPackage

func MeterForPackage(pkg string) metric.Meter

MeterForPackage returns a meter for the given package using the global provider.

func ProviderName

func ProviderName(driver any) string

ProviderName returns the name of the provider associated with the driver value. It is intended to be used to set Tracer.Provider. It actually returns the package path of the driver's type.

func RecordBytesRead

func RecordBytesRead(ctx context.Context, counter metric.Int64Counter, provider string, n int64)

RecordBytesRead records bytes read with provider attribute.

func RecordBytesWritten

func RecordBytesWritten(ctx context.Context, counter metric.Int64Counter, provider string, n int64)

RecordBytesWritten records bytes written with provider attribute.

func RecordCount

func RecordCount(ctx context.Context, counter metric.Int64Counter, method, provider, status string, count int64)

RecordCount records a count with standard attributes.

func RecordLatency

func RecordLatency(ctx context.Context, latencyMeter metric.Float64Histogram, method, provider, status string, duration time.Duration)

RecordLatency records a latency measurement with standard attributes.

func SpanFromContext

func SpanFromContext(ctx context.Context) trace.Span

SpanFromContext retrieves the current span from the context.

func StartSpan

func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

StartSpan is a convenience function that creates a span using the global tracer.

func TraceCall

func TraceCall(ctx context.Context, name string, fn func(context.Context) error) error

TraceCall is a helper that traces the execution of a function.

func TracerForPackage

func TracerForPackage(pkg string) trace.Tracer

TracerForPackage returns a tracer for the given package using the global provider.

func TracingEnabled

func TracingEnabled() bool

TracingEnabled returns whether tracing is currently enabled.

Types

type MetricSet

type MetricSet struct {
	Latency        metric.Float64Histogram
	CompletedCalls metric.Int64Counter
	BytesRead      metric.Int64Counter
	BytesWritten   metric.Int64Counter
}

MetricSet contains the standard metrics used by Go CDK APIs.

func NewMetricSet

func NewMetricSet(ctx context.Context, pkg string) (*MetricSet, error)

NewMetricSet creates a standard set of metrics for a Go CDK package.

type Tracer

type Tracer struct {
	Package  string
	Provider string
}

Tracer provides OpenTelemetry tracing for Go CDK packages.

func NewTracer

func NewTracer(pkg string, provider ...string) *Tracer

NewTracer creates a new Tracer for a package and optional provider.

func (*Tracer) End

func (t *Tracer) End(span trace.Span, err error)

End completes a span with error information if applicable.

func (*Tracer) Start

func (t *Tracer) Start(ctx context.Context, methodName string) (context.Context, trace.Span)

Start creates and starts a new span and returns the updated context and span.

Source Files

init.go metrics.go trace.go

Version
v0.42.0
Published
Jun 28, 2025
Platform
js/wasm
Imports
17 packages
Last checked
1 day ago

Tools for package owners.