Documentation
¶
Overview ¶
Package shotel bridges capitan event coordination with OpenTelemetry observability.
Shotel observes all capitan events and transforms them into OTEL logs automatically, while exposing standard OTEL Logger, Meter, and Tracer interfaces for direct use.
OTEL provider configuration is handled externally - use the providers package for common setups, or construct your own providers for full control.
Index ¶
- func RegisterTransformer[T any](variant capitan.Variant, fn TransformerFunc[T])
- func UnregisterTransformer(variant capitan.Variant)
- type Config
- type ContextExtractionConfig
- type ContextKey
- type LogConfig
- type MetricConfig
- type MetricType
- type Providers
- type Shotel
- type TraceConfig
- type TransformerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterTransformer ¶ added in v0.0.11
func RegisterTransformer[T any](variant capitan.Variant, fn TransformerFunc[T])
RegisterTransformer registers a custom transformer for a specific variant.
The transformer receives the field key and typed value, returning OTEL log attributes. Type assertion is handled automatically - your function receives the concrete type.
Example:
type OrderInfo struct {
ID string
Total float64
Secret string // Not logged
}
orderVariant := capitan.Variant("myapp.OrderInfo")
shotel.RegisterTransformer(orderVariant, func(key string, order OrderInfo) []log.KeyValue {
return []log.KeyValue{
log.String(key+".id", order.ID),
log.Float64(key+".total", order.Total),
// Secret field omitted
}
})
func UnregisterTransformer ¶ added in v0.0.11
UnregisterTransformer removes a custom transformer for a specific variant.
Types ¶
type Config ¶
type Config struct {
// Metrics specifies which signals should be auto-converted to OTEL counters.
Metrics []MetricConfig
// Logs configures which signals should be logged.
// If nil or empty, all signals are logged (default behavior).
Logs *LogConfig
// Traces configures signal pairs that should be correlated into spans.
Traces []TraceConfig
// ContextExtraction specifies context keys to extract and add to OTEL signals.
// If nil, no context extraction is performed.
ContextExtraction *ContextExtractionConfig
// StdoutLogging enables duplication of OTEL output to stdout.
// When true, all OTEL signals are logged to stdout in human-readable format using slog.
StdoutLogging bool
}
Config configures how capitan events are transformed to OTEL signals.
type ContextExtractionConfig ¶ added in v0.0.12
type ContextExtractionConfig struct {
// Logs specifies context keys to extract and add to log attributes.
Logs []ContextKey
// Metrics specifies context keys to extract and add to metric dimensions.
// WARNING: High-cardinality values (like unique request IDs) can significantly
// increase metric storage costs. Use only low-cardinality values.
Metrics []ContextKey
// Traces specifies context keys to extract and add to span attributes.
Traces []ContextKey
}
ContextExtractionConfig defines context values to extract for each signal type.
type ContextKey ¶ added in v0.0.12
type ContextKey struct {
// Key is the context key used with context.Value().
// Typically an unexported type to avoid collisions.
Key any
// Name is the attribute name to use in OTEL signals.
Name string
}
ContextKey defines a key-name pair for extracting values from context.Context.
type LogConfig ¶ added in v0.0.11
type LogConfig struct {
// Whitelist specifies which signals should be logged.
// If empty, all signals are logged.
Whitelist []capitan.Signal
}
LogConfig configures log filtering.
type MetricConfig ¶ added in v0.0.11
type MetricConfig struct {
// Signal is the capitan signal to observe.
Signal capitan.Signal
// Name is the OTEL metric name.
// Required - must be a valid OTEL metric name.
Name string
// Type is the metric instrument type.
// Defaults to MetricTypeCounter if not specified.
Type MetricType
// ValueKey is the field key to extract metric value from.
// Required for Gauge, Histogram, and UpDownCounter.
// Not used for Counter (counts signal occurrences).
// Must have a numeric variant (int, int64, float64, etc.).
ValueKey capitan.Key
// Description is optional metric description.
Description string
}
MetricConfig defines a signal-to-metric conversion.
type MetricType ¶ added in v0.0.11
type MetricType string
MetricType specifies the type of OTEL metric instrument.
const ( // MetricTypeCounter increments on each signal occurrence. // Does not use ValueKey - counts signals. MetricTypeCounter MetricType = "counter" // MetricTypeUpDownCounter increments or decrements based on ValueKey. // Requires ValueKey with numeric variant (int64 or float64). MetricTypeUpDownCounter MetricType = "updowncounter" // MetricTypeGauge records instantaneous value from ValueKey. // Requires ValueKey with numeric variant (int64 or float64). MetricTypeGauge MetricType = "gauge" // MetricTypeHistogram records value distribution from ValueKey. // Requires ValueKey with numeric variant (int64 or float64). MetricTypeHistogram MetricType = "histogram" )
type Providers ¶ added in v0.0.11
type Providers struct {
Log *log.LoggerProvider
Meter *metric.MeterProvider
Trace *trace.TracerProvider
}
Providers holds configured OTEL providers for logs, metrics, and traces.
func DefaultProviders ¶ added in v0.0.11
func DefaultProviders( ctx context.Context, serviceName string, serviceVersion string, otlpEndpoint string, ) (*Providers, error)
DefaultProviders creates OTLP providers with opinionated defaults.
Configuration:
- OTLP HTTP exporters for all signals
- Insecure connection (for local development)
- Batch processing for logs and traces
- Periodic reader (60s) for metrics
- Always-sample strategy for traces
Example:
providers, err := shotel.DefaultProviders(ctx, "my-service", "v1.0.0", "localhost:4318")
if err != nil {
log.Fatal(err)
}
defer providers.Shutdown(ctx)
sh, err := shotel.New(capitan.Default(), providers.Log, providers.Meter, providers.Trace, nil)
if err != nil {
log.Fatal(err)
}
defer sh.Close()
type Shotel ¶
type Shotel struct {
// contains filtered or unexported fields
}
Shotel bridges capitan events to OTEL providers.
func New ¶
func New( c *capitan.Capitan, logProvider log.LoggerProvider, meterProvider metric.MeterProvider, traceProvider trace.TracerProvider, config *Config, ) (*Shotel, error)
New creates a Shotel instance that observes capitan events and forwards them to OTEL.
Shotel automatically transforms capitan events based on the provided configuration. If no config is provided, all events are logged (backward compatible).
Parameters:
- c: Capitan instance to observe (required)
- logProvider: OTEL LoggerProvider (required)
- meterProvider: OTEL MeterProvider (required)
- traceProvider: OTEL TracerProvider (required)
- config: Optional configuration (pass nil for defaults)
Example with providers package:
providers, err := providers.Default(ctx, "my-service", "v1.0.0", "localhost:4318")
if err != nil {
log.Fatal(err)
}
defer providers.Shutdown(ctx)
sh := shotel.New(capitan.Default(), providers.Log, providers.Meter, providers.Trace, nil)
defer sh.Close()
Example with configuration:
config := &shotel.Config{
Metrics: []shotel.MetricConfig{
{Signal: orderCreated, Name: "orders_created_total"},
},
Logs: &shotel.LogConfig{Whitelist: []capitan.Signal{orderCreated}},
}
sh := shotel.New(capitan.Default(), providers.Log, providers.Meter, providers.Trace, config)
func (*Shotel) Close ¶ added in v0.0.11
func (s *Shotel) Close()
Close stops observing capitan events.
Note: This does NOT shutdown the OTEL providers - that is the caller's responsibility. If using the providers package, call providers.Shutdown(ctx) separately.
func (*Shotel) Logger ¶ added in v0.0.11
Logger returns an OTEL logger for the given scope name.
The scope name typically represents the package or component emitting logs.
type TraceConfig ¶ added in v0.0.11
type TraceConfig struct {
// Start is the signal that begins the span.
Start capitan.Signal
// End is the signal that completes the span.
End capitan.Signal
// CorrelationKey is the field key used to correlate start/end events.
// Both start and end events must have this field with matching values.
CorrelationKey *capitan.StringKey
// SpanName is the name of the generated span.
// If empty, uses the Start signal as the span name.
SpanName string
// SpanTimeout is the maximum duration to wait for an end event.
// If the end event doesn't arrive within this timeout, the span is
// automatically ended and cleaned up to prevent memory leaks.
// Defaults to 5 minutes if not specified or zero.
SpanTimeout time.Duration
}
TraceConfig defines a signal pair that forms a trace span.