Skip to content

Commit

Permalink
Update net/http instr span name
Browse files Browse the repository at this point in the history
Only use the method name because the path is likely going to have a high
cardinality.
  • Loading branch information
MrAlias committed May 16, 2023
1 parent b29eef6 commit 7b485e6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
15 changes: 10 additions & 5 deletions pkg/instrumentors/bpf/net/http/server/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"

"go.opentelemetry.io/auto/pkg/instrumentors/bpffs"
Expand All @@ -40,6 +39,8 @@ import (

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c

const instrumentedPkg = "net/http"

// Event represents an event in an HTTP server during an HTTP
// request-response.
type Event struct {
Expand All @@ -65,7 +66,7 @@ func New() *Instrumentor {

// LibraryName returns the net/http package name.
func (h *Instrumentor) LibraryName() string {
return "net/http"
return instrumentedPkg
}

// FuncNames returns the function names from "net/http" that are instrumented.
Expand Down Expand Up @@ -192,7 +193,6 @@ func (h *Instrumentor) Run(eventsChan chan<- *events.Event) {
func (h *Instrumentor) convertEvent(e *Event) *events.Event {
method := unix.ByteSliceToString(e.Method[:])
path := unix.ByteSliceToString(e.Path[:])
name := fmt.Sprintf("%s %s", method, path)

sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: e.SpanContext.TraceID,
Expand All @@ -201,8 +201,13 @@ func (h *Instrumentor) convertEvent(e *Event) *events.Event {
})

return &events.Event{
Library: h.LibraryName(),
Name: name,
Library: h.LibraryName(),
// We do not have a include a low-cardinality path here (there is not
// templatized path manifest to reference). Use the default of just the
// request method as the span name. See
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/http/#name
// for more info.
Name: method,
Kind: trace.SpanKindServer,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
Expand Down
66 changes: 66 additions & 0 deletions pkg/instrumentors/bpf/net/http/server/probe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/auto/pkg/instrumentors/context"
"go.opentelemetry.io/auto/pkg/instrumentors/events"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"
)

func TestInstrumentorConvertEvent(t *testing.T) {
start := time.Now()
end := start.Add(1 * time.Second)

traceID := trace.TraceID{1}
spanID := trace.SpanID{1}

i := New()
got := i.convertEvent(&Event{
StartTime: uint64(start.UnixNano()),
EndTime: uint64(end.UnixNano()),
// "GET"
Method: [7]byte{0x47, 0x45, 0x54},
// "/foo/bar"
Path: [100]byte{0x2f, 0x66, 0x6f, 0x6f, 0x2f, 0x62, 0x61, 0x72},
SpanContext: context.EBPFSpanContext{TraceID: traceID, SpanID: spanID},
})

sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
})
want := &events.Event{
Library: instrumentedPkg,
Name: "GET",
Kind: trace.SpanKindServer,
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
SpanContext: &sc,
Attributes: []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPTargetKey.String("/foo/bar"),
},
}
assert.Equal(t, want, got)
}
2 changes: 1 addition & 1 deletion test/e2e/nethttp/traces.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"kind": 2,
"name": "GET /hello",
"name": "GET",
"parentSpanId": "",
"spanId": "xxxxx",
"status": {},
Expand Down

0 comments on commit 7b485e6

Please sign in to comment.