Documentation
¶
Overview ¶
Package otello provides unified observability (tracing, logging, metrics) for Go backends. It exports directly to Tempo (gRPC) and Loki (HTTP), with Prometheus scrape for metrics.
Index ¶
- Constants
- func EndSpanWithError(span trace.Span, err error)
- func ExtractTraceContext(ctx context.Context, carrier MapCarrier) context.Context
- func GetLogger() zerolog.Logger
- func HTTPMiddleware() func(http.Handler) http.Handler
- func InitGORM(db *gorm.DB)
- func InitGORMWithMetrics(db *gorm.DB, sqlDB *sql.DB, slowThreshold time.Duration)
- func MetricsHandler() http.Handler
- func RegisterRuntimeMetrics(meter metric.Meter) error
- func SetError(span trace.Span, err error)
- func SetSuccess(span trace.Span)
- func SetupZerolog(cfg *Config) (zerolog.Logger, func())
- func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, func(error))
- func TraceOperation(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func TracedLogger(ctx context.Context, logger zerolog.Logger) zerolog.Logger
- func WithBusinessContext(ctx context.Context, bc BusinessContext) context.Context
- type BusinessContext
- type Config
- type LokiConfig
- type LokiLogExporter
- type MapCarrier
- type MetricsConfig
- type PyroscopeConfig
- type ShutdownFunc
Constants ¶
const ( AttrUserID = attribute.Key("app.user.id") AttrTenantID = attribute.Key("app.tenant.id") AttrOperation = attribute.Key("app.operation") AttrEntity = attribute.Key("app.entity.type") AttrEntityID = attribute.Key("app.entity.id") )
Standardized OTEL attribute keys for business context.
Variables ¶
This section is empty.
Functions ¶
func EndSpanWithError ¶
EndSpanWithError ends a span, recording the error if non-nil.
func ExtractTraceContext ¶
func ExtractTraceContext(ctx context.Context, carrier MapCarrier) context.Context
ExtractTraceContext extracts trace context from a MapCarrier into the context.
func HTTPMiddleware ¶
HTTPMiddleware returns a net/http middleware for tracing and metrics.
func InitGORMWithMetrics ¶
InitGORMWithMetrics registers tracing + slow query metrics + DB pool metrics. Pass the underlying *sql.DB for pool stats collection.
func MetricsHandler ¶
MetricsHandler returns an http.Handler that serves Prometheus metrics. Returns nil if metrics are not enabled.
func RegisterRuntimeMetrics ¶
RegisterRuntimeMetrics registers Go runtime metrics (goroutines, heap) on the given meter.
func SetupZerolog ¶
SetupZerolog configures zerolog with a multi-writer that outputs to stderr and optionally to Loki for real-time log shipping.
func StartSpan ¶
func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, func(error))
StartSpan starts a new span and returns the context and an end function. Call the end function with the error (or nil) when done.
ctx, end := otello.StartSpan(ctx, "myOperation") defer end(err)
func TraceOperation ¶
func TraceOperation(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
TraceOperation starts a span and returns the context and span for manual control.
ctx, span := otello.TraceOperation(ctx, "processOrder", otello.AttrEntity.String("order"))
defer span.End()
func TracedLogger ¶
TracedLogger returns a zerolog.Logger enriched with trace/span IDs from the context.
func WithBusinessContext ¶
func WithBusinessContext(ctx context.Context, bc BusinessContext) context.Context
WithBusinessContext injects a BusinessContext into the context.
Types ¶
type BusinessContext ¶
type BusinessContext struct {
UserID string
TenantID string
Operation string
Entity string
EntityID string
}
BusinessContext carries standardized business attributes across the stack.
func BusinessContextFromCtx ¶
func BusinessContextFromCtx(ctx context.Context) (BusinessContext, bool)
BusinessContextFromCtx extracts the BusinessContext from the context.
func (BusinessContext) ApplyToSpan ¶
func (bc BusinessContext) ApplyToSpan(span trace.Span)
ApplyToSpan sets business context attributes on the given span.
func (BusinessContext) Attributes ¶
func (bc BusinessContext) Attributes() []attribute.KeyValue
Attributes returns the non-empty business context fields as OTEL attributes.
type Config ¶
type Config struct {
// Service is the name of the service (e.g., "my-backend"). Required.
Service string
// Version of the service. Defaults to "1.0.0".
Version string
// Namespace is the k8s namespace, used as Loki label and OTEL resource attribute.
Namespace string
// TraceEndpoint overrides OTEL_EXPORTER_OTLP_ENDPOINT env var.
// Default: "tempo.telemetry.svc.cluster.local:4317"
TraceEndpoint string
// Loki enables log export to Loki. nil = disabled.
Loki *LokiConfig
// Metrics enables Prometheus metrics scrape endpoint. nil = disabled.
Metrics *MetricsConfig
// Pyroscope enables continuous profiling. nil = disabled.
Pyroscope *PyroscopeConfig
}
Config configures the otello observability stack.
type LokiConfig ¶
type LokiConfig struct {
// Endpoint overrides LOKI_ENDPOINT env var.
// Default: "http://loki.telemetry.svc.cluster.local:3100"
Endpoint string
}
LokiConfig configures the Loki log exporter.
type LokiLogExporter ¶
type LokiLogExporter struct {
// contains filtered or unexported fields
}
LokiLogExporter implements sdklog.Exporter and pushes logs to Loki via HTTP. Deprecated: Use the zerolog LokiWriter integration instead (automatic via Init).
func NewLokiLogExporter ¶
func NewLokiLogExporter(endpoint string, service, namespace string) *LokiLogExporter
NewLokiLogExporter creates a new exporter that pushes to Loki's push API. Deprecated: Use the zerolog LokiWriter integration instead (automatic via Init).
func (*LokiLogExporter) ForceFlush ¶
func (e *LokiLogExporter) ForceFlush(_ context.Context) error
ForceFlush forces a flush of pending logs.
type MapCarrier ¶
MapCarrier implements propagation.TextMapCarrier for use with NATS headers or any map[string]string transport.
func InjectTraceContext ¶
func InjectTraceContext(ctx context.Context) MapCarrier
InjectTraceContext injects the current trace context into a MapCarrier suitable for NATS headers or similar transports.
func (MapCarrier) Get ¶
func (c MapCarrier) Get(key string) string
func (MapCarrier) Keys ¶
func (c MapCarrier) Keys() []string
func (MapCarrier) Set ¶
func (c MapCarrier) Set(key, value string)
type PyroscopeConfig ¶
type PyroscopeConfig struct {
// Endpoint overrides PYROSCOPE_ENDPOINT env var.
// Default: "http://pyroscope.telemetry.svc.cluster.local:4040"
Endpoint string
}
PyroscopeConfig configures continuous profiling via Pyroscope.
type ShutdownFunc ¶
ShutdownFunc is the function returned by Init to gracefully shut down all providers.
func Init ¶
func Init(cfg Config) (ShutdownFunc, error)
Init initializes the otello observability stack. It returns a shutdown function that must be called on application exit.
shutdown, err := otello.Init(otello.Config{Service: "my-svc", Namespace: "my-ns"})
if err != nil { log.Fatal().Err(err).Msg("otello init failed") }
defer shutdown(ctx)