-
Notifications
You must be signed in to change notification settings - Fork 298
feat(otel): add otel tracing for standalone activities. #2302
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,10 @@ import ( | |
| "go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
| "go.opentelemetry.io/otel/trace" | ||
|
|
||
| "go.temporal.io/sdk/client" | ||
| "go.temporal.io/sdk/contrib/opentelemetry" | ||
| "go.temporal.io/sdk/interceptor" | ||
| "go.temporal.io/sdk/internal" | ||
| "go.temporal.io/sdk/internal/interceptortest" | ||
| "go.temporal.io/sdk/temporal" | ||
| "go.temporal.io/sdk/testsuite" | ||
|
|
@@ -35,6 +37,34 @@ func TestSpanPropagation(t *testing.T) { | |
| interceptortest.AssertSpanPropagation(t, testTracer) | ||
| } | ||
|
|
||
| func TestStandaloneActivitySpanCreation(t *testing.T) { | ||
| rec := tracetest.NewSpanRecorder() | ||
| tracer, err := opentelemetry.NewTracer(opentelemetry.TracerOptions{ | ||
| Tracer: sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(rec)).Tracer(""), | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| ctx := internal.NewHeaderContext(context.Background()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interceptor below writes to a header map expected to be on the context. If the map is missing it panics. To work around this, I went through a number of options:
|
||
| outbound := interceptor.NewTracingInterceptor(tracer).InterceptClient(&testNoopClientOutbound{}) | ||
| _, _ = outbound.ExecuteActivity(ctx, &interceptor.ClientExecuteActivityInput{ | ||
| ActivityType: "test-saa", | ||
| Options: &client.StartActivityOptions{ID: "test-saa-123"}, | ||
| }) | ||
|
|
||
| spans := rec.Ended() | ||
| require.Len(t, spans, 1) | ||
| assert.Equal(t, "StartActivity:test-saa", spans[0].Name()) | ||
|
|
||
| var foundActivityID bool | ||
| for _, attr := range spans[0].Attributes() { | ||
| if string(attr.Key) == "temporalActivityID" { | ||
| foundActivityID = true | ||
| assert.Equal(t, "test-saa-123", attr.Value.AsString()) | ||
| } | ||
| } | ||
| require.True(t, foundActivityID, "expected activity ID span attribute") | ||
| } | ||
|
|
||
| type testTracer struct { | ||
| interceptor.Tracer | ||
| rec *tracetest.SpanRecorder | ||
|
|
@@ -235,3 +265,11 @@ func TestSpanFromWorkflowContextNoOpSpan(t *testing.T) { | |
| require.True(t, env.IsWorkflowCompleted()) | ||
| require.NoError(t, env.GetWorkflowError()) | ||
| } | ||
|
|
||
| type testNoopClientOutbound struct { | ||
| interceptor.ClientOutboundInterceptorBase | ||
| } | ||
|
|
||
| func (n *testNoopClientOutbound) ExecuteActivity(_ context.Context, _ *interceptor.ClientExecuteActivityInput) (client.ActivityHandle, error) { | ||
| return nil, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -390,7 +390,25 @@ func processInternal(cfg config, file *os.File, pairs map[string]map[string]stri | |
| trimmedNextLine = nextLine | ||
| } | ||
|
|
||
| // Check for new doc links to add | ||
| // Track whether nextLine is inside a function or interface block. | ||
| // We update this first so the first line inside a block is not treated | ||
| // as a top-level definition. | ||
| if strings.HasPrefix(trimmedLine, "func ") { | ||
| funcSpaces = indentSize | ||
| inFunc = true | ||
| } else if inFunc && trimmedLine == "}" && funcSpaces == indentSize { | ||
| funcSpaces = -1 | ||
| inFunc = false | ||
| } | ||
| if strings.HasSuffix(trimmedLine, "interface {") { | ||
| interfaceSpaces = indentSize | ||
| inInterface = true | ||
| } else if inInterface && trimmedLine == "}" && interfaceSpaces == indentSize { | ||
| interfaceSpaces = -1 | ||
| inInterface = false | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial commits failed the doclink check because doclink was erroneously interpreting the first line of a top-level function as a top-level line. Without the fix: and running which is not correct. It read |
||
|
|
||
| // Check for new doc links to add on top-level definitions only. | ||
| if !inFunc && !inInterface && isValidDefinition(trimmedNextLine, &inGroup, &inStruct) { | ||
| // Find the "Exposed As" line in the doc comment | ||
| var existingDoclink string | ||
|
|
@@ -451,23 +469,6 @@ func processInternal(cfg config, file *os.File, pairs map[string]map[string]stri | |
| } | ||
| } | ||
|
|
||
| // update inFunc after we actually check for doclinks to allow us to check | ||
| // a function's definition, without checking anything inside the function | ||
| if strings.HasPrefix(trimmedLine, "func ") { | ||
| funcSpaces = indentSize | ||
| inFunc = true | ||
| } else if inFunc && trimmedLine == "}" && funcSpaces == indentSize { | ||
| funcSpaces = -1 | ||
| inFunc = false | ||
| } | ||
| if strings.HasSuffix(trimmedLine, "interface {") { | ||
| interfaceSpaces = indentSize | ||
| inInterface = true | ||
| } else if inInterface && trimmedLine == "}" && interfaceSpaces == indentSize { | ||
| interfaceSpaces = -1 | ||
| inInterface = false | ||
| } | ||
|
|
||
| newFile += line + "\n" | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -419,6 +419,7 @@ type ClientOutboundInterceptor interface { | |
| DescribeWorkflow(context.Context, *ClientDescribeWorkflowInput) (*ClientDescribeWorkflowOutput, error) | ||
|
|
||
| // ExecuteActivity intercepts client.Client.ExecuteActivity. | ||
| // interceptor.Header will return a non-nil map for this context. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consistent with other interceptor methods |
||
| // | ||
| // NOTE: Experimental | ||
| ExecuteActivity(context.Context, *ClientExecuteActivityInput) (ClientActivityHandle, error) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally named this
TestStandaloneActivitySpanPropagationbut I realized there is another test,TestSpanPropagation, that actually tests the propagation. We just care about the span creation here: an SAA should create a span with the correct name andtemporalActivityID.