Skip to content

Commit

Permalink
distro: Add WithIDGenerator option (#2634)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiopastan authored Nov 14, 2023
1 parent 7ef9bcf commit 88d29e9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Add

- Add the `WithIDGenerator` option to
`github.com/signalfx/splunk-otel-go/distro`. (#2634)

## [1.10.0] - 2023-11-10

This release upgrades [OpenTelemetry Go to v1.20.0/v0.43.0][otel-v1.20.0]
Expand Down
15 changes: 12 additions & 3 deletions distro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ type exporterConfig struct {

// config is the configuration used to create and operate an SDK.
type config struct {
Logger logr.Logger
Propagator propagation.TextMapPropagator
SpanLimits *trace.SpanLimits
Logger logr.Logger
Propagator propagation.TextMapPropagator
SpanLimits *trace.SpanLimits
IDGenerator trace.IDGenerator

ExportConfig *exporterConfig
TracesExporterFunc traceExporterFunc
Expand Down Expand Up @@ -147,3 +148,11 @@ func WithLogger(l logr.Logger) Option {
c.Logger = l
})
}

// WithIDGenerator configures the generator used to generate span and trace IDs.
// If this option is not provided, the SDK's default ID generator will be used.
func WithIDGenerator(g trace.IDGenerator) Option {
return optionFunc(func(c *config) {
c.IDGenerator = g
})
}
2 changes: 1 addition & 1 deletion distro/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0
go.opentelemetry.io/otel/sdk v1.20.0
go.opentelemetry.io/otel/sdk/metric v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
go.opentelemetry.io/proto/otlp v1.0.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.26.0
Expand All @@ -34,7 +35,6 @@ require (
go.opentelemetry.io/contrib/propagators/ot v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions distro/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func runTraces(c *config, res *resource.Resource) (shutdownFunc, error) {
trace.WithResource(res),
trace.WithRawSpanLimits(*c.SpanLimits),
trace.WithSpanProcessor(trace.NewBatchSpanProcessor(exp)),
trace.WithIDGenerator(c.IDGenerator),
}
if _, ok := os.LookupEnv(tracesSamplerKey); !ok {
o = append(o, trace.WithSampler(trace.AlwaysSample()))
Expand Down
31 changes: 31 additions & 0 deletions distro/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tonglil/buflogr"
"go.opentelemetry.io/otel"
otelt "go.opentelemetry.io/otel/trace"
cmpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
ctpb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
comm "go.opentelemetry.io/proto/otlp/common/v1"
Expand Down Expand Up @@ -341,6 +342,20 @@ func TestTracesResource(t *testing.T) {
assertResource(t, got.Resource.GetAttributes())
}

func TestWithIDGenerator(t *testing.T) {
coll := &collector{}
coll.Start(t)
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://"+coll.Endpoint)

emitSpan(t, distro.WithIDGenerator(&testIDGenerator{}))

got := coll.ExportedSpans()
require.NotNil(t, got)
assert.Contains(t, string(got.Spans[0].TraceId), "testtrace")
assert.Contains(t, string(got.Spans[0].SpanId), "testspan")
}

func TestRunOTLPMetricsExporter(t *testing.T) {
assertBase := func(t *testing.T, got *metricsExportRequest) {
assertHasMetric(t, got, metricName)
Expand Down Expand Up @@ -662,6 +677,8 @@ type (
Resource *rpb.Resource
Metrics []*mpb.Metric
}

testIDGenerator struct{}
)

func (coll *collector) Start(t *testing.T) {
Expand Down Expand Up @@ -759,3 +776,17 @@ func (cmss *collectorMetricsServiceServer) Export(ctx context.Context, exp *cmpb

return &cmpb.ExportMetricsServiceResponse{}, nil
}

func (g *testIDGenerator) NewSpanID(_ context.Context, _ otelt.TraceID) otelt.SpanID {
sid := otelt.SpanID{}
copy(sid[:], "testspan")
return sid
}

func (g *testIDGenerator) NewIDs(_ context.Context) (otelt.TraceID, otelt.SpanID) {
tid := otelt.TraceID{}
copy(tid[:], "testtrace")
sid := otelt.SpanID{}
copy(sid[:], "testspan")
return tid, sid
}

0 comments on commit 88d29e9

Please sign in to comment.