Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove path from HTTP instrumentation span name. #161

Merged
merged 6 commits into from
May 18, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
### Changed

- Upgrade OpenTelemetry semantic conventions to v1.18.0. ([#162](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/162))
- Remove the HTTP path from span names in `net/http`, `gin-gonic/gin`, and `gorilla/mux` instrumentations. ([#161](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/161))

## [v0.2.1-alpha] - 2023-05-15

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"

"os"

Expand All @@ -41,6 +40,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 = "github.com/gin-gonic/gin"

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

// LibraryName returns the gin-gonic/gin package import path.
func (h *Instrumentor) LibraryName() string {
return "github.com/gin-gonic/gin"
return instrumentedPkg
}

// FuncNames returns the function names from "github.com/gin-gonic/gin" that are
Expand Down Expand Up @@ -194,7 +195,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 @@ -203,8 +203,11 @@ func (h *Instrumentor) convertEvent(e *Event) *events.Event {
})

return &events.Event{
Library: h.LibraryName(),
Name: name,
Library: h.LibraryName(),
// Do not include the high-cardinality path here (there is no
// templatized path manifest to reference, given we are instrumenting
// Engine.ServeHTTP which is not passed a Gin Context).
Name: method,
Kind: trace.SpanKindServer,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
Expand Down
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 gin

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)
}
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 = "github.com/gorilla/mux"

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

// LibraryName returns the gorilla/mux package import path.
func (g *Instrumentor) LibraryName() string {
return "github.com/gorilla/mux"
return instrumentedPkg
}

// FuncNames returns the function names from "github.com/gorilla/mux" that are
Expand Down Expand Up @@ -191,7 +192,6 @@ func (g *Instrumentor) Run(eventsChan chan<- *events.Event) {
func (g *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 @@ -200,8 +200,10 @@ func (g *Instrumentor) convertEvent(e *Event) *events.Event {
})

return &events.Event{
Library: g.LibraryName(),
Name: name,
Library: g.LibraryName(),
// Do not include the high-cardinality path here (there is no
// templatized path manifest to reference).
Name: method,
Kind: trace.SpanKindServer,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
Expand Down
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 mux

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)
}
12 changes: 7 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,10 @@ func (h *Instrumentor) convertEvent(e *Event) *events.Event {
})

return &events.Event{
Library: h.LibraryName(),
Name: name,
Library: h.LibraryName(),
// Do not include the high-cardinality path here (there is no
// templatized path manifest to reference).
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/gin/traces.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"kind": 2,
"name": "GET /hello-gin",
"name": "GET",
"parentSpanId": "",
"spanId": "xxxxx",
"status": {},
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/gin/verify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ LIBRARY_NAME="github.com/gin-gonic/gin"
assert_equal "$result" '"sample-app"'
}

@test "${LIBRARY_NAME} :: emits a span name '{http.method} {http.target}' (per semconv)" {
@test "${LIBRARY_NAME} :: emits a span name '{http.method}' (per semconv)" {
result=$(span_names_for ${LIBRARY_NAME})
assert_equal "$result" '"GET /hello-gin"'
assert_equal "$result" '"GET"'
}

@test "${LIBRARY_NAME} :: includes http.method attribute" {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/gorillamux/traces.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"kind": 2,
"name": "GET /users/foo",
"name": "GET",
"parentSpanId": "",
"spanId": "xxxxx",
"status": {},
Expand Down Expand Up @@ -74,7 +74,7 @@
}
],
"kind": 2,
"name": "GET /users/foo",
"name": "GET",
"parentSpanId": "",
"spanId": "xxxxx",
"status": {},
Expand Down
Loading