Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ require (
github.com/go-coldbrew/options v0.2.6
github.com/google/uuid v1.6.0
github.com/rollbar/rollbar-go v1.4.8
go.opentelemetry.io/otel v1.42.0
go.opentelemetry.io/otel/trace v1.42.0
go.opentelemetry.io/otel v1.43.0
go.opentelemetry.io/otel/sdk v1.43.0
go.opentelemetry.io/otel/trace v1.43.0
google.golang.org/grpc v1.79.3
)

Expand Down Expand Up @@ -88,6 +89,8 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.8.0 // indirect
github.com/go-git/go-git/v5 v5.17.1 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
github.com/go-toolsmith/astcopy v1.1.0 // indirect
github.com/go-toolsmith/astequal v1.2.0 // indirect
Expand Down Expand Up @@ -235,6 +238,8 @@ require (
go-simpler.org/sloglint v0.11.1 // indirect
go.augendre.info/arangolint v0.4.0 // indirect
go.augendre.info/fatcontext v0.9.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/metric v1.43.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
Expand Down
19 changes: 15 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,11 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logfmt/logfmt v0.6.1 h1:4hvbpePJKnIzH1B+8OR/JPbTx37NktoI9LE2QZBBkvE=
github.com/go-logfmt/logfmt v0.6.1/go.mod h1:EV2pOAQoZaT1ZXZbqDl5hrymndi4SY9ED9/z6CO0XAk=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -754,10 +757,18 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opentelemetry.io/otel v1.42.0 h1:lSQGzTgVR3+sgJDAU/7/ZMjN9Z+vUip7leaqBKy4sho=
go.opentelemetry.io/otel v1.42.0/go.mod h1:lJNsdRMxCUIWuMlVJWzecSMuNjE7dOYyWlqOXWkdqCc=
go.opentelemetry.io/otel/trace v1.42.0 h1:OUCgIPt+mzOnaUTpOQcBiM/PLQ/Op7oq6g4LenLmOYY=
go.opentelemetry.io/otel/trace v1.42.0/go.mod h1:f3K9S+IFqnumBkKhRJMeaZeNk9epyhnCmQh/EysQCdc=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
106 changes: 106 additions & 0 deletions notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

"github.com/go-coldbrew/errors"
"github.com/go-coldbrew/options"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"google.golang.org/grpc/metadata"
)

func TestGetTraceId_NonStringValue(t *testing.T) {
Expand Down Expand Up @@ -75,3 +79,105 @@ func TestSetMaxAsyncNotifications_ConcurrentAccess(t *testing.T) {
}()
wg.Wait()
}

func setupTestTracer(t *testing.T) *tracetest.InMemoryExporter {
t.Helper()
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
old := otel.GetTracerProvider()
otel.SetTracerProvider(tp)
t.Cleanup(func() {
otel.SetTracerProvider(old)
tp.Shutdown(context.Background())
})
Comment thread
ankurs marked this conversation as resolved.
return exporter
}

func TestSetTraceId_SetsOTELAttribute(t *testing.T) {
exporter := setupTestTracer(t)
ctx, span := otel.Tracer("test").Start(context.Background(), "test-span")
Comment thread
ankurs marked this conversation as resolved.

ctx = SetTraceId(ctx)
expectedTraceID := GetTraceId(ctx)
span.End()

Comment thread
ankurs marked this conversation as resolved.
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
found := false
for _, attr := range spans[0].Attributes {
if string(attr.Key) == "coldbrew.trace_id" {
if attr.Value.AsString() != expectedTraceID {
t.Errorf("coldbrew.trace_id = %q, want %q", attr.Value.AsString(), expectedTraceID)
}
found = true
}
}
if !found {
t.Error("coldbrew.trace_id attribute not found on span")
}
}
Comment thread
ankurs marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func TestSetTraceId_EarlyReturn_SetsOTELAttribute(t *testing.T) {
exporter := setupTestTracer(t)
ctx, span := otel.Tracer("test").Start(context.Background(), "test-span")

// Pre-set trace ID so SetTraceId takes the early return path
ctx = options.AddToOptions(ctx, tracerID, "pre-existing-id")
SetTraceId(ctx)
Comment thread
ankurs marked this conversation as resolved.
Outdated
span.End()

Comment thread
ankurs marked this conversation as resolved.
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
found := false
for _, attr := range spans[0].Attributes {
if string(attr.Key) == "coldbrew.trace_id" && attr.Value.AsString() == "pre-existing-id" {
found = true
}
}
if !found {
t.Error("coldbrew.trace_id should be 'pre-existing-id' even on early return")
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func TestSetTraceId_MetadataPriority(t *testing.T) {
exporter := setupTestTracer(t)
ctx, span := otel.Tracer("test").Start(context.Background(), "test-span")

// Inject gRPC metadata with a trace header — should take priority over OTEL span trace ID
md := metadata.Pairs(traceHeader, "metadata-trace-id-123")
ctx = metadata.NewIncomingContext(ctx, md)

ctx = SetTraceId(ctx)
traceID := GetTraceId(ctx)
span.End()

if traceID != "metadata-trace-id-123" {
t.Errorf("expected metadata trace ID 'metadata-trace-id-123', got %q", traceID)
}

// Verify the attribute on the span matches the metadata trace ID
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
found := false
for _, attr := range spans[0].Attributes {
if string(attr.Key) == "coldbrew.trace_id" && attr.Value.AsString() == "metadata-trace-id-123" {
found = true
}
}
if !found {
t.Error("coldbrew.trace_id should be the metadata-supplied trace ID, not OTEL span trace ID")
}
}

func TestSetTraceId_NoSpan_NoPanic(t *testing.T) {
ctx := SetTraceId(context.Background())
if GetTraceId(ctx) == "" {
t.Error("expected a generated trace ID")
}
}
Loading