Go API Documentation

github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing

No package summary is available.

Package

Files: 3. Third party imports: 10. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const (
	webEngineName			= "Caddy"
	defaultSpanName			= "handler"
	nextCallCtxKey	caddy.CtxKey	= "nextCall"
)

Vars

var (
	_	caddy.Provisioner		= (*Tracing)(nil)
	_	caddyhttp.MiddlewareHandler	= (*Tracing)(nil)
	_	caddyfile.Unmarshaler		= (*Tracing)(nil)
)

globalTracerProvider stores global tracer provider and is responsible for graceful shutdown when nobody is using it.

var globalTracerProvider = &tracerProvider{}

Types

Tracing

Tracing implements an HTTP handler that adds support for distributed tracing, using OpenTelemetry. This module is responsible for the injection and propagation of the trace context. Configure this module via environment variables (see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md). Some values can be overwritten in the configuration file.

type Tracing struct {
	// SpanName is a span name. It should follow the naming guidelines here:
	// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#span
	SpanName	string	`json:"span"`

	// otel implements opentelemetry related logic.
	otel	openTelemetryWrapper

	logger	*zap.Logger
}

nextCall

nextCall store the next handler, and the error value return on calling it (if any)

type nextCall struct {
	next	caddyhttp.Handler
	err	error
}

openTelemetryWrapper

openTelemetryWrapper is responsible for the tracing injection, extraction and propagation.

type openTelemetryWrapper struct {
	propagators	propagation.TextMapPropagator

	handler	http.Handler

	spanName	string
}

tracerProvider

This type doesn't have documentation.

type tracerProvider struct {
	mu			sync.Mutex
	tracerProvider		*sdktrace.TracerProvider
	tracerProvidersCounter	int
}

Functions

func (*Tracing) Cleanup

Cleanup implements caddy.CleanerUpper and closes any idle connections. It calls Shutdown method for a trace provider https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#shutdown.

func (ot *Tracing) Cleanup() error {
	if err := ot.otel.cleanup(ot.logger); err != nil {
		return fmt.Errorf("tracerProvider shutdown: %w", err)
	}
	return nil
}

Cognitive complexity: 2, Cyclomatic complexity: 2

Uses: fmt.Errorf.

func (*Tracing) Provision

Provision implements caddy.Provisioner.

func (ot *Tracing) Provision(ctx caddy.Context) error {
	ot.logger = ctx.Logger()

	var err error
	ot.otel, err = newOpenTelemetryWrapper(ctx, ot.SpanName)

	return err
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Tracing) ServeHTTP

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (ot *Tracing) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
	return ot.otel.ServeHTTP(w, r, next)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Tracing) UnmarshalCaddyfile

UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:

tracing {
    [span <span_name>]
}

func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
	setParameter := func(d *caddyfile.Dispenser, val *string) error {
		if d.NextArg() {
			*val = d.Val()
		} else {
			return d.ArgErr()
		}
		if d.NextArg() {
			return d.ArgErr()
		}
		return nil
	}

	// paramsMap is a mapping between "string" parameter from the Caddyfile and its destination within the module
	paramsMap := map[string]*string{
		"span": &ot.SpanName,
	}

	d.Next()	// consume directive name
	if d.NextArg() {
		return d.ArgErr()
	}

	for d.NextBlock(0) {
		if dst, ok := paramsMap[d.Val()]; ok {
			if err := setParameter(d, dst); err != nil {
				return err
			}
		} else {
			return d.ArgErr()
		}
	}
	return nil
}

Cognitive complexity: 18, Cyclomatic complexity: 7

Uses: caddyfile.Dispenser.

func (Tracing) CaddyModule

CaddyModule returns the Caddy module information.

func (Tracing) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		ID:	"http.handlers.tracing",
		New:	func() caddy.Module { return new(Tracing) },
	}
}

Cognitive complexity: 2, Cyclomatic complexity: 1

Private functions

func init

init ()
References: httpcaddyfile.RegisterHandlerDirective.

func newOpenTelemetryWrapper

newOpenTelemetryWrapper is responsible for the openTelemetryWrapper initialization using provided configuration.

newOpenTelemetryWrapper (ctx context.Context, spanName string) (openTelemetryWrapper, error)
References: autoprop.NewTextMapPropagator, fmt.Errorf, http.HandlerFunc, otelhttp.NewHandler, otelhttp.WithPropagators, otelhttp.WithSpanNameFormatter, otelhttp.WithTracerProvider, otlptracegrpc.New, sdktrace.WithBatcher, sdktrace.WithResource.

func parseCaddyfile

parseCaddyfile (h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)

func cleanup

cleanup flush all remaining data and shutdown a tracerProvider

cleanup (logger *zap.Logger) error

func newResource

newResource creates a resource that describe current handler instance and merge it with a default attributes value.

newResource (webEngineName,webEngineVersion string) (*resource.Resource, error)
References: resource.Default, resource.Merge, resource.NewSchemaless, semconv.WebEngineName, semconv.WebEngineVersion.

func serveHTTP

serveHTTP injects a tracing context and call the next handler.

serveHTTP (w http.ResponseWriter, r *http.Request)
References: caddyhttp.ExtraLogFields, caddyhttp.ExtraLogFieldsCtxKey, caddyhttp.SetVar, propagation.HeaderCarrier, trace.SpanContextFromContext, zap.String.

func spanNameFormatter

spanNameFormatter performs the replacement of placeholders in the span name

spanNameFormatter (operation string, r *http.Request) string

func cleanupTracerProvider

cleanupTracerProvider gracefully shutdown a TracerProvider

cleanupTracerProvider (logger *zap.Logger) error
References: context.Background, fmt.Errorf, zap.Error, zapcore.ErrorLevel.

func getTracerProvider

getTracerProvider create or return an existing global TracerProvider

getTracerProvider (opts ...sdktrace.TracerProviderOption) *sdktrace.TracerProvider
References: sdktrace.NewTracerProvider.


Tests

Files: 3. Third party imports: 1. Imports from organisation: 0. Tests: 8. Benchmarks: 0.

Test functions

TestOpenTelemetryWrapper_newOpenTelemetryWrapper

References: context.Background.

TestTracing_ServeHTTP_Next_Error

References: caddyhttp.HandlerFunc, context.Background, errors.Is, errors.New, http.Request, http.ResponseWriter, httptest.NewRecorder.

TestTracing_ServeHTTP_Propagation_With_Initial_Headers

References: caddyhttp.HandlerFunc, context.Background, http.Request, http.ResponseWriter, httptest.NewRecorder, strings.HasPrefix.

TestTracing_ServeHTTP_Propagation_Without_Initial_Headers

References: caddyhttp.HandlerFunc, context.Background, http.Request, http.ResponseWriter, httptest.NewRecorder, strings.HasPrefix.

TestTracing_UnmarshalCaddyfile

References: caddyfile.Dispenser, caddyfile.NewTestDispenser, testing.T.

TestTracing_UnmarshalCaddyfile_Error

References: caddyfile.Dispenser, caddyfile.NewTestDispenser, testing.T.

Test_tracersProvider_cleanupTracerProvider

References: zap.NewNop.

Test_tracersProvider_getTracerProvider