mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-16 09:01:15 -05:00
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.25.0, ubuntu-latest, 0, 1.25, linux) (push) Failing after 16s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.25.0, 1.25, aix) (push) Failing after 14s
Cross-Build / build (~1.25.0, 1.25, darwin) (push) Failing after 13s
Cross-Build / build (~1.25.0, 1.25, dragonfly) (push) Failing after 13s
Cross-Build / build (~1.25.0, 1.25, freebsd) (push) Failing after 13s
Cross-Build / build (~1.25.0, 1.25, illumos) (push) Failing after 14s
Cross-Build / build (~1.25.0, 1.25, linux) (push) Failing after 14s
Cross-Build / build (~1.25.0, 1.25, netbsd) (push) Failing after 14s
Cross-Build / build (~1.25.0, 1.25, openbsd) (push) Failing after 13s
Cross-Build / build (~1.25.0, 1.25, solaris) (push) Failing after 14s
Cross-Build / build (~1.25.0, 1.25, windows) (push) Failing after 14s
Lint / lint (ubuntu-latest, linux) (push) Failing after 13s
Lint / govulncheck (push) Successful in 1m30s
Lint / dependency-review (push) Failing after 14s
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 31s
Tests / test (./cmd/caddy/caddy, ~1.25.0, macos-14, 0, 1.25, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.25.0, windows-latest, True, 1.25, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
* WIP tracing span attributes * better test * only write attributes after other middleware (and request) * Fix test to use header response placeholders
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package tracing
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(Tracing{})
|
|
httpcaddyfile.RegisterHandlerDirective("tracing", parseCaddyfile)
|
|
}
|
|
|
|
// 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"`
|
|
|
|
// SpanAttributes are custom key-value pairs to be added to spans
|
|
SpanAttributes map[string]string `json:"span_attributes,omitempty"`
|
|
|
|
// otel implements opentelemetry related logic.
|
|
otel openTelemetryWrapper
|
|
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// 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) },
|
|
}
|
|
}
|
|
|
|
// 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, ot.SpanAttributes)
|
|
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
|
|
//
|
|
// tracing {
|
|
// [span <span_name>]
|
|
// [span_attributes {
|
|
// attr1 value1
|
|
// attr2 value2
|
|
// }]
|
|
// }
|
|
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) {
|
|
switch d.Val() {
|
|
case "span_attributes":
|
|
if ot.SpanAttributes == nil {
|
|
ot.SpanAttributes = make(map[string]string)
|
|
}
|
|
for d.NextBlock(1) {
|
|
key := d.Val()
|
|
if !d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
value := d.Val()
|
|
if d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
ot.SpanAttributes[key] = value
|
|
}
|
|
default:
|
|
if dst, ok := paramsMap[d.Val()]; ok {
|
|
if err := setParameter(d, dst); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return d.ArgErr()
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
var m Tracing
|
|
err := m.UnmarshalCaddyfile(h.Dispenser)
|
|
return &m, err
|
|
}
|
|
|
|
// Interface guards
|
|
var (
|
|
_ caddy.Provisioner = (*Tracing)(nil)
|
|
_ caddyhttp.MiddlewareHandler = (*Tracing)(nil)
|
|
_ caddyfile.Unmarshaler = (*Tracing)(nil)
|
|
)
|