-
Notifications
You must be signed in to change notification settings - Fork 325
Add ContinueAsNew fresh-trace support for periodic orchestrations #1337
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
3216b55
c9c9684
718c560
29ddef8
26d5d37
20cd4c9
0e54bb3
48e7117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
| #nullable enable | ||
| namespace DurableTask.Core | ||
| { | ||
| /// <summary> | ||
| /// Configures how an orchestration continues as new. | ||
| /// </summary> | ||
| public sealed class ContinueAsNewOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets how distributed tracing should behave for the next generation. | ||
| /// The default is <see cref="ContinueAsNewTraceBehavior.PreserveTraceContext"/>, | ||
| /// which keeps the next generation in the same distributed trace. | ||
| /// </summary> | ||
| public ContinueAsNewTraceBehavior TraceBehavior { get; set; } = | ||
| ContinueAsNewTraceBehavior.PreserveTraceContext; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Describes how distributed tracing should behave for the next <c>ContinueAsNew</c> generation. | ||
| /// </summary> | ||
| public enum ContinueAsNewTraceBehavior | ||
| { | ||
| /// <summary> | ||
| /// Preserve the current trace lineage across generations. This is the default. | ||
| /// </summary> | ||
| PreserveTraceContext = 0, | ||
|
|
||
| /// <summary> | ||
| /// Start the next generation in a fresh distributed trace. Useful for long-running | ||
| /// periodic orchestrations where each cycle should be independently observable. | ||
| /// </summary> | ||
| StartNewTrace = 1, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ namespace DurableTask.Core | |
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
@@ -641,8 +642,20 @@ protected async Task<bool> OnProcessWorkItemAsync(TaskOrchestrationWorkItem work | |
| continueAsNewExecutionStarted!.Correlation = CorrelationTraceContext.Current.SerializableTraceContext; | ||
| }); | ||
|
|
||
| // Copy the distributed trace context, if any | ||
| continueAsNewExecutionStarted!.SetParentTraceContext(runtimeState.ExecutionStartedEvent); | ||
| // Copy the distributed trace context to preserve lineage, unless | ||
| // the next generation was explicitly requested to start a fresh trace. | ||
| if (!continueAsNewExecutionStarted!.GenerateNewTrace) | ||
| { | ||
| continueAsNewExecutionStarted.SetParentTraceContext(runtimeState.ExecutionStartedEvent); | ||
|
Comment on lines
+645
to
+649
|
||
| } | ||
| else | ||
| { | ||
| // Stamp the dispatcher processing time for this continuation request | ||
| // so the producer span created by TraceHelper uses this timestamp. | ||
| continueAsNewExecutionStarted.Tags ??= new Dictionary<string, string>(); | ||
| continueAsNewExecutionStarted.Tags[OrchestrationTags.RequestTime] = | ||
|
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. in this path, the RequestTime will be override everytime, so the value should be always accurate, but this might have a minor tag leak to the user. user will always see one extra tag set by the library |
||
| DateTimeOffset.UtcNow.ToString("O", CultureInfo.InvariantCulture); | ||
| } | ||
|
|
||
| runtimeState = new OrchestrationRuntimeState(); | ||
| runtimeState.AddEvent(new OrchestratorStartedEvent(-1)); | ||
|
|
@@ -1055,10 +1068,18 @@ internal static bool ReconcileMessagesWithState(TaskOrchestrationWorkItem workIt | |
| InstanceId = runtimeState.OrchestrationInstance!.InstanceId, | ||
| ExecutionId = Guid.NewGuid().ToString("N") | ||
| }, | ||
| Tags = runtimeState.Tags, | ||
| // Clone tags to avoid mutating the current generation's tag dictionary. | ||
| // Preserve the comparer if the underlying type is Dictionary<string, string>. | ||
| Tags = runtimeState.Tags == null | ||
| ? null | ||
| : runtimeState.Tags is Dictionary<string, string> dictionaryTags | ||
| ? new Dictionary<string, string>(dictionaryTags, dictionaryTags.Comparer) | ||
| : new Dictionary<string, string>(runtimeState.Tags), | ||
| ParentInstance = runtimeState.ParentInstance, | ||
| Name = runtimeState.Name, | ||
| Version = completeOrchestratorAction.NewVersion ?? runtimeState.Version | ||
| Version = completeOrchestratorAction.NewVersion ?? runtimeState.Version, | ||
| // Signal that the next generation should start a fresh distributed trace | ||
| GenerateNewTrace = completeOrchestratorAction.ContinueAsNewTraceBehavior == ContinueAsNewTraceBehavior.StartNewTrace, | ||
| }; | ||
|
|
||
| taskMessage.OrchestrationInstance = startedEvent.OrchestrationInstance; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.