Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
}
}
}
}
}
2 changes: 2 additions & 0 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sync"
"time"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/http/httpguts"
Expand Down Expand Up @@ -256,6 +257,7 @@ func (h *Handler) Provision(ctx caddy.Context) error {
if module, ok := h.Transport.(caddy.Module); ok && module.CaddyModule().ID.Name() == "fastcgi" && h.RequestBuffers == 0 {
h.RequestBuffers = 4096
}
h.Transport = otelhttp.NewTransport(h.Transport)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually broke the instrumentation in my example, because it now only works if h.TransportRaw != nil which isn't the case when using the default transport.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we reliably instrument all transports, we can stop mutating the inbound headers using ot.propagators.Inject in tracing.go as the transport will inject into outbound requests. - However as per my understanding leaving it will probably not hurt either.

}
if h.LoadBalancing != nil && h.LoadBalancing.SelectionPolicyRaw != nil {
mod, err := ctx.LoadModule(h.LoadBalancing, "SelectionPolicyRaw")
Expand Down
23 changes: 16 additions & 7 deletions modules/caddyhttp/tracing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func init() {
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"`
SpanName string `json:"span"`
InjectServerTimingHeader bool `json:"server_timing,omitempty"`

// otel implements opentelemetry related logic.
otel openTelemetryWrapper
Expand All @@ -46,7 +47,7 @@ func (ot *Tracing) Provision(ctx caddy.Context) error {
ot.logger = ctx.Logger()

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

return err
}
Expand All @@ -68,6 +69,7 @@ func (ot *Tracing) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
//
// tracing {
// [server_timing]
// [span <span_name>]
// }
func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
Expand All @@ -94,12 +96,19 @@ func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}

for d.NextBlock(0) {
if dst, ok := paramsMap[d.Val()]; ok {
if err := setParameter(d, dst); err != nil {
return err
switch d.Val() {
case "server_timing":
if d.NextArg() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading up a bit on testing the feature myself, I think this condition isn't right. With this if the documented Caddyfile syntax doesn't work, because no argument is present after server_timing. The header is currently only set if there is an arbitrary garbage arg.

ot.InjectServerTimingHeader = true
}
default:
if dst, ok := paramsMap[d.Val()]; ok {
if err := setParameter(d, dst); err != nil {
return err
}
} else {
return d.ArgErr()
}
} else {
return d.ArgErr()
}
}
return nil
Expand Down
21 changes: 14 additions & 7 deletions modules/caddyhttp/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/contrib/propagators/autoprop"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -37,20 +38,19 @@ type openTelemetryWrapper struct {

handler http.Handler

spanName string
spanName string
injectServerTimingHeader bool
}

// newOpenTelemetryWrapper is responsible for the openTelemetryWrapper initialization using provided configuration.
func newOpenTelemetryWrapper(
ctx context.Context,
spanName string,
) (openTelemetryWrapper, error) {
func newOpenTelemetryWrapper(ctx context.Context, spanName string, injectServerTimingHeader bool) (openTelemetryWrapper, error) {
if spanName == "" {
spanName = defaultSpanName
}

ot := openTelemetryWrapper{
spanName: spanName,
injectServerTimingHeader: injectServerTimingHeader,
spanName: spanName,
}

version, _ := caddy.Version()
Expand All @@ -64,7 +64,9 @@ func newOpenTelemetryWrapper(
return ot, fmt.Errorf("creating trace exporter error: %w", err)
}

ot.propagators = autoprop.NewTextMapPropagator()
prop := autoprop.NewTextMapPropagator()
otel.SetTextMapPropagator(prop)
ot.propagators = prop

tracerProvider := globalTracerProvider.getTracerProvider(
sdktrace.WithBatcher(traceExporter),
Expand Down Expand Up @@ -98,6 +100,11 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
extra.Add(zap.String("traceID", traceID))
extra.Add(zap.String("spanID", spanID))
}

// Add the server-timing header so clients can make the connection
if ot.injectServerTimingHeader {
w.Header().Set("server-timing", fmt.Sprintf("traceparent;desc=\"00-%s-%s-%s\"", traceID, spanID, spanCtx.TraceFlags().String()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be Add instead of Set. Otherwise, we'll clobber unrelated metrics in server-timings.

}
}
next := ctx.Value(nextCallCtxKey).(*nextCall)
next.err = next.next.ServeHTTP(w, r)
Expand Down
4 changes: 1 addition & 3 deletions modules/caddyhttp/tracing/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ func TestOpenTelemetryWrapper_newOpenTelemetryWrapper(t *testing.T) {
var otw openTelemetryWrapper
var err error

if otw, err = newOpenTelemetryWrapper(ctx,
"",
); err != nil {
if otw, err = newOpenTelemetryWrapper(ctx, "", false); err != nil {
t.Errorf("newOpenTelemetryWrapper() error = %v", err)
t.FailNow()
}
Expand Down
Loading