Skip to content
Merged
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
11 changes: 9 additions & 2 deletions cmd/activator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"k8s.io/apimachinery/pkg/util/wait"

// Injection related imports.
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

network "knative.dev/networking/pkg"
netcfg "knative.dev/networking/pkg/config"
netprobe "knative.dev/networking/pkg/http/probe"
Expand Down Expand Up @@ -230,7 +232,7 @@ func main() {
apiconfig.DefaultRevisionIdleTimeoutSeconds * time.Second
})
ah = concurrencyReporter.Handler(ah)
ah = activatorhandler.NewTracingHandler(tp, ah)
ah = activatorhandler.NewTracingAttributeHandler(tp, ah)
reqLogHandler, err := pkghttp.NewRequestLogHandler(ah, logging.NewSyncFileWriter(os.Stdout), "",
requestLogTemplateInputGetter, false /*enableProbeRequestLog*/)
if err != nil {
Expand All @@ -240,11 +242,16 @@ func main() {

// NOTE: MetricHandler is being used as the outermost handler of the meaty bits. We're not interested in measuring
// the healthchecks or probes.
ah = activatorhandler.NewMetricHandler(env.PodName, ah)
ah = activatorhandler.NewMetricAttributeHandler(env.PodName, ah)
// We need the context handler to run first so ctx gets the revision info.
ah = activatorhandler.WrapActivatorHandlerWithFullDuplex(ah, logger)
ah = activatorhandler.NewContextHandler(ctx, ah, configStore)

ah = otelhttp.NewHandler(ah, "handle",
otelhttp.WithTracerProvider(tp),
otelhttp.WithMeterProvider(mp),
)

// Network probe handlers.
ah = &activatorhandler.ProbeHandler{NextHandler: ah}
ah = netprobe.NewHandler(ah)
Expand Down
8 changes: 4 additions & 4 deletions pkg/activator/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func BenchmarkHandlerChain(b *testing.B) {
// Make sure to update this if the activator's main file changes.
ah := New(ctx, fakeThrottler{}, rt, false, logger, false /* TLS */, tp)
ah = concurrencyReporter.Handler(ah)
ah = NewTracingHandler(tp, ah)
ah = NewTracingAttributeHandler(tp, ah)
ah, _ = pkghttp.NewRequestLogHandler(ah, io.Discard, "", nil, false)
ah = NewMetricHandler(activatorPodName, ah)
ah = NewMetricAttributeHandler(activatorPodName, ah)
ah = NewContextHandler(ctx, ah, configStore)
ah = &ProbeHandler{NextHandler: ah}
ah = netprobe.NewHandler(ah)
Expand Down Expand Up @@ -204,9 +204,9 @@ func TestActivatorChainHandlerWithFullDuplex(t *testing.T) {
})
var ah http.Handler
ah = concurrencyReporter.Handler(proxyWithMiddleware)
ah = NewTracingHandler(tp, ah)
ah = NewTracingAttributeHandler(tp, ah)
ah, _ = pkghttp.NewRequestLogHandler(ah, io.Discard, "", nil, false)
ah = NewMetricHandler(activatorPodName, ah)
ah = NewMetricAttributeHandler(activatorPodName, ah)
ah = WrapActivatorHandlerWithFullDuplex(ah, logger)
ah = NewContextHandler(ctx, ah, configStore)
ah = &ProbeHandler{NextHandler: ah}
Expand Down
5 changes: 3 additions & 2 deletions pkg/activator/handler/metric_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"knative.dev/serving/pkg/metrics"
)

// NewMetricHandler creates a handler that collects and reports request metrics.
func NewMetricHandler(podName string, next http.Handler) *MetricHandler {
// NewMetricAttributeHandler creates a handler that adds serving attributes
// to the otelhttp labeler
func NewMetricAttributeHandler(podName string, next http.Handler) *MetricHandler {
return &MetricHandler{
nextHandler: next,
podName: podName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/activator/handler/metric_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestRequestMetricHandler(t *testing.T) {

for _, test := range tests {
t.Run(test.label, func(t *testing.T) {
handler := NewMetricHandler(testPod, test.baseHandler)
handler := NewMetricAttributeHandler(testPod, test.baseHandler)

labeler := &otelhttp.Labeler{}

Expand Down Expand Up @@ -121,7 +121,7 @@ func BenchmarkMetricHandler(b *testing.B) {

reqCtx = otelhttp.ContextWithLabeler(reqCtx, &otelhttp.Labeler{})

handler := NewMetricHandler("benchPod", baseHandler)
handler := NewMetricAttributeHandler("benchPod", baseHandler)

resp := httptest.NewRecorder()
b.Run("sequential", func(b *testing.B) {
Expand Down
10 changes: 4 additions & 6 deletions pkg/activator/handler/tracing_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"go.opentelemetry.io/otel/trace"
)

func NewTracingHandler(tp trace.TracerProvider, next http.Handler) http.Handler {
shim := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// NewTracingAttributeHandler creates a handler that adds serving attributes
// to the span
func NewTracingAttributeHandler(tp trace.TracerProvider, next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
defer func() {
// otelhttp middleware creates the labeler
labeler, _ := otelhttp.LabelerFromContext(r.Context())
Expand All @@ -36,8 +38,4 @@ func NewTracingHandler(tp trace.TracerProvider, next http.Handler) http.Handler

next.ServeHTTP(rw, r)
})

return otelhttp.NewHandler(shim, "activate",
otelhttp.WithTracerProvider(tp),
)
}
5 changes: 3 additions & 2 deletions pkg/activator/handler/tracing_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestTracingHandler(t *testing.T) {
func TestTracingAttributeHandler(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := trace.NewTracerProvider(
trace.WithSyncer(exporter),
Expand All @@ -43,7 +43,8 @@ func TestTracingHandler(t *testing.T) {
labeler, _ := otelhttp.LabelerFromContext(r.Context())
labeler.Add(attribute.Bool("x", true))
})
handler := NewTracingHandler(tp, baseHandler)
handler := NewTracingAttributeHandler(tp, baseHandler)
handler = otelhttp.NewHandler(handler, "op", otelhttp.WithTracerProvider(tp))

resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "http://example.com", nil)
Expand Down
Loading