From 232c3fd6e79a348106b8e420223ee76bb74bbc37 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 19:38:44 -0400 Subject: [PATCH 01/15] Create Interface For Initializing Sanitizers Signed-off-by: Mahad Zaryab --- .../storageexporter/sanitizer/sanitizer.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go new file mode 100644 index 00000000000..3fc78fd84c9 --- /dev/null +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go @@ -0,0 +1,26 @@ +package sanitizer + +import ( + "go.opentelemetry.io/collector/pdata/ptrace" +) + +// Sanitize is a function that performs enrichment, clean-up, or normalization of trace data. +type Sanitize func(traces ptrace.Traces) ptrace.Traces + +func NewStandardSanitizers() []Sanitize { + return []Sanitize{} +} + +// NewChainedSanitizer creates a Sanitizer from the variadic list of passed Sanitizers. +// If the list only has one element, it is returned directly to minimize indirection. +func NewChainedSanitizer(sanitizers ...Sanitize) Sanitize { + if len(sanitizers) == 1 { + return sanitizers[0] + } + return func(traces ptrace.Traces) ptrace.Traces { + for _, s := range sanitizers { + traces = s(traces) + } + return traces + } +} From 648058e758992ce3ac6d7368b95040760853e068 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 20:40:10 -0400 Subject: [PATCH 02/15] Add Documentation For Standard Sanitizers Function Signed-off-by: Mahad Zaryab --- .../exporters/storageexporter/sanitizer/sanitizer.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go index 3fc78fd84c9..64b19a3b41e 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go @@ -7,8 +7,12 @@ import ( // Sanitize is a function that performs enrichment, clean-up, or normalization of trace data. type Sanitize func(traces ptrace.Traces) ptrace.Traces +// NewStandardSanitizers returns a list of all the sanitizers that are used by the +// storage exporter. func NewStandardSanitizers() []Sanitize { - return []Sanitize{} + return []Sanitize{ + NewEmptyServiceNameSanitizer(), + } } // NewChainedSanitizer creates a Sanitizer from the variadic list of passed Sanitizers. From 0a9074118daabcb65941dc1b59dbfe37589b67f8 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 20:41:46 -0400 Subject: [PATCH 03/15] Implement Empty Service Name Sanitizer Signed-off-by: Mahad Zaryab --- .../sanitizer/emptyservicename.go | 36 ++++++++++++ .../sanitizer/emptyservicename_test.go | 57 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go create mode 100644 cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go new file mode 100644 index 00000000000..099cfbd74db --- /dev/null +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go @@ -0,0 +1,36 @@ +package sanitizer + +import ( + "fmt" + + "github.com/jaegertracing/jaeger/pkg/otelsemconv" + "go.opentelemetry.io/collector/pdata/ptrace" +) + +const ( + emptyServiceName = "empty-service-name" + missingServiceName = "missing-service-name" +) + +// NewEmptyServiceNameSanitizer returns a sanitizer function that replaces +// empty and missing service names with placeholder strings. +func NewEmptyServiceNameSanitizer() Sanitize { + return sanitizeEmptyServiceName +} + +func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces { + resourceSpans := traces.ResourceSpans() + for i := 0; i < resourceSpans.Len(); i++ { + resourceSpan := resourceSpans.At(i) + attributes := resourceSpan.Resource().Attributes() + serviceName, ok := attributes.Get(string(otelsemconv.ServiceNameKey)) + fmt.Printf("service name: %s, ok: %v\n", serviceName.Str(), ok) + if !ok { + attributes.PutStr(string(otelsemconv.ServiceNameKey), missingServiceName) + } else if serviceName.Str() == "" { + fmt.Println("here") + attributes.PutStr(string(otelsemconv.ServiceNameKey), emptyServiceName) + } + } + return traces +} diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go new file mode 100644 index 00000000000..58bd432494e --- /dev/null +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go @@ -0,0 +1,57 @@ +package sanitizer + +import ( + "testing" + + "github.com/crossdock/crossdock-go/require" + "go.opentelemetry.io/collector/pdata/ptrace" +) + +func TestEmptyServiceNameSanitizer(t *testing.T) { + emptyServiceName := "" + nonEmptyServiceName := "hello" + tests := []struct { + name string + serviceName *string + expectedServiceName string + }{ + { + name: "no service name", + expectedServiceName: "missing-service-name", + }, + { + name: "empty service name", + serviceName: &emptyServiceName, + expectedServiceName: "empty-service-name", + }, + { + name: "non-empty service name", + serviceName: &nonEmptyServiceName, + expectedServiceName: "hello", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + traces := ptrace.NewTraces() + attributes := traces. + ResourceSpans(). + AppendEmpty(). + Resource(). + Attributes() + if test.serviceName != nil { + attributes.PutStr("service.name", *test.serviceName) + } + sanitizer := NewEmptyServiceNameSanitizer() + sanitized := sanitizer(traces) + serviceName, ok := sanitized. + ResourceSpans(). + At(0). + Resource(). + Attributes(). + Get("service.name") + require.True(t, ok) + require.Equal(t, test.expectedServiceName, serviceName.Str()) + }) + } +} From f4c1bcdb79e842415ddf7796a07e5b62c1d5c952 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 21:52:01 -0400 Subject: [PATCH 04/15] Add Sanitizer To Exporter Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/exporters/storageexporter/exporter.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/jaeger/internal/exporters/storageexporter/exporter.go b/cmd/jaeger/internal/exporters/storageexporter/exporter.go index 2e29be4cc39..cee24e0e198 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/exporter.go +++ b/cmd/jaeger/internal/exporters/storageexporter/exporter.go @@ -11,6 +11,7 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" "go.uber.org/zap" + "github.com/jaegertracing/jaeger/cmd/jaeger/internal/exporters/storageexporter/sanitizer" "github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage" "github.com/jaegertracing/jaeger/storage_v2/spanstore" ) @@ -19,12 +20,16 @@ type storageExporter struct { config *Config logger *zap.Logger traceWriter spanstore.Writer + sanitizer sanitizer.Sanitize } func newExporter(config *Config, otel component.TelemetrySettings) *storageExporter { return &storageExporter{ config: config, logger: otel.Logger, + sanitizer: sanitizer.NewChainedSanitizer( + sanitizer.NewStandardSanitizers()..., + ), } } @@ -47,5 +52,5 @@ func (*storageExporter) close(_ context.Context) error { } func (exp *storageExporter) pushTraces(ctx context.Context, td ptrace.Traces) error { - return exp.traceWriter.WriteTraces(ctx, td) + return exp.traceWriter.WriteTraces(ctx, exp.sanitizer(td)) } From 24e1a6be9d1005e83f08eb672088fc5d4dbf0484 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 21:52:59 -0400 Subject: [PATCH 05/15] Add package_test Signed-off-by: Mahad Zaryab --- .../storageexporter/sanitizer/package_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go new file mode 100644 index 00000000000..fb4e60c3dd0 --- /dev/null +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go @@ -0,0 +1,11 @@ +package sanitizer + +import ( + "testing" + + "github.com/jaegertracing/jaeger/pkg/testutils" +) + +func TestMain(m *testing.M) { + testutils.VerifyGoLeaks(m) +} From b65efe7321fe641ebed822c890690d1a3bd3ca0e Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:23:30 -0400 Subject: [PATCH 06/15] Add Test For New Chained Sanitizer Signed-off-by: Mahad Zaryab --- .../sanitizer/sanitizer_test.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go new file mode 100644 index 00000000000..91167fe7f08 --- /dev/null +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go @@ -0,0 +1,52 @@ +// Copyright (c) 2024 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + +package sanitizer + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/pdata/ptrace" +) + +func TestNewChainedSanitizer(t *testing.T) { + var s1 Sanitize = func(traces ptrace.Traces) ptrace.Traces { + traces. + ResourceSpans(). + AppendEmpty(). + Resource(). + Attributes(). + PutStr("hello", "world") + return traces + } + var s2 Sanitize = func(traces ptrace.Traces) ptrace.Traces { + traces. + ResourceSpans(). + At(0). + Resource(). + Attributes(). + PutStr("hello", "goodbye") + return traces + } + c1 := NewChainedSanitizer(s1) + t1 := c1(ptrace.NewTraces()) + hello, ok := t1. + ResourceSpans(). + At(0). + Resource(). + Attributes(). + Get("hello") + require.True(t, ok) + require.Equal(t, "world", hello.Str()) + c2 := NewChainedSanitizer(s1, s2) + t2 := c2(ptrace.NewTraces()) + hello, ok = t2. + ResourceSpans(). + At(0). + Resource(). + Attributes(). + Get("hello") + require.True(t, ok) + require.Equal(t, "goodbye", hello.Str()) +} From a52a4b0869673d5dfeab5c02f1b2d1ec1b268b7a Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:23:43 -0400 Subject: [PATCH 07/15] Run Formatter Signed-off-by: Mahad Zaryab --- .../exporters/storageexporter/sanitizer/emptyservicename.go | 6 +++++- .../storageexporter/sanitizer/emptyservicename_test.go | 5 ++++- .../exporters/storageexporter/sanitizer/package_test.go | 3 +++ .../exporters/storageexporter/sanitizer/sanitizer.go | 3 +++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go index 099cfbd74db..8cec0241194 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go @@ -1,10 +1,14 @@ +// Copyright (c) 2024 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + package sanitizer import ( "fmt" - "github.com/jaegertracing/jaeger/pkg/otelsemconv" "go.opentelemetry.io/collector/pdata/ptrace" + + "github.com/jaegertracing/jaeger/pkg/otelsemconv" ) const ( diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go index 58bd432494e..c6c1df4736e 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go @@ -1,9 +1,12 @@ +// Copyright (c) 2024 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + package sanitizer import ( "testing" - "github.com/crossdock/crossdock-go/require" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/ptrace" ) diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go index fb4e60c3dd0..a7d22934b92 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go @@ -1,3 +1,6 @@ +// Copyright (c) 2024 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + package sanitizer import ( diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go index 64b19a3b41e..ba59910f374 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go +++ b/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go @@ -1,3 +1,6 @@ +// Copyright (c) 2024 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + package sanitizer import ( From 252f710899f16782e4b7a4cc63411c82cbf7c3d1 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:36:33 -0400 Subject: [PATCH 08/15] Move Sanitizer Outside of ExportersDirectory Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/exporters/storageexporter/exporter.go | 2 +- .../storageexporter => }/sanitizer/emptyservicename.go | 0 .../storageexporter => }/sanitizer/emptyservicename_test.go | 0 .../{exporters/storageexporter => }/sanitizer/package_test.go | 0 .../{exporters/storageexporter => }/sanitizer/sanitizer.go | 0 .../{exporters/storageexporter => }/sanitizer/sanitizer_test.go | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename cmd/jaeger/internal/{exporters/storageexporter => }/sanitizer/emptyservicename.go (100%) rename cmd/jaeger/internal/{exporters/storageexporter => }/sanitizer/emptyservicename_test.go (100%) rename cmd/jaeger/internal/{exporters/storageexporter => }/sanitizer/package_test.go (100%) rename cmd/jaeger/internal/{exporters/storageexporter => }/sanitizer/sanitizer.go (100%) rename cmd/jaeger/internal/{exporters/storageexporter => }/sanitizer/sanitizer_test.go (100%) diff --git a/cmd/jaeger/internal/exporters/storageexporter/exporter.go b/cmd/jaeger/internal/exporters/storageexporter/exporter.go index cee24e0e198..cbadc1fa16e 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/exporter.go +++ b/cmd/jaeger/internal/exporters/storageexporter/exporter.go @@ -11,8 +11,8 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" "go.uber.org/zap" - "github.com/jaegertracing/jaeger/cmd/jaeger/internal/exporters/storageexporter/sanitizer" "github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage" + "github.com/jaegertracing/jaeger/cmd/jaeger/internal/sanitizer" "github.com/jaegertracing/jaeger/storage_v2/spanstore" ) diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go b/cmd/jaeger/internal/sanitizer/emptyservicename.go similarity index 100% rename from cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename.go rename to cmd/jaeger/internal/sanitizer/emptyservicename.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go b/cmd/jaeger/internal/sanitizer/emptyservicename_test.go similarity index 100% rename from cmd/jaeger/internal/exporters/storageexporter/sanitizer/emptyservicename_test.go rename to cmd/jaeger/internal/sanitizer/emptyservicename_test.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go b/cmd/jaeger/internal/sanitizer/package_test.go similarity index 100% rename from cmd/jaeger/internal/exporters/storageexporter/sanitizer/package_test.go rename to cmd/jaeger/internal/sanitizer/package_test.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go b/cmd/jaeger/internal/sanitizer/sanitizer.go similarity index 100% rename from cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer.go rename to cmd/jaeger/internal/sanitizer/sanitizer.go diff --git a/cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go b/cmd/jaeger/internal/sanitizer/sanitizer_test.go similarity index 100% rename from cmd/jaeger/internal/exporters/storageexporter/sanitizer/sanitizer_test.go rename to cmd/jaeger/internal/sanitizer/sanitizer_test.go From 24f1df9aa02feed6c15b7698545def847a8e873d Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:38:06 -0400 Subject: [PATCH 09/15] Remove Debugging Statements Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/sanitizer/emptyservicename.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/jaeger/internal/sanitizer/emptyservicename.go b/cmd/jaeger/internal/sanitizer/emptyservicename.go index 8cec0241194..362c31738a6 100644 --- a/cmd/jaeger/internal/sanitizer/emptyservicename.go +++ b/cmd/jaeger/internal/sanitizer/emptyservicename.go @@ -4,8 +4,6 @@ package sanitizer import ( - "fmt" - "go.opentelemetry.io/collector/pdata/ptrace" "github.com/jaegertracing/jaeger/pkg/otelsemconv" @@ -28,11 +26,9 @@ func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces { resourceSpan := resourceSpans.At(i) attributes := resourceSpan.Resource().Attributes() serviceName, ok := attributes.Get(string(otelsemconv.ServiceNameKey)) - fmt.Printf("service name: %s, ok: %v\n", serviceName.Str(), ok) if !ok { attributes.PutStr(string(otelsemconv.ServiceNameKey), missingServiceName) } else if serviceName.Str() == "" { - fmt.Println("here") attributes.PutStr(string(otelsemconv.ServiceNameKey), emptyServiceName) } } From f032c20b8421bb819095978eec0cca232dbfa64e Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:42:32 -0400 Subject: [PATCH 10/15] Add Test For New Standard Sanitizers Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/sanitizer/sanitizer_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/jaeger/internal/sanitizer/sanitizer_test.go b/cmd/jaeger/internal/sanitizer/sanitizer_test.go index 91167fe7f08..aa592017f19 100644 --- a/cmd/jaeger/internal/sanitizer/sanitizer_test.go +++ b/cmd/jaeger/internal/sanitizer/sanitizer_test.go @@ -10,6 +10,11 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" ) +func TestNewStandardSanitizers(t *testing.T) { + sanitizers := NewStandardSanitizers() + require.Len(t, sanitizers, 1) +} + func TestNewChainedSanitizer(t *testing.T) { var s1 Sanitize = func(traces ptrace.Traces) ptrace.Traces { traces. From 3fca5a8c3e8b79908b34b23741cf3d0232e4cfc4 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 22:57:22 -0400 Subject: [PATCH 11/15] Add Check For Non String Service Name Signed-off-by: Mahad Zaryab --- .../internal/sanitizer/emptyservicename.go | 8 +++++-- .../sanitizer/emptyservicename_test.go | 22 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cmd/jaeger/internal/sanitizer/emptyservicename.go b/cmd/jaeger/internal/sanitizer/emptyservicename.go index 362c31738a6..94928abac29 100644 --- a/cmd/jaeger/internal/sanitizer/emptyservicename.go +++ b/cmd/jaeger/internal/sanitizer/emptyservicename.go @@ -4,14 +4,16 @@ package sanitizer import ( + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" "github.com/jaegertracing/jaeger/pkg/otelsemconv" ) const ( - emptyServiceName = "empty-service-name" - missingServiceName = "missing-service-name" + emptyServiceName = "empty-service-name" + serviceNameWrongType = "service-name-wrong-type" + missingServiceName = "missing-service-name" ) // NewEmptyServiceNameSanitizer returns a sanitizer function that replaces @@ -28,6 +30,8 @@ func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces { serviceName, ok := attributes.Get(string(otelsemconv.ServiceNameKey)) if !ok { attributes.PutStr(string(otelsemconv.ServiceNameKey), missingServiceName) + } else if serviceName.Type() != pcommon.ValueTypeStr { + attributes.PutStr(string(otelsemconv.ServiceNameKey), serviceNameWrongType) } else if serviceName.Str() == "" { attributes.PutStr(string(otelsemconv.ServiceNameKey), emptyServiceName) } diff --git a/cmd/jaeger/internal/sanitizer/emptyservicename_test.go b/cmd/jaeger/internal/sanitizer/emptyservicename_test.go index c6c1df4736e..19073d3a0d1 100644 --- a/cmd/jaeger/internal/sanitizer/emptyservicename_test.go +++ b/cmd/jaeger/internal/sanitizer/emptyservicename_test.go @@ -10,7 +10,7 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" ) -func TestEmptyServiceNameSanitizer(t *testing.T) { +func TestEmptyServiceNameSanitizer_SubstitutesCorrectlyForStrings(t *testing.T) { emptyServiceName := "" nonEmptyServiceName := "hello" tests := []struct { @@ -58,3 +58,23 @@ func TestEmptyServiceNameSanitizer(t *testing.T) { }) } } + +func TestEmptyServiceNameSanitizer_SubstitutesCorrectlyForNonStringType(t *testing.T) { + traces := ptrace.NewTraces() + traces. + ResourceSpans(). + AppendEmpty(). + Resource(). + Attributes(). + PutInt("service.name", 1) + sanitizer := NewEmptyServiceNameSanitizer() + sanitized := sanitizer(traces) + serviceName, ok := sanitized. + ResourceSpans(). + At(0). + Resource(). + Attributes(). + Get("service.name") + require.True(t, ok) + require.Equal(t, "service-name-wrong-type", serviceName.Str()) +} From b12dbeef004eaa185a37b67e5208f9c3e811f022 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 23:27:16 -0400 Subject: [PATCH 12/15] Use Switch Statement Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/sanitizer/emptyservicename.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/jaeger/internal/sanitizer/emptyservicename.go b/cmd/jaeger/internal/sanitizer/emptyservicename.go index 94928abac29..d0ba8060f0a 100644 --- a/cmd/jaeger/internal/sanitizer/emptyservicename.go +++ b/cmd/jaeger/internal/sanitizer/emptyservicename.go @@ -28,11 +28,12 @@ func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces { resourceSpan := resourceSpans.At(i) attributes := resourceSpan.Resource().Attributes() serviceName, ok := attributes.Get(string(otelsemconv.ServiceNameKey)) - if !ok { + switch { + case !ok: attributes.PutStr(string(otelsemconv.ServiceNameKey), missingServiceName) - } else if serviceName.Type() != pcommon.ValueTypeStr { + case serviceName.Type() != pcommon.ValueTypeStr: attributes.PutStr(string(otelsemconv.ServiceNameKey), serviceNameWrongType) - } else if serviceName.Str() == "" { + case serviceName.Str() == "": attributes.PutStr(string(otelsemconv.ServiceNameKey), emptyServiceName) } } From 7ccd378dcb5b641efc21464d3718dcc76b790776 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 23:36:43 -0400 Subject: [PATCH 13/15] Add Check For Sanitizer In exporter_test Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/exporters/storageexporter/exporter_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go b/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go index f5286679924..ba57dbf213f 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go +++ b/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go @@ -157,6 +157,9 @@ func TestExporter(t *testing.T) { requiredTrace, err := spanReader.GetTrace(ctx, requiredTraceID) require.NoError(t, err) assert.Equal(t, spanID.String(), requiredTrace.Spans[0].SpanID.String()) + + // check that the service name attribute was by the sanitizer + require.Equal(t, "missing-service-name", requiredTrace.Spans[0].Process.ServiceName) } func makeStorageExtension(t *testing.T, memstoreName string) component.Host { From ce9ce7019e4465fa6c1b2e1ca8798e64b7a7c0f6 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 23:37:22 -0400 Subject: [PATCH 14/15] Fix Typo Signed-off-by: Mahad Zaryab --- cmd/jaeger/internal/exporters/storageexporter/exporter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go b/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go index ba57dbf213f..a73375faadd 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go +++ b/cmd/jaeger/internal/exporters/storageexporter/exporter_test.go @@ -158,7 +158,7 @@ func TestExporter(t *testing.T) { require.NoError(t, err) assert.Equal(t, spanID.String(), requiredTrace.Spans[0].SpanID.String()) - // check that the service name attribute was by the sanitizer + // check that the service name attribute was added by the sanitizer require.Equal(t, "missing-service-name", requiredTrace.Spans[0].Process.ServiceName) } From f3deafa77a6a05366a578466b663bcc7814f419c Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Fri, 11 Oct 2024 23:41:55 -0400 Subject: [PATCH 15/15] Rename Function From Sanitize To Func Signed-off-by: Mahad Zaryab --- .../internal/exporters/storageexporter/exporter.go | 2 +- cmd/jaeger/internal/sanitizer/emptyservicename.go | 2 +- cmd/jaeger/internal/sanitizer/sanitizer.go | 10 +++++----- cmd/jaeger/internal/sanitizer/sanitizer_test.go | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/jaeger/internal/exporters/storageexporter/exporter.go b/cmd/jaeger/internal/exporters/storageexporter/exporter.go index cbadc1fa16e..2cb7e8d1ba8 100644 --- a/cmd/jaeger/internal/exporters/storageexporter/exporter.go +++ b/cmd/jaeger/internal/exporters/storageexporter/exporter.go @@ -20,7 +20,7 @@ type storageExporter struct { config *Config logger *zap.Logger traceWriter spanstore.Writer - sanitizer sanitizer.Sanitize + sanitizer sanitizer.Func } func newExporter(config *Config, otel component.TelemetrySettings) *storageExporter { diff --git a/cmd/jaeger/internal/sanitizer/emptyservicename.go b/cmd/jaeger/internal/sanitizer/emptyservicename.go index d0ba8060f0a..65bdb3aa854 100644 --- a/cmd/jaeger/internal/sanitizer/emptyservicename.go +++ b/cmd/jaeger/internal/sanitizer/emptyservicename.go @@ -18,7 +18,7 @@ const ( // NewEmptyServiceNameSanitizer returns a sanitizer function that replaces // empty and missing service names with placeholder strings. -func NewEmptyServiceNameSanitizer() Sanitize { +func NewEmptyServiceNameSanitizer() Func { return sanitizeEmptyServiceName } diff --git a/cmd/jaeger/internal/sanitizer/sanitizer.go b/cmd/jaeger/internal/sanitizer/sanitizer.go index ba59910f374..1ac3fa9f579 100644 --- a/cmd/jaeger/internal/sanitizer/sanitizer.go +++ b/cmd/jaeger/internal/sanitizer/sanitizer.go @@ -7,20 +7,20 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" ) -// Sanitize is a function that performs enrichment, clean-up, or normalization of trace data. -type Sanitize func(traces ptrace.Traces) ptrace.Traces +// Func is a function that performs enrichment, clean-up, or normalization of trace data. +type Func func(traces ptrace.Traces) ptrace.Traces // NewStandardSanitizers returns a list of all the sanitizers that are used by the // storage exporter. -func NewStandardSanitizers() []Sanitize { - return []Sanitize{ +func NewStandardSanitizers() []Func { + return []Func{ NewEmptyServiceNameSanitizer(), } } // NewChainedSanitizer creates a Sanitizer from the variadic list of passed Sanitizers. // If the list only has one element, it is returned directly to minimize indirection. -func NewChainedSanitizer(sanitizers ...Sanitize) Sanitize { +func NewChainedSanitizer(sanitizers ...Func) Func { if len(sanitizers) == 1 { return sanitizers[0] } diff --git a/cmd/jaeger/internal/sanitizer/sanitizer_test.go b/cmd/jaeger/internal/sanitizer/sanitizer_test.go index aa592017f19..18399a28bc2 100644 --- a/cmd/jaeger/internal/sanitizer/sanitizer_test.go +++ b/cmd/jaeger/internal/sanitizer/sanitizer_test.go @@ -16,7 +16,7 @@ func TestNewStandardSanitizers(t *testing.T) { } func TestNewChainedSanitizer(t *testing.T) { - var s1 Sanitize = func(traces ptrace.Traces) ptrace.Traces { + var s1 Func = func(traces ptrace.Traces) ptrace.Traces { traces. ResourceSpans(). AppendEmpty(). @@ -25,7 +25,7 @@ func TestNewChainedSanitizer(t *testing.T) { PutStr("hello", "world") return traces } - var s2 Sanitize = func(traces ptrace.Traces) ptrace.Traces { + var s2 Func = func(traces ptrace.Traces) ptrace.Traces { traces. ResourceSpans(). At(0).