Skip to content

Commit b6b8bb1

Browse files
committed
Collect user provided tracer name and use it for scope name
1 parent 7540073 commit b6b8bb1

File tree

11 files changed

+201
-14
lines changed

11 files changed

+201
-14
lines changed

internal/pkg/inject/offset_results.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,98 @@
939939
]
940940
}
941941
]
942+
},
943+
{
944+
"field": "name",
945+
"offsets": [
946+
{
947+
"offset": null,
948+
"versions": [
949+
"0.1.0",
950+
"0.1.1",
951+
"0.1.2",
952+
"0.2.0",
953+
"0.2.1",
954+
"0.2.2",
955+
"0.2.3",
956+
"0.3.0",
957+
"0.4.0",
958+
"0.4.1",
959+
"0.4.2",
960+
"0.4.3",
961+
"0.5.0",
962+
"0.6.0",
963+
"0.7.0",
964+
"0.8.0",
965+
"0.9.0",
966+
"0.10.0",
967+
"0.11.0",
968+
"0.12.0",
969+
"0.13.0"
970+
]
971+
},
972+
{
973+
"offset": 0,
974+
"versions": [
975+
"0.19.0",
976+
"0.20.0",
977+
"1.0.0-RC1",
978+
"1.0.0-RC2",
979+
"1.0.0-RC3",
980+
"1.0.0",
981+
"1.0.1",
982+
"1.1.0",
983+
"1.2.0",
984+
"1.3.0",
985+
"1.4.0",
986+
"1.4.1",
987+
"1.5.0",
988+
"1.6.0",
989+
"1.6.1",
990+
"1.6.2",
991+
"1.6.3",
992+
"1.7.0",
993+
"1.8.0",
994+
"1.9.0",
995+
"1.10.0",
996+
"1.11.0",
997+
"1.11.1",
998+
"1.11.2",
999+
"1.12.0",
1000+
"1.13.0",
1001+
"1.14.0",
1002+
"1.15.0-rc.1",
1003+
"1.15.0-rc.2",
1004+
"1.15.0",
1005+
"1.15.1",
1006+
"1.16.0-rc.1",
1007+
"1.16.0",
1008+
"1.17.0",
1009+
"1.18.0",
1010+
"1.19.0-rc.1",
1011+
"1.19.0"
1012+
]
1013+
},
1014+
{
1015+
"offset": 16,
1016+
"versions": [
1017+
"0.14.0",
1018+
"0.15.0",
1019+
"0.16.0",
1020+
"0.17.0",
1021+
"0.18.0",
1022+
"1.20.0",
1023+
"1.21.0",
1024+
"1.22.0",
1025+
"1.23.0-rc.1",
1026+
"1.23.0",
1027+
"1.23.1",
1028+
"1.24.0",
1029+
"1.25.0",
1030+
"1.26.0"
1031+
]
1032+
}
1033+
]
9421034
}
9431035
]
9441036
}

internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/bpf/probe.bpf.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ char __license[] SEC("license") = "Dual MIT/GPL";
2525
#define MAX_CONCURRENT 50
2626
#define MAX_SPAN_NAME_LEN 64
2727
#define MAX_STATUS_DESCRIPTION_LEN 64
28+
#define MAX_TRACER_NAME_LEN 128
2829

