otel

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 27 Imported by: 5

Documentation

Index

Constants

View Source
const (
	AttrKeyEventName  = attribute.Key("event.name")
	AttrKeyTrackingID = attribute.Key("tracking.id")
)

OpenTelemetry attribute keys.

Variables

View Source
var (
	ErrEmptyServiceName      = errors.New("serviceName cannot be empty")
	ErrTraceExporterNotFound = errors.New("OTEL Trace Exporter not found")
)

Functions

func EventConsumer added in v0.2.0

func EventConsumer() consumer.Middleware

func EventsAddOpenTelemetry

func EventsAddOpenTelemetry(eventName string) events.Middleware

EventsAddOpenTelemetry adds an opentracing span to the context and finishes the span when the handler returns. Use go.opentelemetry.io/otel/trace.SpanFromContext to get the span from the context.

func HTTPInject

func HTTPInject(ctx context.Context, r *http.Request)

HTTPInject injects OTel "cross-cutting concerns" (a.k.a OTel headers) and X-Tracking-Id into the outgoing request headers.

func HTTPMiddleware

func HTTPMiddleware(serviceName, handlerName, path string) func(http.Handler) http.Handler

HTTPMiddleware returns a http.NewHandler and adds HTTP information on the span (similar to the otelhttp.NewHandler middleware) and adds extra information on top. Use go.opentelemetry.io/otel/trace.SpanFromContext to get the span from the context.

func InstrumentTransport

func InstrumentTransport(t http.RoundTripper) http.RoundTripper

InstrumentTransport instruments the given http.RoundTripper with OTel. If the given http.RoundTripper is nil, http.DefaultTransport is used. Requests should be made with the context with the span.

func NewHTTPHandler added in v0.2.0

func NewHTTPHandler(handler http.Handler, operation string) http.Handler

NewHTTPHandler wraps the passed handler in a span named like operation.

func SetUpOTel

func SetUpOTel(serviceName string, log logger.Logger, opts ...Option) error

SetUpOTel perform all necessary initialisations for open telemetry and registers a trace provider. Any call to OTel API before the setup is done, will likely use the default noop implementations.

some values will be infered from env vars:

- OTEL_ENABLED: to activate/deactivate otel, default: true

- OTEL_DEBUG: to activate debug mode, default: false

- OTEL_TRACE_EXPORTER_OTLP_HTTP_ENDPOINT: otlp HTTP trace exporter is activated

- OTEL_TRACE_EXPORTER_OTLP_GRPC_ENDPOINT: otlp GRPC trace exporter is activated

- ENV: is the application environment

func SpanAddErr

func SpanAddErr(span trace.Span, err error)

SpanAddErr adds an error, including stacktrace, to span and sets its status to error.

Types

type Config

type Config struct {
	Enable bool   `json:"enable"`
	Debug  bool   `json:"debug"`
	Env    string `json:"env"`

	ServiceName    string `json:"serviceName"`
	ServiceVersion string `json:"serviceVersion"`
	// contains filtered or unexported fields
}

Config holds the OTel configuration and is edited by Option.

func (Config) String

func (c Config) String() string

String returns a JSON representation of c. If json.Marshal fails, the returned string will be the error.

type Option

type Option func(config *Config)

Option applies a configuration to config.

func WithDebug

func WithDebug() Option

WithDebug enables debug by adding a span processor which prints to stdout.

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment adds env as the environment span attribute.

func WithErrorHandler

func WithErrorHandler(h func(error)) Option

WithErrorHandler registers h as OTel's global error handler. See go.opentelemetry.io/otel.ErrorHandler for more details on the error handler.

func WithGrpcTraceExporter

func WithGrpcTraceExporter(endpoint string) Option

WithGrpcTraceExporter registers an otlp trace exporter.

func WithHttpTraceExporter

func WithHttpTraceExporter(endpoint string) Option

WithHttpTraceExporter registers an otlp http trace exporter.

func WithServiceVersion

func WithServiceVersion(version string) Option

WithServiceVersion adds version as the service version span attribute.

Directories

Path Synopsis
examples module
internal

Jump to

Keyboard shortcuts

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