-
Notifications
You must be signed in to change notification settings - Fork 1
Add --otel distributed tracing support #126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| namespace CosmosShell.Tests; | ||
|
|
||
| using System.Diagnostics; | ||
|
|
||
| using Azure.Data.Cosmos.Shell.Core; | ||
|
|
||
| using Xunit; | ||
|
|
||
| public class TracingBootstrapTests | ||
| { | ||
| [Fact] | ||
| public void StartCommandActivity_WithoutProvider_ReturnsNull() | ||
| { | ||
| using var activity = TracingBootstrap.StartCommandActivity("cosmosdbshell.command"); | ||
|
|
||
| Assert.Null(activity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StartCommandActivity_WhenInitialized_ProducesRecordedActivity() | ||
| { | ||
| using var tracing = TracingBootstrap.Initialize(otlpEndpoint: null); | ||
|
|
||
| using var activity = TracingBootstrap.StartCommandActivity("cosmosdbshell.command"); | ||
|
|
||
| Assert.NotNull(activity); | ||
| Assert.True(activity!.Recorded); | ||
| Assert.True(activity.ActivityTraceFlags.HasFlag(ActivityTraceFlags.Recorded)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Initialize_SetsAzureActivitySourceSwitch() | ||
| { | ||
| using var tracing = TracingBootstrap.Initialize(otlpEndpoint: null); | ||
|
|
||
| Assert.True( | ||
| AppContext.TryGetSwitch("Azure.Experimental.EnableActivitySource", out var enabled) && enabled); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/TracingBootstrap.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| namespace Azure.Data.Cosmos.Shell.Core; | ||
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Resources; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| /// <summary> | ||
| /// Owns the distributed-tracing lifecycle for the shell. When enabled it registers | ||
| /// a <see cref="TracerProvider"/> that records every activity, which causes the | ||
| /// Azure Cosmos DB SDK to emit a sampled W3C <c>traceparent</c> | ||
| /// (the <c>-01</c> flag) on its outgoing requests. An OTLP exporter is added only | ||
| /// when an endpoint is supplied. | ||
| /// </summary> | ||
| public sealed class TracingBootstrap : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Name of the <see cref="ActivitySource"/> used for per-command root activities. | ||
| /// </summary> | ||
| public const string ActivitySourceName = "CosmosDBShell"; | ||
|
|
||
| private const string CosmosOperationSourceName = "Azure.Cosmos.Operation"; | ||
|
|
||
| private static readonly ActivitySource SharedSource = new(ActivitySourceName); | ||
|
|
||
| private readonly TracerProvider provider; | ||
|
|
||
| private TracingBootstrap(TracerProvider provider) | ||
| { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Enables distributed tracing for the current process. Sets the Azure SDK | ||
| /// experimental switch required to emit <c>traceparent</c> headers and builds a | ||
| /// tracer provider that records all activities. | ||
| /// </summary> | ||
| /// <param name="otlpEndpoint">Optional OTLP endpoint to export spans to. When null or empty, no exporter is added and tracing only propagates a sampled <c>traceparent</c> on the wire.</param> | ||
| /// <returns>A <see cref="TracingBootstrap"/> that must be disposed to flush and tear down the provider.</returns> | ||
| public static TracingBootstrap Initialize(string? otlpEndpoint) | ||
| { | ||
| // Required so the Azure.Core HTTP pipeline writes a W3C traceparent header. | ||
| AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); | ||
|
|
||
| var builder = Sdk.CreateTracerProviderBuilder() | ||
| .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(ActivitySourceName)) | ||
| .SetSampler(new AlwaysOnSampler()) | ||
| .AddSource(CosmosOperationSourceName) | ||
| .AddSource(ActivitySourceName); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(otlpEndpoint)) | ||
| { | ||
| builder.AddOtlpExporter(options => options.Endpoint = new Uri(otlpEndpoint)); | ||
| } | ||
|
|
||
| return new TracingBootstrap(builder.Build()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts a root activity for a shell command. Returns null when tracing is not | ||
| /// enabled, so callers incur no overhead in the common case. | ||
| /// </summary> | ||
| /// <param name="name">The activity name.</param> | ||
| /// <returns>The started activity, or null when no tracer is listening.</returns> | ||
| public static Activity? StartCommandActivity(string name) | ||
| { | ||
| return SharedSource.StartActivity(name, ActivityKind.Client); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Dispose() | ||
| { | ||
| this.provider.Dispose(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.