Documentation
ยถ
Overview ยถ
Package xyliumotel provides the OpenTelemetry connector for the Xylium framework. This file contains the OpenTelemetry middleware for instrumenting HTTP requests.
Package xyliumotel provides the OpenTelemetry (OTel) connector for the Xylium framework. It simplifies the integration of distributed tracing into Xylium applications by managing the OTel SDK setup (TracerProvider, Exporter, Sampler, Propagator) and providing middleware for automatic HTTP request instrumentation.
Index ยถ
- type Config
- type Connector
- func (c *Connector) Close() error
- func (c *Connector) GetTracer(instrumentationName string, opts ...trace.TracerOption) trace.Tracer
- func (c *Connector) IsNoOp() bool
- func (connector *Connector) OtelMiddleware(mwCustomCfg ...MiddlewareConfig) xylium.Middleware
- func (c *Connector) Propagator() propagation.TextMapPropagator
- type ExporterType
- type MiddlewareConfig
- type OTLPConfig
Constants ยถ
This section is empty.
Variables ยถ
This section is empty.
Functions ยถ
This section is empty.
Types ยถ
type Config ยถ
type Config struct {
// AppLogger is the Xylium application logger instance used by the connector for its own logging.
// This field is required.
AppLogger xylium.Logger
// ServiceName is the logical name of the service, e.g., "user-service".
// Required if not providing ExternalTracerProvider or ExternalSDKTracerProvider.
// Used to create the OTel resource.
ServiceName string
// ServiceVersion is the version of the service, e.g., "v1.2.3". Optional.
ServiceVersion string
// Environment is the deployment environment, e.g., "production", "staging". Optional.
Environment string
// Exporter defines the type of trace exporter to initialize if an internal
// TracerProvider is being created.
// Defaults to ExporterStdout if Xylium mode is Debug/Test, or ExporterNone if Release,
// unless an external provider is specified.
Exporter ExporterType
// OTLP holds configuration for the OTLP gRPC exporter if Exporter is ExporterOTLPGRPC.
OTLP OTLPConfig
// ExternalTracerProvider allows providing a pre-configured trace.TracerProvider.
// If set, the connector will use this provider and will not manage its lifecycle
// (e.g., it won't call Shutdown on it). Internal exporter configuration is ignored.
// If ManageGlobalProviders is true, this provider will be set as the global OTel provider.
ExternalTracerProvider trace.TracerProvider
// ExternalSDKTracerProvider allows providing a pre-configured *sdktrace.TracerProvider.
// Similar to ExternalTracerProvider but for the SDK-specific type. Takes precedence over ExternalTracerProvider.
// If ManageGlobalProviders is true, this provider will be set as the global OTel provider.
ExternalSDKTracerProvider *sdktrace.TracerProvider
// ManageGlobalProviders determines if this connector should manage (set) the global
// OTel TracerProvider and TextMapPropagator using otel.SetTracerProvider and otel.SetTextMapPropagator.
// If false, the application is responsible for setting global providers if needed.
// The connector will then use its internally configured/provided tracer and propagator
// instances for its operations (e.g., middleware).
// Defaults to true.
ManageGlobalProviders *bool // Pointer to distinguish between not set (use default true) and explicitly false.
// Propagator is the OpenTelemetry TextMapPropagator to use.
// If nil, a default composite propagator (TraceContext and Baggage) is used.
// If ManageGlobalProviders is true, this propagator will be set as the global OTel propagator.
Propagator propagation.TextMapPropagator
// Sampler defines the sampling strategy for traces.
// If nil, ParentBased(AlwaysSample()) is used as a default.
Sampler sdktrace.Sampler
// ShutdownTimeout is the duration to wait for the managed TracerProvider to shut down gracefully.
// Defaults to 5 seconds. Only applicable if the connector manages the TracerProvider lifecycle.
ShutdownTimeout time.Duration
// Disabled, if true, completely disables OpenTelemetry integration by this connector.
// The connector will operate in a no-op mode.
Disabled bool
}
Config holds all configuration options for initializing the OpenTelemetry Connector.
type Connector ยถ
type Connector struct {
// contains filtered or unexported fields
}
Connector is the Xylium-aware wrapper for OpenTelemetry functionality. It manages the TracerProvider, Propagator, and provides middleware for instrumentation.
func New ยถ
New creates and initializes a new OpenTelemetry Connector instance based on the provided configuration. It sets up the TracerProvider (either by using an external one or initializing one internally based on exporter settings), configures the Propagator and Sampler, and potentially registers these with the global OpenTelemetry instance if `ManageGlobalProviders` is true. Returns the configured Connector or an error if initialization fails.
func (*Connector) Close ยถ
Close shuts down the internally managed TracerProvider, if one was created by this connector. It respects the Config.ShutdownTimeout. If an external TracerProvider was used, this method is a no-op for the provider's lifecycle. Implements io.Closer, allowing Xylium to manage its lifecycle during graceful shutdown when the connector instance is stored using `app.AppSet()`.
func (*Connector) GetTracer ยถ
GetTracer returns a trace.Tracer instance. If ManageGlobalProviders is false and an internal TracerProvider was initialized, it returns a tracer from that internal provider. Otherwise, it returns a tracer from the (potentially globally set) OTel TracerProvider. `instrumentationName` is the name of the library or component creating spans. `opts` are optional `trace.TracerOption`s.
func (*Connector) IsNoOp ยถ
IsNoOp returns true if the connector is configured to be a no-operation instance (e.g., due to Config.Disabled being true or inability to initialize a TracerProvider). Middleware and other operations will effectively be pass-throughs if IsNoOp is true.
func (*Connector) OtelMiddleware ยถ
func (connector *Connector) OtelMiddleware(mwCustomCfg ...MiddlewareConfig) xylium.Middleware
OtelMiddleware returns a Xylium middleware function for OpenTelemetry HTTP server instrumentation. This method is called on an initialized xyliumotel.Connector instance. It can optionally take a MiddlewareConfig to customize its behavior. If no config is provided, sensible defaults are used for span naming and filtering.
The middleware performs the following:
- Extracts trace context from incoming request headers using the Connector's Propagator.
- Starts a new server span for the request, linking it to an existing trace if context was propagated.
- Sets standard OpenTelemetry semantic attributes for HTTP servers on the span.
- Injects the `trace_id` and `span_id` of the active span into the `xylium.Context` store.
- Propagates the Go `context.Context` (enriched with the active span) to subsequent handlers.
- Records errors from the handler chain on the span and sets the span status accordingly.
- Sets the HTTP response status code as a span attribute.
func (*Connector) Propagator ยถ
func (c *Connector) Propagator() propagation.TextMapPropagator
Propagator returns the configured TextMapPropagator. If ManageGlobalProviders is false, it returns the propagator instance held by the connector. Otherwise, it returns the global OTel propagator.
type ExporterType ยถ
type ExporterType string
ExporterType defines the type of OpenTelemetry trace exporter to configure.
const ( // ExporterOTLPGRPC configures the OTLP (OpenTelemetry Protocol) gRPC exporter. // Requires OTLPConfig.Endpoint to be set. ExporterOTLPGRPC ExporterType = "otlp_grpc" // ExporterStdout configures an exporter that writes traces to standard output. // Useful for local development and debugging. ExporterStdout ExporterType = "stdout" // ExporterNone indicates that no exporter should be configured by this connector. // Tracing will be effectively disabled unless an external TracerProvider is used, // or if ManageGlobalProviders is false and a global provider is set elsewhere. ExporterNone ExporterType = "none" )
type MiddlewareConfig ยถ
type MiddlewareConfig struct {
// TracerName is the name of the tracer that will be specifically used by this middleware
// to create server spans. If empty, a default name like "xylium.otel.middleware" is used.
// This can be different from the connector's main tracer if desired.
TracerName string
// SpanNameFormatter is a function to customize the name of the server span created for each request.
// The xylium.Context of the current request is passed to this function.
// For best observability and lower cardinality in tracing backends, it is highly recommended
// to name spans using the HTTP method and the matched route pattern (e.g., "GET /users/:id").
// If nil, a default formatter is used which typically combines c.Method() and c.Path().
// Example: `func(c *xylium.Context) string { return c.Method() + " " + c.MatchedRoutePattern() }` (if available)
SpanNameFormatter func(c *xylium.Context) string
// AdditionalAttributes allows adding a list of custom key-value attributes
// to every server span created by this specific middleware instance.
// These are added in addition to attributes from the global Connector config.
AdditionalAttributes []attribute.KeyValue
// Filter is an optional function to conditionally skip tracing for some requests.
// If Filter returns true for a given xylium.Context, tracing is bypassed for that request.
// Useful for excluding health checks, metrics endpoints, etc.
Filter func(c *xylium.Context) bool
}
MiddlewareConfig holds configuration options specific to the OpenTelemetry middleware's operation. These settings allow customization of how server spans are named, what additional attributes are added, and which requests might be filtered out from tracing.
type OTLPConfig ยถ
type OTLPConfig struct {
// Endpoint is the target URL for the OTLP gRPC exporter (e.g., "localhost:4317").
Endpoint string
// Insecure determines whether to use an insecure gRPC connection (e.g., for local testing).
// Defaults to false (secure connection) if not specified and Endpoint is set.
Insecure bool
// Headers is a map of additional headers to send with OTLP gRPC requests.
Headers map[string]string
// Timeout for OTLP gRPC export operations.
// Defaults to 10 seconds if not set.
Timeout time.Duration
}
OTLPConfig holds configuration specific to the OTLP exporter.
Directories
ยถ
| Path | Synopsis |
|---|---|
|
Package main provides a runnable example demonstrating the use of the xylium-otel connector with the Xylium core framework for distributed tracing.
|
Package main provides a runnable example demonstrating the use of the xylium-otel connector with the Xylium core framework for distributed tracing. |