Documentation
¶
Overview ¶
Package otel provides OpenTelemetry instrumentation utilities for github.com/jasoet/pkg/v2.
This package offers:
- Centralized configuration for traces, metrics, and logs
- Library-specific semantic conventions
- No-op implementations when telemetry is disabled
Configuration ¶
Create an otel.Config with the desired providers:
cfg := &otel.Config{
TracerProvider: tracerProvider, // optional
MeterProvider: meterProvider, // optional
LoggerProvider: loggerProvider, // optional
ServiceName: "my-service",
ServiceVersion: "1.0.0",
}
Then pass this config to package configurations (server.Config, grpc options, etc.).
Telemetry Pillars ¶
Enable any combination of:
- Traces (distributed tracing)
- Metrics (measurements and aggregations)
- Logs (structured log export via OpenTelemetry standard)
Each pillar is independently controlled by setting its provider. Nil providers result in no-op implementations with zero overhead.
Semantic Conventions ¶
This package defines library-specific attributes (AttrServerPort, AttrGRPCMode, etc.) that complement standard OpenTelemetry semantic conventions.
Index ¶
- Constants
- type Config
- func (c *Config) GetLogger(scopeName string, opts ...log.LoggerOption) log.Logger
- func (c *Config) GetMeter(scopeName string, opts ...metric.MeterOption) metric.Meter
- func (c *Config) GetTracer(scopeName string, opts ...trace.TracerOption) trace.Tracer
- func (c *Config) IsLoggingEnabled() bool
- func (c *Config) IsMetricsEnabled() bool
- func (c *Config) IsTracingEnabled() bool
- func (c *Config) Shutdown(ctx context.Context) error
- func (c *Config) WithLoggerProvider(lp log.LoggerProvider) *Config
- func (c *Config) WithMeterProvider(mp metric.MeterProvider) *Config
- func (c *Config) WithServiceVersion(version string) *Config
- func (c *Config) WithTracerProvider(tp trace.TracerProvider) *Config
- func (c *Config) WithoutLogging() *Config
Constants ¶
const ( // Package identification AttrPackageName = attribute.Key("pkg.name") AttrPackageVersion = attribute.Key("pkg.version") // Server package attributes AttrServerPort = attribute.Key("pkg.server.port") // gRPC package attributes AttrGRPCMode = attribute.Key("pkg.grpc.mode") AttrGRPCPort = attribute.Key("pkg.grpc.port") AttrGRPCHTTPPort = attribute.Key("pkg.grpc.http_port") AttrGRPCReflection = attribute.Key("pkg.grpc.reflection_enabled") AttrGRPCGatewayEnabled = attribute.Key("pkg.grpc.gateway_enabled") // REST client package attributes AttrRESTClientName = attribute.Key("pkg.rest.client.name") AttrRESTRetryCount = attribute.Key("pkg.rest.retry.max_count") AttrRESTRetryAttempt = attribute.Key("pkg.rest.retry.attempt") AttrRESTTimeout = attribute.Key("pkg.rest.timeout_ms") // Database package attributes AttrDBConnectionPool = attribute.Key("pkg.db.pool.name") AttrDBType = attribute.Key("pkg.db.type") AttrDBMaxIdleConns = attribute.Key("pkg.db.pool.max_idle") AttrDBMaxOpenConns = attribute.Key("pkg.db.pool.max_open") // Concurrent package attributes AttrConcurrentTaskCount = attribute.Key("pkg.concurrent.task.count") AttrConcurrentTaskSuccess = attribute.Key("pkg.concurrent.task.success") AttrConcurrentTaskFailed = attribute.Key("pkg.concurrent.task.failed") AttrConcurrentMaxWorkers = attribute.Key("pkg.concurrent.max_workers") )
Semantic conventions specific to github.com/jasoet/pkg/v2 library. These complement standard OpenTelemetry semantic conventions.
const ( // gRPC modes GRPCModeSeparate = "separate" GRPCModeH2C = "h2c" // Database types DBTypePostgreSQL = "postgresql" DBTypeMySQL = "mysql" DBTypeMSSQL = "mssql" )
Common attribute values
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// TracerProvider for distributed tracing
// If nil, tracing will be disabled (no-op tracer)
TracerProvider trace.TracerProvider
// MeterProvider for metrics collection
// If nil, metrics will be disabled (no-op meter)
MeterProvider metric.MeterProvider
// LoggerProvider for structured logging via OTel
// Defaults to stdout exporter when using NewConfig()
// Set to nil explicitly to disable logging
LoggerProvider log.LoggerProvider
// ServiceName identifies the service in telemetry data
ServiceName string
// ServiceVersion identifies the service version
ServiceVersion string
}
Config holds OpenTelemetry configuration for instrumentation. TracerProvider and MeterProvider are optional - nil values result in no-op implementations. LoggerProvider defaults to stdout exporter when using NewConfig().
func NewConfig ¶
NewConfig creates a new OpenTelemetry configuration with default LoggerProvider. The default LoggerProvider uses stdout exporter for easy debugging. Use With* methods to add TracerProvider and MeterProvider.
For production use with better formatting and automatic log-span correlation, use logging.NewLoggerProvider() instead:
import "github.com/jasoet/pkg/v2/logging"
cfg := &otel.Config{
ServiceName: "my-service",
LoggerProvider: logging.NewLoggerProvider("my-service", false),
}
cfg.WithTracerProvider(tp).WithMeterProvider(mp)
Example with default stdout logger:
cfg := otel.NewConfig("my-service").
WithTracerProvider(tp).
WithMeterProvider(mp)
func (*Config) GetLogger ¶
GetLogger returns a logger for the given instrumentation scope. Returns a no-op logger if logging is not configured.
func (*Config) GetMeter ¶
GetMeter returns a meter for the given instrumentation scope. Returns a no-op meter if metrics are not configured.
func (*Config) GetTracer ¶
GetTracer returns a tracer for the given instrumentation scope. Returns a no-op tracer if tracing is not configured.
func (*Config) IsLoggingEnabled ¶
IsLoggingEnabled returns true if OTel logging is configured
func (*Config) IsMetricsEnabled ¶
IsMetricsEnabled returns true if metrics collection is configured
func (*Config) IsTracingEnabled ¶
IsTracingEnabled returns true if tracing is configured
func (*Config) Shutdown ¶
Shutdown gracefully shuts down all configured providers Call this when your application exits to flush any pending telemetry
func (*Config) WithLoggerProvider ¶
func (c *Config) WithLoggerProvider(lp log.LoggerProvider) *Config
WithLoggerProvider sets a custom LoggerProvider, replacing the default stdout logger
func (*Config) WithMeterProvider ¶
func (c *Config) WithMeterProvider(mp metric.MeterProvider) *Config
WithMeterProvider sets the MeterProvider for metrics collection
func (*Config) WithServiceVersion ¶
WithServiceVersion sets the service version for telemetry data
func (*Config) WithTracerProvider ¶
func (c *Config) WithTracerProvider(tp trace.TracerProvider) *Config
WithTracerProvider sets the TracerProvider for distributed tracing
func (*Config) WithoutLogging ¶
WithoutLogging disables the default logging by setting LoggerProvider to nil