mirror of
https://github.com/caddyserver/caddy.git
synced 2025-12-08 06:09:53 +00:00
WIP tracing span attributes
This commit is contained in:
parent
39ace450de
commit
10404e6699
5 changed files with 240 additions and 13 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/contrib/propagators/autoprop"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
|
|
@ -37,20 +38,23 @@ type openTelemetryWrapper struct {
|
|||
|
||||
handler http.Handler
|
||||
|
||||
spanName string
|
||||
spanName string
|
||||
spanAttributes map[string]string
|
||||
}
|
||||
|
||||
// newOpenTelemetryWrapper is responsible for the openTelemetryWrapper initialization using provided configuration.
|
||||
func newOpenTelemetryWrapper(
|
||||
ctx context.Context,
|
||||
spanName string,
|
||||
spanAttributes map[string]string,
|
||||
) (openTelemetryWrapper, error) {
|
||||
if spanName == "" {
|
||||
spanName = defaultSpanName
|
||||
}
|
||||
|
||||
ot := openTelemetryWrapper{
|
||||
spanName: spanName,
|
||||
spanName: spanName,
|
||||
spanAttributes: spanAttributes,
|
||||
}
|
||||
|
||||
version, _ := caddy.Version()
|
||||
|
|
@ -99,6 +103,20 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
|
|||
extra.Add(zap.String("spanID", spanID))
|
||||
}
|
||||
}
|
||||
|
||||
// Add custom span attributes to the current span
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if span.IsRecording() && len(ot.spanAttributes) > 0 {
|
||||
replacer := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
attributes := make([]attribute.KeyValue, 0, len(ot.spanAttributes))
|
||||
for key, value := range ot.spanAttributes {
|
||||
// Allow placeholder replacement in attribute values
|
||||
replacedValue := replacer.ReplaceAll(value, "")
|
||||
attributes = append(attributes, attribute.String(key, replacedValue))
|
||||
}
|
||||
span.SetAttributes(attributes...)
|
||||
}
|
||||
|
||||
next := ctx.Value(nextCallCtxKey).(*nextCall)
|
||||
next.err = next.next.ServeHTTP(w, r)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue