gotel

package module
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2026 License: Apache-2.0 Imports: 42 Imported by: 23

README

gotel

gotel is a reusable library for setting up OpenTelemetry exporters in Go with configurations.

Documentation

Overview

Package gotel is a reusable library for setting up OpenTelemetry exporters in Go with configurations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLogger

func GetLogger(ctx context.Context) *slog.Logger

GetLogger gets the logger instance from context.

func GetOrCreateLogger added in v0.6.3

func GetOrCreateLogger(ctx context.Context, name string) (*slog.Logger, bool)

GetOrCreateLogger returns an existing logger from the context or creates a new one. The returned bool is true if the logger was found in the context.

func GetRequestLogger added in v0.2.0

func GetRequestLogger(r *http.Request) *slog.Logger

GetRequestLogger get the logger from the an http request.

func NewTracingMiddleware added in v0.2.0

func NewTracingMiddleware(
	exporters *OTelExporters,
	options ...TracingMiddlewareOption,
) func(http.Handler) http.Handler

NewTracingMiddleware creates a middleware with tracing and logger.

Types

type CustomAttributesFunc added in v0.3.0

type CustomAttributesFunc func(r *http.Request) []attribute.KeyValue

CustomAttributesFunc abstracts a hook function to add custom attributes.

type LogHandler

type LogHandler struct {
	// contains filtered or unexported fields
}

LogHandler wraps slog logger with the OpenTelemetry logs exporter handler.

func (LogHandler) Enabled

