diff --git a/pkg/instrumentors/bpf/net/http/server/probe.go b/pkg/instrumentors/bpf/net/http/server/probe.go index 78cbce87b..8ea42e399 100644 --- a/pkg/instrumentors/bpf/net/http/server/probe.go +++ b/pkg/instrumentors/bpf/net/http/server/probe.go @@ -18,7 +18,6 @@ import ( "bytes" "encoding/binary" "errors" - "fmt" "os" "go.opentelemetry.io/auto/pkg/instrumentors/bpffs" @@ -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 { @@ -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. @@ -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, @@ -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), diff --git a/pkg/instrumentors/bpf/net/http/server/probe_test.go b/pkg/instrumentors/bpf/net/http/server/probe_test.go new file mode 100644 index 000000000..23dc3e5ed --- /dev/null +++ b/pkg/instrumentors/bpf/net/http/server/probe_test.go @@ -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) +} diff --git a/test/e2e/nethttp/traces.json b/test/e2e/nethttp/traces.json index 7a8f957ed..c6f740302 100644 --- a/test/e2e/nethttp/traces.json +++ b/test/e2e/nethttp/traces.json @@ -45,7 +45,7 @@ } ], "kind": 2, - "name": "GET /hello", + "name": "GET", "parentSpanId": "", "spanId": "xxxxx", "status": {},