diff --git a/host/host.go b/host/host.go index b74d9c788..1adc80f07 100644 --- a/host/host.go +++ b/host/host.go @@ -42,7 +42,7 @@ type Frame struct { } type Trace struct { - Comm string + Comm libpf.String ProcessName libpf.String ExecutablePath libpf.String ContainerID libpf.String @@ -56,6 +56,6 @@ type Trace struct { APMTransactionID libpf.APMTransactionID CPU int EnvVars map[libpf.String]libpf.String - CustomLabels map[string]string + CustomLabels map[libpf.String]libpf.String KernelFrames libpf.Frames } diff --git a/interpreter/golabels/integrationtests/golabels_integration_test.go b/interpreter/golabels/integrationtests/golabels_integration_test.go index 6953b64c9..dab18c58f 100644 --- a/interpreter/golabels/integrationtests/golabels_integration_test.go +++ b/interpreter/golabels/integrationtests/golabels_integration_test.go @@ -134,8 +134,9 @@ func Test_Golabels(t *testing.T) { } if len(trace.CustomLabels) > 0 { hits := 0 - for k, v := range trace.CustomLabels { - t.Logf("Received label %v=%v", k, v) + for ks, vs := range trace.CustomLabels { + k := ks.String() + v := vs.String() if strings.HasPrefix(k, "l1") { require.Len(t, v, 22) require.True(t, strings.HasPrefix(v, "label1")) diff --git a/libpf/trace.go b/libpf/trace.go index 0a6032bb2..0a71838a8 100644 --- a/libpf/trace.go +++ b/libpf/trace.go @@ -76,5 +76,5 @@ func (frames *Frames) Append(frame *Frame) { type Trace struct { Frames Frames Hash TraceHash - CustomLabels map[string]string + CustomLabels map[String]String } diff --git a/reporter/internal/pdata/generate.go b/reporter/internal/pdata/generate.go index 8eb224235..97919722b 100644 --- a/reporter/internal/pdata/generate.go +++ b/reporter/internal/pdata/generate.go @@ -251,7 +251,7 @@ func (p *Pdata) setProfile( } attrMgr.AppendOptionalString(sample.AttributeIndices(), - semconv.ThreadNameKey, traceKey.Comm) + semconv.ThreadNameKey, traceKey.Comm.String()) attrMgr.AppendOptionalString(sample.AttributeIndices(), semconv.ProcessExecutableNameKey, exeName) @@ -278,8 +278,8 @@ func (p *Pdata) setProfile( // reached an agreement, use the actual OTel SemConv attribute. attrMgr.AppendOptionalString( sample.AttributeIndices(), - attribute.Key("process.context.label."+key), - value) + attribute.Key("process.context.label."+key.String()), + value.String()) } if p.ExtraSampleAttrProd != nil { diff --git a/reporter/internal/pdata/generate_test.go b/reporter/internal/pdata/generate_test.go index 5f89386e8..448bf430c 100644 --- a/reporter/internal/pdata/generate_test.go +++ b/reporter/internal/pdata/generate_test.go @@ -281,7 +281,7 @@ func TestGenerate_SingleContainerSingleOrigin(t *testing.T) { traceKey := samples.TraceAndMetaKey{ ExecutablePath: filePath, - Comm: "testproc", + Comm: libpf.Intern("testproc"), Pid: 123, Tid: 456, ApmServiceName: "svc", @@ -475,7 +475,7 @@ func TestGenerate_NativeFrame(t *testing.T) { traceKey := samples.TraceAndMetaKey{ ExecutablePath: filePath, - Comm: "native_app", + Comm: libpf.Intern("native_app"), Pid: 789, Tid: 1011, } diff --git a/reporter/samples/attrmgr_test.go b/reporter/samples/attrmgr_test.go index ec075c7ac..6917eeaab 100644 --- a/reporter/samples/attrmgr_test.go +++ b/reporter/samples/attrmgr_test.go @@ -22,6 +22,8 @@ type attributeStruct struct { } func TestAttrTableManager(t *testing.T) { + comm1 := libpf.Intern("comm1") + comm2 := libpf.Intern("comm2") tests := map[string]struct { k []TraceAndMetaKey expectedIndices [][]int32 @@ -31,7 +33,6 @@ func TestAttrTableManager(t *testing.T) { k: []TraceAndMetaKey{ { Hash: libpf.TraceHash{}, - Comm: "", ApmServiceName: "", Pid: 0, }, @@ -45,13 +46,13 @@ func TestAttrTableManager(t *testing.T) { k: []TraceAndMetaKey{ { Hash: libpf.TraceHash{}, - Comm: "comm1", + Comm: comm1, ApmServiceName: "apmServiceName1", Pid: 1234, }, { Hash: libpf.TraceHash{}, - Comm: "comm1", + Comm: comm1, ApmServiceName: "apmServiceName1", Pid: 1234, }, @@ -67,13 +68,13 @@ func TestAttrTableManager(t *testing.T) { k: []TraceAndMetaKey{ { Hash: libpf.TraceHash{}, - Comm: "comm1", + Comm: comm1, ApmServiceName: "apmServiceName1", Pid: 1234, }, { Hash: libpf.TraceHash{}, - Comm: "comm2", + Comm: comm2, ApmServiceName: "apmServiceName2", Pid: 6789, }, @@ -98,7 +99,7 @@ func TestAttrTableManager(t *testing.T) { indices := make([][]int32, 0) for _, k := range tc.k { inner := pcommon.NewInt32Slice() - mgr.AppendOptionalString(inner, semconv.ThreadNameKey, k.Comm) + mgr.AppendOptionalString(inner, semconv.ThreadNameKey, k.Comm.String()) mgr.AppendOptionalString(inner, semconv.ServiceNameKey, k.ApmServiceName) mgr.AppendInt(inner, semconv.ProcessPIDKey, k.Pid) indices = append(indices, inner.AsRaw()) diff --git a/reporter/samples/samples.go b/reporter/samples/samples.go index 4ef54add0..4e1559a87 100644 --- a/reporter/samples/samples.go +++ b/reporter/samples/samples.go @@ -9,7 +9,7 @@ import ( type TraceEventMeta struct { Timestamp libpf.UnixTime64 - Comm string + Comm libpf.String ProcessName libpf.String ExecutablePath libpf.String APMServiceName string @@ -27,7 +27,7 @@ type TraceEvents struct { Timestamps []uint64 // in nanoseconds OffTimes []int64 // in nanoseconds EnvVars map[libpf.String]libpf.String - Labels map[string]string + Labels map[libpf.String]libpf.String } // TraceAndMetaKey is the deduplication key for samples. This **must always** @@ -38,7 +38,7 @@ type TraceAndMetaKey struct { // to not aggregate difference traces. Hash libpf.TraceHash // comm and apmServiceName are provided by the eBPF programs - Comm string + Comm libpf.String ApmServiceName string Pid int64 Tid int64 diff --git a/tracer/ebpf_integration_test.go b/tracer/ebpf_integration_test.go index 5eb92effc..f6e6849ee 100644 --- a/tracer/ebpf_integration_test.go +++ b/tracer/ebpf_integration_test.go @@ -144,9 +144,10 @@ Loop: case <-timeout.C: break Loop case trace := <-traceChan: - require.GreaterOrEqual(t, len(trace.Comm), 4) - require.Equal(t, "\xAA\xBB\xCC", trace.Comm[0:3]) - traces[trace.Comm[3]] = trace + comm := trace.Comm.String() + require.GreaterOrEqual(t, len(comm), 4) + require.Equal(t, "\xAA\xBB\xCC", comm[0:3]) + traces[comm[3]] = trace } } diff --git a/tracer/tracer.go b/tracer/tracer.go index ddbd86c37..c1eb5b884 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -178,12 +178,12 @@ type progLoaderHelper struct { } // Convert a C-string to Go string. -func goString(cstr []byte) string { +func goString(cstr []byte) libpf.String { index := bytes.IndexByte(cstr, byte(0)) if index < 0 { index = len(cstr) } - return strings.Clone(pfunsafe.ToString(cstr[:index])) + return libpf.Intern(pfunsafe.ToString(cstr[:index])) } // schedProcessFreeHookName returns the name of the tracepoint hook to use. @@ -912,7 +912,7 @@ func (t *Tracer) loadBpfTrace(raw []byte, cpu int) *host.Trace { } if ptr.Custom_labels.Len > 0 { - trace.CustomLabels = make(map[string]string, int(ptr.Custom_labels.Len)) + trace.CustomLabels = make(map[libpf.String]libpf.String, int(ptr.Custom_labels.Len)) for i := 0; i < int(ptr.Custom_labels.Len); i++ { lbl := ptr.Custom_labels.Labels[i] key := goString(lbl.Key[:])