-
Notifications
You must be signed in to change notification settings - Fork 56
feat(otel): add otel tracing for standalone activities. #647
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
Changes from all commits
23bdf63
116906f
df03b01
6c3d6a2
9997d21
65ceb90
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 |
|---|---|---|
|
|
@@ -231,6 +231,21 @@ protected virtual IDictionary<string, string> HeadersFromContext( | |
| return ret; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create tag collection for the given standalone activity ID. | ||
| /// </summary> | ||
| /// <param name="activityId">Standalone activity ID.</param> | ||
| /// <returns>Tags.</returns> | ||
| protected virtual IEnumerable<KeyValuePair<string, object?>> CreateStandaloneActivityTags( | ||
| string activityId) | ||
| { | ||
| if (Options.TagNameActivityId is string name) | ||
| { | ||
| return new KeyValuePair<string, object?>[] { new(name, activityId) }; | ||
| } | ||
| return Enumerable.Empty<KeyValuePair<string, object?>>(); | ||
| } | ||
|
|
||
| private static void RecordExceptionWithStatus(Activity? activity, Exception exception) | ||
| { | ||
| // If the exception is a benign exception, we do not consider the status an error. Note, | ||
|
|
@@ -394,6 +409,31 @@ public override async Task<WorkflowUpdateHandle<TResult>> StartWorkflowUpdateAsy | |
| } | ||
| } | ||
|
|
||
| public override async Task<ActivityHandle<TResult>> StartActivityAsync<TResult>( | ||
| StartActivityInput input) | ||
| { | ||
| using (var activity = ClientSource.StartActivity( | ||
| $"StartActivity:{input.Activity}", | ||
| kind: ActivityKind.Client, | ||
| parentContext: default, | ||
| tags: input.Options.Id is string id ? root.CreateStandaloneActivityTags(id) : null)) | ||
|
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.
Contributor
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. It shouldn't be |
||
| { | ||
| if (HeadersFromContext(input.Headers) is Dictionary<string, Payload> headers) | ||
| { | ||
| input = input with { Headers = headers }; | ||
| } | ||
| try | ||
| { | ||
| return await base.StartActivityAsync<TResult>(input).ConfigureAwait(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| RecordExceptionWithStatus(activity, e); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serialize current context to headers if one exists. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -797,6 +797,27 @@ public async Task TracingInterceptor_Nexus_HasTracing() | |
| "RunWorkflow:TracingWorkflow")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TracingInterceptor_StandaloneActivity_HasProperSpans() | ||
| { | ||
| var activityId = $"act-{Guid.NewGuid()}"; | ||
| var spans = await WithTracingWorkerAsync(async (client, _) => | ||
| { | ||
| await client.StartActivityAsync( | ||
| "TestStandaloneActivitySpans", | ||
| Array.Empty<object?>(), | ||
| new(activityId, $"standalone-tq-{Guid.NewGuid()}") | ||
| { | ||
| ScheduleToCloseTimeout = TimeSpan.FromMinutes(5), | ||
| }); | ||
| }); | ||
|
|
||
| var activityTags = new[] { ActivityAssertion.TagEqual("temporalActivityID", activityId) }; | ||
| AssertActivities( | ||
| spans, | ||
| new ActivityAssertion("StartActivity:TestStandaloneActivitySpans", Parent: null, Tags: activityTags)); | ||
| } | ||
|
|
||
| private static void AssertActivities( | ||
| IReadOnlyCollection<Activity> activities, params ActivityAssertion[] assertions) | ||
| { | ||
|
|
@@ -886,7 +907,7 @@ private async Task<IReadOnlyCollection<Activity>> WithTracingWorkerAsync( | |
| var activities = new List<Activity>(); | ||
|
|
||
| // Setup provider | ||
| using var tracerProvider = global::OpenTelemetry.Sdk.CreateTracerProviderBuilder(). | ||
| using var _ = global::OpenTelemetry.Sdk.CreateTracerProviderBuilder(). | ||
|
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. Took me a second to realize that |
||
| AddSource( | ||
| TracingInterceptor.ClientSource.Name, | ||
| TracingInterceptor.WorkflowsSource.Name, | ||
|
|
||
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 could just inline this logic now that start_activity is the only event we are adding tracing to.
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 think it's fine to leave here as-is. There's precedent for this pattern.