Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TraceID and SpanID Field Names Configurable in OpenTelemetry Integration #1734

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 18 additions & 2 deletions contrib/opentelemetry/tracing_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ type TracerOptions struct {
// SpanStarter is a callback to create spans. If not set, this creates normal
// OpenTelemetry spans calling Tracer.Start.
SpanStarter func(ctx context.Context, t trace.Tracer, spanName string, opts ...trace.SpanStartOption) trace.Span

// TraceIDFieldName is the name of the field used to represent the Trace ID
// If empty, a default name will be used.
TraceIDFieldName string

// SpanIDFieldName is the name of the field used to represent the Span ID
// If empty, a default name will be used.
SpanIDFieldName string
Comment on lines +90 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TraceIDFieldName is the name of the field used to represent the Trace ID
// If empty, a default name will be used.
TraceIDFieldName string
// SpanIDFieldName is the name of the field used to represent the Span ID
// If empty, a default name will be used.
SpanIDFieldName string
// TraceIDLoggerFieldName is the name of the field used to represent the Trace ID in logger.
// If empty, a default name will be used.
TraceIDLoggerFieldName string
// SpanIDLoggerFieldName is the name of the field used to represent the Span ID in logger.
// If empty, a default name will be used.
SpanIDLoggerFieldName string

Not clear this is only for logger and nothing else

}

type spanContextKey struct{}
Expand Down Expand Up @@ -123,6 +131,14 @@ func NewTracer(options TracerOptions) (interceptor.Tracer, error) {
return span
}
}
// Ensure TraceIDFieldName and SpanIDFieldName have defaults if fields are empty
if options.TraceIDFieldName == "" {
options.TraceIDFieldName = "TraceID"
}
if options.SpanIDFieldName == "" {
options.SpanIDFieldName = "SpanID"
}

return &tracer{options: &options}, nil
}

Expand Down Expand Up @@ -240,8 +256,8 @@ func (t *tracer) GetLogger(logger log.Logger, ref interceptor.TracerSpanRef) log
}

logger = log.With(logger,
"TraceID", span.SpanContext().TraceID(),
"SpanID", span.SpanContext().SpanID(),
t.options.TraceIDFieldName, span.SpanContext().TraceID(),
t.options.SpanIDFieldName, span.SpanContext().SpanID(),
)

return logger
Expand Down
41 changes: 41 additions & 0 deletions contrib/opentelemetry/tracing_interceptor_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,47 @@ func TestLogFields(t *testing.T) {
assert.Contains(t, buf.String(), "SpanID="+span.Parent().SpanID().String())
}

func TestLogWithCustomFieldNames(t *testing.T) {
var rec tracetest.SpanRecorder

tracer, err := opentelemetry.NewTracer(opentelemetry.TracerOptions{
Tracer: sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(&rec)).Tracer(""),
TraceIDFieldName: "trace_id",
SpanIDFieldName: "span_id",
})
require.NoError(t, err)

var suite testsuite.WorkflowTestSuite

buf := bytes.Buffer{}
slogger := slog.New(slog.NewTextHandler(&buf, nil))
suite.SetLogger(slogger)

env := suite.NewTestWorkflowEnvironment()
env.RegisterActivity(testActivity)
env.RegisterWorkflow(testWorkflow)

// Set tracer interceptor
env.SetWorkerOptions(worker.Options{
Interceptors: []interceptor.WorkerInterceptor{interceptor.NewTracingInterceptor(tracer)},
})

var testWorkflowStartTime = time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC)
env.SetStartTime(testWorkflowStartTime)

// Exec
env.ExecuteWorkflow(testWorkflow)

// Ensure it doesn't introduce panic or else
require.True(t, env.IsWorkflowCompleted())
require.NoError(t, env.GetWorkflowError())

// Validate that the fields are present and properly set.
span := rec.Ended()[0]
assert.Contains(t, buf.String(), "trace_id="+span.Parent().TraceID().String())
assert.Contains(t, buf.String(), "span_id="+span.Parent().SpanID().String())
}

func testWorkflow(ctx workflow.Context) error {
logger := workflow.GetLogger(ctx)
logger.Info("inside a worflow")
Expand Down
Loading