Skip to content

Commit

Permalink
Check if tracer.delegate is set at tracer.Start to avoid tracking it (#…
Browse files Browse the repository at this point in the history
…569)

Co-authored-by: Eden Federman <[email protected]>
  • Loading branch information
RonFed and edeNFed authored Dec 31, 2023
1 parent 4e962c0 commit da00d2e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
91 changes: 91 additions & 0 deletions internal/pkg/inject/offset_results.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,95 @@
[
{
"module": "go.opentelemetry.io/otel",
"packages": [
{
"package": "go.opentelemetry.io/otel/internal/global",
"structs": [
{
"struct": "tracer",
"fields": [
{
"field": "delegate",
"offsets": [
{
"offset": 40,
"versions": [
"0.19.0",
"0.20.0"
]
},
{
"offset": 48,
"versions": [
"1.0.0-RC1",
"1.0.0-RC2",
"1.0.0-RC3",
"1.0.0",
"1.0.1",
"1.1.0",
"1.2.0",
"1.3.0",
"1.4.0",
"1.4.1",
"1.5.0",
"1.6.0",
"1.6.1",
"1.6.2",
"1.6.3",
"1.7.0",
"1.8.0",
"1.9.0",
"1.10.0",
"1.11.0",
"1.11.1",
"1.11.2",
"1.12.0",
"1.13.0",
"1.14.0",
"1.15.0-rc.1",
"1.15.0-rc.2",
"1.15.0",
"1.15.1",
"1.16.0-rc.1",
"1.16.0",
"1.17.0",
"1.18.0",
"1.19.0-rc.1",
"1.19.0"
]
},
{
"offset": 56,
"versions": [
"0.14.0",
"0.15.0",
"0.16.0",
"0.17.0",
"0.18.0"
]
},
{
"offset": 64,
"versions": [
"0.2.2",
"0.2.3",
"0.3.0",
"0.4.0",
"0.4.1",
"0.4.2",
"0.4.3",
"1.20.0",
"1.21.0"
]
}
]
}
]
}
]
}
]
},
{
"module": "golang.org/x/net",
"packages": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ struct {
} events SEC(".maps");

// Injected in init
volatile const u64 span_name_pos;
volatile const u64 span_attributes_pos;
volatile const u64 tracer_delegate_pos;

// This instrumentation attaches uprobe to the following function:
// func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
// https://github.com/open-telemetry/opentelemetry-go/blob/98b32a6c3a87fbee5d34c063b9096f416b250897/internal/global/trace.go#L149
SEC("uprobe/Start")
int uprobe_Start(struct pt_regs *ctx) {
void *tracer_ptr = get_argument(ctx, 1);
void *delegate_ptr = NULL;
bpf_probe_read(&delegate_ptr, sizeof(delegate_ptr), (void*)(tracer_ptr + tracer_delegate_pos));
if (delegate_ptr != NULL) {
// Delegate is set, so we should not instrument this call
return 0;
}
struct span_name_t span_name = {0};

// Getting span name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/structfield"

"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
Expand Down Expand Up @@ -87,6 +88,10 @@ func New(logger logr.Logger) probe.Probe {
Key: "attr_type_stringslice",
Val: uint64(attribute.STRINGSLICE),
},
probe.StructFieldConst{
Key: "tracer_delegate_pos",
Val: structfield.NewID("go.opentelemetry.io/otel", "go.opentelemetry.io/otel/internal/global", "tracer", "delegate"),
},
},
Uprobes: []probe.Uprobe[bpfObjects]{
{
Expand Down
14 changes: 14 additions & 0 deletions internal/tools/inspect/cmd/offsetgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func manifests() ([]inspect.Manifest, error) {
return nil, fmt.Errorf("failed to get \"golang.org/x/net\" versions: %w", err)
}

goOtelVers, err := PkgVersions("go.opentelemetry.io/otel")
if err != nil {
return nil, fmt.Errorf("failed to get \"go.opentelemetry.io/otel\" versions: %w", err)
}

ren := func(src string) inspect.Renderer {
return inspect.NewRenderer(logger, src, inspect.DefaultFS)
}
Expand Down Expand Up @@ -140,6 +145,15 @@ func manifests() ([]inspect.Manifest, error) {
structfield.NewID("golang.org/x/net", "golang.org/x/net/http2", "FrameHeader", "StreamID"),
},
},
{
Application: inspect.Application{
Renderer: ren("templates/go.opentelemetry.io/otel/traceglobal/*.tmpl"),
Versions: goOtelVers,
},
StructFields: []structfield.ID{
structfield.NewID("go.opentelemetry.io/otel", "go.opentelemetry.io/otel/internal/global", "tracer", "delegate"),
},
},
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/tools/inspect/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
//go:embed templates/google.golang.org/grpc/*.tmpl
//go:embed templates/net/http/*.tmpl
//go:embed templates/runtime/*.tmpl
//go:embed templates/go.opentelemetry.io/otel/traceglobal/*.tmpl
var DefaultFS embed.FS

// Renderer renders templates from an fs.FS.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module otelapp

go 1.12

require go.opentelemetry.io/otel {{ .Version }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"context"
"go.opentelemetry.io/otel"
)

var tracer = otel.Tracer("trace-example")

func main() {
_, span := tracer.Start(context.Background(), "some name")
defer span.End()
}

0 comments on commit da00d2e

Please sign in to comment.