func (l LogHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (LogHandler) Handle

func (l LogHandler) Handle(ctx context.Context, record slog.Record) error

Handle handles the Record. It will only be called when Enabled returns true.

func (LogHandler) WithAttrs

func (l LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.

func (LogHandler) WithGroup

func (l LogHandler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given group appended to the receiver's existing groups.

type NewWrapResponseWriterFunc added in v0.2.0

type NewWrapResponseWriterFunc func(w http.ResponseWriter, protoMajor int) WrapResponseWriter

NewWrapResponseWriterFunc abstracts an interface for the http.ResponseWriter wrapper constructor.

type OTELLogsExporterType added in v0.2.0

type OTELLogsExporterType string

OTELLogsExporterType defines the type of OpenTelemetry logs exporter.

const (
	// OTELLogsExporterNone represents an enum that disables the logs exporter.
	OTELLogsExporterNone OTELLogsExporterType = "none"
	// OTELLogsExporterOTLP represents an enum that enables the logs exporter via OTLP protocol.
	OTELLogsExporterOTLP OTELLogsExporterType = "otlp"
)

type OTELMetricsExporterType

type OTELMetricsExporterType string

OTELMetricsExporterType defines the type of OpenTelemetry metrics exporter.

const (
	// OTELMetricsExporterNone represents an enum that disables the metrics exporter.
	OTELMetricsExporterNone OTELMetricsExporterType = "none"
	// OTELMetricsExporterOTLP represents an enum that enables the metrics exporter via OTLP protocol.
	OTELMetricsExporterOTLP OTELMetricsExporterType = "otlp"
	// OTELMetricsExporterPrometheus represents an enum that enables the metrics exporter via Prometheus.
	OTELMetricsExporterPrometheus OTELMetricsExporterType = "prometheus"
)

type OTLPCompressionType

type OTLPCompressionType string

OTLPCompressionType represents the compression type enum for OTLP.

const (
	// OTLPCompressionNone is the enum that disables compression.
	OTLPCompressionNone OTLPCompressionType = "none"
	// OTLPCompressionGzip is the enum that enables the gzip compression algorithm.
	OTLPCompressionGzip OTLPCompressionType = "gzip"
)

type OTLPConfig

type OTLPConfig struct {
	// OpenTelemetry service name.
	ServiceName string `json:"serviceName,omitempty" yaml:"serviceName,omitempty" env:"OTEL_SERVICE_NAME" help:"OpenTelemetry service name."`
	// OTLP receiver endpoint that is set as default for all types.
	OtlpEndpoint string `` /* 161-byte string literal not displayed */
	// OTLP receiver endpoint for traces exporter.
	OtlpTracesEndpoint string `` /* 154-byte string literal not displayed */
	// OTLP receiver endpoint for metrics exporter.
	OtlpMetricsEndpoint string `` /* 158-byte string literal not displayed */
	// OTLP receiver endpoint for logs exporter.
	OtlpLogsEndpoint string `` /* 146-byte string literal not displayed */
	// Disable TLS for OpenTelemetry exporters.
	OtlpInsecure *bool `` /* 141-byte string literal not displayed */
	// Disable TLS for OpenTelemetry traces exporter.
	OtlpTracesInsecure *bool `` /* 166-byte string literal not displayed */
	// Disable TLS for OpenTelemetry metrics exporter.
	OtlpMetricsInsecure *bool `` /* 170-byte string literal not displayed */
	// Disable TLS for OpenTelemetry logs exporter.
	OtlpLogsInsecure *bool `` /* 158-byte string literal not displayed */
	// OTLP receiver protocol for all exporters. Default is grpc.
	OtlpProtocol OTLPProtocol `` /* 241-byte string literal not displayed */
	// OTLP receiver protocol for traces.
	OtlpTracesProtocol OTLPProtocol `` /* 234-byte string literal not displayed */
	// OTLP receiver protocol for metrics.
	OtlpMetricsProtocol OTLPProtocol `` /* 238-byte string literal not displayed */
	// OTLP receiver protocol for logs.
	OtlpLogsProtocol OTLPProtocol `` /* 226-byte string literal not displayed */
	// Enable compression for OTLP exporters. Accept: none, gzip
	OtlpCompression OTLPCompressionType `` /* 232-byte string literal not displayed */
	// Enable compression for OTLP traces exporter. Accept: none, gzip
	OtlpTracesCompression OTLPCompressionType `` /* 254-byte string literal not displayed */
	// Enable compression for OTLP metrics exporter. Accept: none, gzip
	OtlpMetricsCompression OTLPCompressionType `` /* 258-byte string literal not displayed */
	// Enable compression for OTLP logs exporter. Accept: none, gzip
	OtlpLogsCompression OTLPCompressionType `` /* 246-byte string literal not displayed */
	// Metrics export type. Accept: none, otlp, prometheus
	MetricsExporter OTELMetricsExporterType `` /* 244-byte string literal not displayed */
	// Logs export type. Accept: none, otlp
	LogsExporter OTELLogsExporterType `` /* 193-byte string literal not displayed */
	// Prometheus port for the Prometheus HTTP server. Use /metrics endpoint of the connector server if empty.
	PrometheusPort *uint `` /* 249-byte string literal not displayed */
	// Disable internal Go and process metrics (prometheus exporter only).
	DisableGoMetrics *bool `json:"disableGoMetrics,omitempty" yaml:"disableGoMetrics,omitempty" help:"Disable internal Go and process metrics"`
}

OTLPConfig contains configuration for OpenTelemetry exporter.

func (OTLPConfig) GetLogsExporter added in v0.4.5

func (oc OTLPConfig) GetLogsExporter() OTELLogsExporterType

GetLogsExporter returns the type of logs exporter. Default is none.

func (OTLPConfig) GetMetricsExporter added in v0.4.5

func (oc OTLPConfig) GetMetricsExporter() OTELMetricsExporterType

GetMetricsExporter returns the type of metrics exporter. Default is none.

func (OTLPConfig) GetOTLPCompression added in v0.4.5

func (oc OTLPConfig) GetOTLPCompression() OTLPCompressionType

GetOTLPCompression returns the OTLP compression type. Default is gzip.

func (OTLPConfig) GetOTLPLogsCompression added in v0.4.5

func (oc OTLPConfig) GetOTLPLogsCompression() OTLPCompressionType

GetOTLPLogsCompression returns the OTLP logs compression type. Default is the otlpCompression value.

func (OTLPConfig) GetOTLPLogsProtocol added in v0.4.5

func (oc OTLPConfig) GetOTLPLogsProtocol() OTLPProtocol

GetOTLPLogsProtocol returns the OTLP protocol for OTEL logs exporter.

func (OTLPConfig) GetOTLPMetricsCompression added in v0.4.5

func (oc OTLPConfig) GetOTLPMetricsCompression() OTLPCompressionType

GetOTLPMetricsCompression returns the OTLP metrics compression type. Default is the otlpCompression value.

func (OTLPConfig) GetOTLPMetricsProtocol added in v0.4.5

func (oc OTLPConfig) GetOTLPMetricsProtocol() OTLPProtocol

GetOTLPMetricsProtocol returns the OTLP protocol for OTEL metrics exporter.

func (OTLPConfig) GetOTLPProtocol added in v0.4.5

func (oc OTLPConfig) GetOTLPProtocol() OTLPProtocol

GetOTLPProtocol returns the OTLP protocol for OpenTelemetry exporters. Default is grpc.

func (OTLPConfig) GetOTLPTracesCompression added in v0.4.5

func (oc OTLPConfig) GetOTLPTracesCompression() OTLPCompressionType

GetOTLPTracesCompression returns the OTLP traces compression type. Default is the otlpCompression value.

func (OTLPConfig) GetOTLPTracesProtocol added in v0.4.5

func (oc OTLPConfig) GetOTLPTracesProtocol() OTLPProtocol

GetOTLPTracesProtocol returns the OTLP protocol for OTEL traces exporter.

type OTLPProtocol

type OTLPProtocol string

OTLPProtocol represents the OTLP protocol enum.

const (
	// OTLPProtocolGRPC represents the gRPC OTLP protocol enum.
	OTLPProtocolGRPC OTLPProtocol = "grpc"
	// OTLPProtocolHTTPProtobuf represents the HTTP Protobuf OTLP protocol enum.
	OTLPProtocolHTTPProtobuf OTLPProtocol = "http/protobuf"
)

type OTelExporters added in v0.4.0

type OTelExporters struct {
	Tracer   *Tracer
	Meter    metricapi.Meter
	Logger   *slog.Logger
	Shutdown func(context.Context) error
}

OTelExporters contains outputs of OpenTelemetry exporters.

func SetupOTelExporters

func SetupOTelExporters(
	ctx context.Context,
	config *OTLPConfig,
	serviceVersion string,
	logger *slog.Logger,
) (*OTelExporters, error)

SetupOTelExporters set up OpenTelemetry exporters from configuration.

type Tracer

type Tracer struct {
	traceapi.Tracer
}

Tracer is the wrapper of traceapi.Tracer with user visibility on Hasura Console.

func NewTracer

func NewTracer(name string, opts ...traceapi.TracerOption) *Tracer

NewTracer creates a new OpenTelemetry tracer.

func (*Tracer) Start

func (t *Tracer) Start(
	ctx context.Context,
	spanName string,
	opts ...traceapi.SpanStartOption,
) (context.Context, traceapi.Span)

Start creates a span and a context.Context containing the newly-created span with `internal.visibility: "user"` so that it shows up in the Hasura Console.

func (*Tracer) StartInternal

func (t *Tracer) StartInternal(
	ctx context.Context,
	spanName string,
	opts ...traceapi.SpanStartOption,
) (context.Context, traceapi.Span)

StartInternal creates a span and a context.Context containing the newly-created span. It won't show up in the Hasura Console.

type TracingMiddlewareOption added in v0.2.0

type TracingMiddlewareOption func(*tracingMiddlewareOptions)

TracingMiddlewareOption abstracts a function to apply options to the tracing middleware.

func ResponseWriterWrapperFunc added in v0.2.0

func ResponseWriterWrapperFunc(wrapper NewWrapResponseWriterFunc) TracingMiddlewareOption

ResponseWriterWrapperFunc return an option to set the response writer wrapper function.

func WithAllowedRequestHeaders added in v0.8.0

func WithAllowedRequestHeaders(names []string) TracingMiddlewareOption

WithAllowedRequestHeaders return an option to set allowed request headers. If empty, all headers are allowed.

func WithAllowedResponseHeaders added in v0.8.0

func WithAllowedResponseHeaders(names []string) TracingMiddlewareOption

WithAllowedResponseHeaders return an option to set allowed response headers. If empty, all headers are allowed.

func WithCustomAttributesFunc added in v0.3.0

func WithCustomAttributesFunc(fn CustomAttributesFunc) TracingMiddlewareOption

WithCustomAttributesFunc set the option to add custom OpenTelemetry attributes.

func WithDebugPaths added in v0.3.0

func WithDebugPaths(paths []string) TracingMiddlewareOption

WithDebugPaths return an option to add request paths to be printed logs in the debug level. By default, metrics and health check endpoints are added to avoid noisy logs.

func WithHighCardinalityMetrics added in v0.3.0

func WithHighCardinalityMetrics(enabled bool) TracingMiddlewareOption

WithHighCardinalityMetrics set the option to enable high cardinality request path labels.

func WithHighCardinalitySpans added in v0.3.0

func WithHighCardinalitySpans(enabled bool) TracingMiddlewareOption

WithHighCardinalitySpans set the option to enable high cardinality spans. The request path is removed from the span name.

func WithSensitivePatterns added in v0.8.0

func WithSensitivePatterns(patterns []string) TracingMiddlewareOption

WithSensitivePatterns set the option to add sensitive patterns to be masked.

type WrapResponseWriter added in v0.2.0

type WrapResponseWriter interface {
	http.ResponseWriter
	// Status returns the HTTP status of the request, or 0 if one has not
	// yet been sent.
	Status() int
	// BytesWritten returns the total number of bytes sent to the client.
	BytesWritten() int
	// Tee causes the response body to be written to the given io.Writer in
	// addition to proxying the writes through. Only one io.Writer can be
	// tee'd to at once: setting a second one will overwrite the first.
	// Writes will be sent to the proxy before being written to this
	// io.Writer. It is illegal for the tee'd writer to be modified
	// concurrently with writes.
	Tee(w io.Writer)
	// Unwrap returns the original proxied target.
	Unwrap() http.ResponseWriter
	// Discard causes all writes to the original ResponseWriter be discarded,
	// instead writing only to the tee'd writer if it's set.
	// The caller is responsible for calling WriteHeader and Write on the
	// original ResponseWriter once the processing is done.
	Discard()
}

WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook into various parts of the response process. The original work was derived from Chi's middleware, source: https://github.com/go-chi/chi/blob/master/middleware/wrap_writer.go

Directories

Path Synopsis
Package otelutils contain reusable utilities for OpenTelemetry attributes.
Package otelutils contain reusable utilities for OpenTelemetry attributes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL