2022-03-09 03:18:32 +08:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
2023-03-10 00:02:35 +08:00
|
|
|
"go.opentelemetry.io/contrib/propagators/autoprop"
|
2022-03-09 03:18:32 +08:00
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2023-02-09 01:49:17 +08:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
|
2023-04-27 10:46:41 +08:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-03-09 03:18:32 +08:00
|
|
|
"go.uber.org/zap"
|
2023-08-14 23:41:15 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2022-03-09 03:18:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
webEngineName = "Caddy"
|
|
|
|
defaultSpanName = "handler"
|
|
|
|
nextCallCtxKey caddy.CtxKey = "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 is responsible for the tracing injection, extraction and propagation.
|
|
|
|
type openTelemetryWrapper struct {
|
|
|
|
propagators propagation.TextMapPropagator
|
|
|
|
|
|
|
|
handler http.Handler
|
|
|
|
|
|
|
|
spanName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOpenTelemetryWrapper is responsible for the openTelemetryWrapper initialization using provided configuration.
|
|
|
|
func newOpenTelemetryWrapper(
|
|
|
|
ctx context.Context,
|
|
|
|
spanName string,
|
|
|
|
) (openTelemetryWrapper, error) {
|
|
|
|
if spanName == "" {
|
|
|
|
spanName = defaultSpanName
|
|
|
|
}
|
|
|
|
|
|
|
|
ot := openTelemetryWrapper{
|
|
|
|
spanName: spanName,
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:16:59 +08:00
|
|
|
version, _ := caddy.Version()
|
|
|
|
res, err := ot.newResource(webEngineName, version)
|
2022-03-09 03:18:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return ot, fmt.Errorf("creating resource error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
traceExporter, err := otlptracegrpc.New(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ot, fmt.Errorf("creating trace exporter error: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-10 00:02:35 +08:00
|
|
|
ot.propagators = autoprop.NewTextMapPropagator()
|
2022-03-09 03:18:32 +08:00
|
|
|
|
|
|
|
tracerProvider := globalTracerProvider.getTracerProvider(
|
|
|
|
sdktrace.WithBatcher(traceExporter),
|
|
|
|
sdktrace.WithResource(res),
|
|
|
|
)
|
|
|
|
|
2023-01-25 15:26:44 +08:00
|
|
|
ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP),
|
|
|
|
ot.spanName,
|
|
|
|
otelhttp.WithTracerProvider(tracerProvider),
|
|
|
|
otelhttp.WithPropagators(ot.propagators),
|
|
|
|
otelhttp.WithSpanNameFormatter(ot.spanNameFormatter),
|
|
|
|
)
|
|
|
|
|
2022-03-09 03:18:32 +08:00
|
|
|
return ot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveHTTP injects a tracing context and call the next handler.
|
|
|
|
func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
2023-04-27 10:46:41 +08:00
|
|
|
ctx := r.Context()
|
|
|
|
ot.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header))
|
|
|
|
spanCtx := trace.SpanContextFromContext(ctx)
|
|
|
|
if spanCtx.IsValid() {
|
2024-05-09 06:40:40 +08:00
|
|
|
traceID := spanCtx.TraceID().String()
|
|
|
|
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
|
|
|
|
caddyhttp.SetVar(ctx, "trace_id", traceID)
|
|
|
|
// Add the trace id to the log fields for the request.
|
2023-04-27 10:46:41 +08:00
|
|
|
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
|
2024-05-09 06:40:40 +08:00
|
|
|
extra.Add(zap.String("traceID", traceID))
|
2023-04-27 10:46:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
next := ctx.Value(nextCallCtxKey).(*nextCall)
|
2022-03-09 03:18:32 +08:00
|
|
|
next.err = next.next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP propagates call to the by wrapped by `otelhttp` next handler.
|
|
|
|
func (ot *openTelemetryWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
|
|
|
n := &nextCall{
|
|
|
|
next: next,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
ot.handler.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), nextCallCtxKey, n)))
|
|
|
|
|
|
|
|
return n.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup flush all remaining data and shutdown a tracerProvider
|
|
|
|
func (ot *openTelemetryWrapper) cleanup(logger *zap.Logger) error {
|
|
|
|
return globalTracerProvider.cleanupTracerProvider(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newResource creates a resource that describe current handler instance and merge it with a default attributes value.
|
|
|
|
func (ot *openTelemetryWrapper) newResource(
|
|
|
|
webEngineName,
|
|
|
|
webEngineVersion string,
|
|
|
|
) (*resource.Resource, error) {
|
|
|
|
return resource.Merge(resource.Default(), resource.NewSchemaless(
|
2023-02-09 01:49:17 +08:00
|
|
|
semconv.WebEngineName(webEngineName),
|
|
|
|
semconv.WebEngineVersion(webEngineVersion),
|
2022-03-09 03:18:32 +08:00
|
|
|
))
|
|
|
|
}
|
2023-01-25 15:26:44 +08:00
|
|
|
|
|
|
|
// spanNameFormatter performs the replacement of placeholders in the span name
|
|
|
|
func (ot *openTelemetryWrapper) spanNameFormatter(operation string, r *http.Request) string {
|
|
|
|
return r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer).ReplaceAll(operation, "")
|
|
|
|
}
|