2930
struct span_description_t {
3031
char buf[MAX_STATUS_DESCRIPTION_LEN];
@@ -39,11 +40,16 @@ struct span_name_t {
3940
char buf[MAX_SPAN_NAME_LEN];
4041
};
4142

43+
typedef struct tracer_name {
44+
char buf[MAX_TRACER_NAME_LEN];
45+
} tracer_name_t;
46+
4247
struct otel_span_t {
4348
BASE_SPAN_PROPERTIES
4449
struct span_name_t span_name;
4550
otel_status_t status;
4651
otel_attributes_t attributes;
52+
tracer_name_t tracer_name;
4753
};
4854

4955
struct {
@@ -60,6 +66,13 @@ struct {
6066
__uint(max_entries, MAX_CONCURRENT);
6167
} span_name_by_context SEC(".maps");
6268

69+
struct {
70+
__uint(type, BPF_MAP_TYPE_HASH);
71+
__type(key, void*);
72+
__type(value, tracer_name_t);
73+
__uint(max_entries, MAX_CONCURRENT);
74+
} tracer_name_by_context SEC(".maps");
75+
6376
struct
6477
{
6578
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
@@ -74,6 +87,7 @@ struct {
7487

7588
// Injected in init
7689
volatile const u64 tracer_delegate_pos;
90+
volatile const u64 tracer_name_pos;
7791

7892
// read_span_name reads the span name from the provided span_name_ptr and stores the result in
7993
// span_name.buf.
@@ -105,6 +119,14 @@ int uprobe_Start(struct pt_regs *ctx) {
105119
void *context_ptr_val = get_Go_context(ctx, 3, 0, true);
106120
void *key = get_consistent_key(ctx, context_ptr_val);
107121
bpf_map_update_elem(&span_name_by_context, &key, &span_name, 0);
122+
123+
// Get the tracer name
124+
tracer_name_t tracer_name = {0};
125+
if (!get_go_string_from_user_ptr((void*)(tracer_ptr + tracer_name_pos), tracer_name.buf, MAX_TRACER_NAME_LEN)) {
126+
bpf_printk("Failed to read tracer name");
127+
} else {
128+
bpf_map_update_elem(&tracer_name_by_context, &key, &tracer_name, 0);
129+
}
108130
return 0;
109131
}
110132

@@ -121,6 +143,11 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {
121143
return 0;
122144
}
123145

146+
tracer_name_t *tracer_name = bpf_map_lookup_elem(&tracer_name_by_context, &key);
147+
if (tracer_name == NULL) {
148+
goto done_without_tracer_name;
149+
}
150+
124151
u32 zero_span_key = 0;
125152
struct otel_span_t *zero_span = bpf_map_lookup_elem(&otel_span_storage_map, &zero_span_key);
126153
if (zero_span == NULL) {
@@ -137,7 +164,8 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {
137164
}
138165

139166
otel_span->start_time = bpf_ktime_get_ns();
140-
copy_byte_arrays((unsigned char*)span_name->buf, (unsigned char*)otel_span->span_name.buf, MAX_SPAN_NAME_LEN);
167+
otel_span->span_name = *span_name;
168+
otel_span->tracer_name = *tracer_name;
141169

142170
// Get the ** returned ** context and Span (concrete type of the interfaces)
143171
void *ret_context_ptr_val = get_argument(ctx, 2);
@@ -157,6 +185,8 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {
157185
start_tracking_span(ret_context_ptr_val, &otel_span->sc);
158186

159187
done:
188+
bpf_map_delete_elem(&tracer_name_by_context, &key);
189+
done_without_tracer_name:
160190
bpf_map_delete_elem(&span_name_by_context, &key);
161191
return 0;
162192
}

internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/bpf_arm64_bpfel.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/bpf_x86_bpfel.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/instrumentation/bpf/go.opentelemetry.io/otel/traceglobal/probe.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func New(logger logr.Logger) probe.Probe {
9494
Key: "tracer_delegate_pos",
9595
Val: structfield.NewID("go.opentelemetry.io/otel", "go.opentelemetry.io/otel/internal/global", "tracer", "delegate"),
9696
},
97+
probe.StructFieldConst{
98+
Key: "tracer_name_pos",
99+
Val: structfield.NewID("go.opentelemetry.io/otel", "go.opentelemetry.io/otel/internal/global", "tracer", "name"),
100+
},
97101
},
98102
Uprobes: []probe.Uprobe[bpfObjects]{
99103
{
@@ -252,6 +256,7 @@ type event struct {
252256
SpanName [64]byte
253257
Status status
254258
Attributes attributesBuffer
259+
TracerName [128]byte
255260
}
256261

257262
func convertEvent(e *event) []*probe.SpanEvent {
@@ -288,6 +293,7 @@ func convertEvent(e *event) []*probe.SpanEvent {
288293
Code: codes.Code(e.Status.Code),
289294
Description: string(unix.ByteSliceToString(e.Status.Description[:])),
290295
},
296+
TracerName: unix.ByteSliceToString(e.TracerName[:]),
291297
},
292298
}
293299
}

internal/pkg/instrumentation/probe/event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ type SpanEvent struct {
4040
SpanContext *trace.SpanContext
4141
ParentSpanContext *trace.SpanContext
4242
Status Status
43+
TracerName string
4344
}

internal/pkg/opentelemetry/controller.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,32 @@ type Controller struct {
3535
bootTime int64
3636
}
3737

38-
func (c *Controller) getTracer(pkg string) trace.Tracer {
39-
t, exists := c.tracersMap[pkg]
38+
func (c *Controller) getTracer(pkg, tracerName string) trace.Tracer {
39+
var (
40+
newTracer trace.Tracer
41+
tracerKey = pkg
42+
)
43+
44+
if tracerName != "" {
45+
tracerKey = tracerName
46+
}
47+
48+
t, exists := c.tracersMap[tracerKey]
4049
if exists {
4150
return t
4251
}
4352

44-
newTracer := c.tracerProvider.Tracer(
45-
"go.opentelemetry.io/auto/"+pkg,
46-
trace.WithInstrumentationVersion(c.version),
47-
)
48-
c.tracersMap[pkg] = newTracer
53+
if tracerName != "" {
54+
// If the user has provided a tracer name, use it.
55+
newTracer = c.tracerProvider.Tracer(tracerName)
56+
} else {
57+
newTracer = c.tracerProvider.Tracer(
58+
"go.opentelemetry.io/auto/"+pkg,
59+
trace.WithInstrumentationVersion(c.version),
60+
)
61+
}
62+
63+
c.tracersMap[tracerKey] = newTracer
4964
return newTracer
5065
}
5166

@@ -66,7 +81,7 @@ func (c *Controller) Trace(event *probe.Event) {
6681
}
6782

6883
ctx = ContextWithEBPFEvent(ctx, *se)
69-
_, span := c.getTracer(event.Package).
84+
_, span := c.getTracer(event.Package, se.TracerName).
7085
Start(ctx, se.SpanName,
7186
trace.WithAttributes(se.Attributes...),
7287
trace.WithSpanKind(event.Kind),

internal/pkg/opentelemetry/controller_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,34 @@ func TestTrace(t *testing.T) {
237237
})
238238
}
239239
}
240+
241+
func TestGetTracer(t *testing.T) {
242+
logger := stdr.New(log.New(os.Stderr, "", log.LstdFlags))
243+
244+
exporter := tracetest.NewInMemoryExporter()
245+
tp := sdktrace.NewTracerProvider(
246+
sdktrace.WithSampler(sdktrace.AlwaysSample()),
247+
sdktrace.WithBatcher(exporter),
248+
sdktrace.WithResource(instResource()),
249+
)
250+
defer func() {
251+
err := tp.Shutdown(context.Background())
252+
assert.NoError(t, err)
253+
}()
254+
255+
ctrl, err := NewController(logger, tp, "test")
256+
assert.NoError(t, err)
257+
258+
t1 := ctrl.getTracer("foo/bar", "test")
259+
assert.Equal(t, t1, ctrl.tracersMap["test"])
260+
assert.Nil(t, ctrl.tracersMap["foo/bar"])
261+
262+
t2 := ctrl.getTracer("net/http", "")
263+
assert.Equal(t, t2, ctrl.tracersMap["net/http"])
264+
265+
t3 := ctrl.getTracer("foo/bar", "test")
266+
assert.Equal(t, t1, t3)
267+
268+
t4 := ctrl.getTracer("net/http", "")
269+
assert.Equal(t, t2, t4)
270+
}

0 commit comments

Comments
 (0)