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

[Internal] OpenTelemetry : Adds Telemetry Distributed Tracing functionality #3801

Merged
69 commits merged into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
54bb621
Direct Package update and replacing dagnostic files
aavasthy Apr 4, 2023
3075125
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 4, 2023
b6da31d
Resolve merge conflicts
aavasthy Apr 5, 2023
0f0bfc5
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 5, 2023
ae93a46
Running updateCOnstracts script
aavasthy Apr 5, 2023
a8a946b
Add code changes for distributed tracing open telemetry changes
aavasthy Apr 5, 2023
c70d50e
Add distributed tracing tests
aavasthy Apr 5, 2023
c40ab53
Updated tests for distributed tracing
aavasthy Apr 5, 2023
158a767
Addin traceID for diagnostics
aavasthy Apr 5, 2023
8d149ad
Running update contract script
aavasthy Apr 5, 2023
7ec2a89
Removed LinqTranslationWithCustomSerializerBaseline file
aavasthy Apr 6, 2023
217943c
Adding isDistributedTracingEnabled flag
aavasthy Apr 6, 2023
05f4b23
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 7, 2023
ff297e9
Running update contracts
aavasthy Apr 9, 2023
f09f6e1
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 9, 2023
d67debd
Running update contracts
aavasthy Apr 9, 2023
44de8c4
Merge branch 'users/aavasthy/distributedTracing_directPackageUpdate' …
aavasthy Apr 9, 2023
89a8673
Updates based on differnt code review comments
aavasthy Apr 9, 2023
7ea6e90
Running update contracts
aavasthy Apr 10, 2023
c3f3aca
Running update contracts
aavasthy Apr 10, 2023
5c791cf
Running update contracts
aavasthy Apr 10, 2023
1c5c76c
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 10, 2023
378f506
Merge branch 'users/aavasthy/distributedTracing_directPackageUpdate' …
aavasthy Apr 11, 2023
d940193
Merge branch 'master' into users/aavasthy/distributedTracing_directPa…
aavasthy Apr 11, 2023
fed263e
fix test
sourabh1007 Apr 12, 2023
d6866eb
Merge branch 'users/aavasthy/distributedTracing_directPackageUpdate' …
aavasthy Apr 14, 2023
1078328
Code cleanup for test fix
aavasthy Apr 14, 2023
ce3ccb6
Running Update contracts
aavasthy Apr 14, 2023
c2f9ac8
resolving merge conflicts
aavasthy Apr 14, 2023
b202fc2
Merge branch 'users/aavasthy/distributedTracing_directPackageUpdate' …
aavasthy Apr 14, 2023
5365819
resolving merge conflicts
aavasthy Apr 14, 2023
b12cafc
Resolving merge conflicts
aavasthy Apr 18, 2023
5b461ff
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy Apr 18, 2023
4abf4e4
Set EnableDistributedTracing to true for performance tests
aavasthy Apr 18, 2023
12b646e
Benchmark project change for distributed tracing
aavasthy Apr 19, 2023
4f9f71c
Updating tests
aavasthy Apr 19, 2023
c6528f4
Updated unit tests
aavasthy Apr 21, 2023
3d11d6c
Updated unit tests
aavasthy Apr 21, 2023
8ae7732
Updated tests and constructors based on review comments
aavasthy Apr 24, 2023
d357ea7
Updated scope name in recorder
aavasthy Apr 24, 2023
2837201
Updated distributedOtel tests to cover more scenarios
aavasthy Apr 24, 2023
6e78647
Updated distributedOtel tests
aavasthy Apr 25, 2023
dba6b6b
Reverting benchmark performance test changes
aavasthy Apr 25, 2023
ccf8590
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy Apr 25, 2023
4984f06
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy Apr 25, 2023
dfd4407
Update DistributedOpentelemetry tests
aavasthy May 9, 2023
ff4b9af
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 9, 2023
8c8491e
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 11, 2023
03c8d56
Update test cleanup
aavasthy May 14, 2023
8351a61
Merge branch 'users/aavasthy/distributedTracing_opentelemetry' of htt…
aavasthy May 14, 2023
8d953e0
Update distributed tests with custom builder
aavasthy May 17, 2023
d10b1c1
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 17, 2023
948af5f
Update distributed open telemetry tests
aavasthy May 23, 2023
93a372b
Merge branch 'users/aavasthy/distributedTracing_opentelemetry' of htt…
aavasthy May 23, 2023
8a10bbc
Update contracts
aavasthy May 24, 2023
be88b35
Resolved merge conflicts with master
aavasthy May 24, 2023
8539837
Cleanup files
aavasthy May 24, 2023
6abf5c4
Update distributed Otel tests
aavasthy May 25, 2023
9caa2f9
Update distributed Otel tests
aavasthy May 25, 2023
a852d07
code refactoring
sourabh1007 May 25, 2023
6c01ea5
fix custom listener
sourabh1007 May 25, 2023
358170a
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 25, 2023
965833e
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 29, 2023
c217a21
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy May 30, 2023
14c8582
Update direct package to 3.31.1
aavasthy Jun 6, 2023
c49b1a1
Resolve merge conflicts
aavasthy Jun 6, 2023
3124ce8
Code clean up
aavasthy Jun 6, 2023
92cf62a
Update tests with display name
aavasthy Jun 6, 2023
63ae9df
Merge branch 'master' into users/aavasthy/distributedTracing_opentele…
aavasthy Jun 7, 2023
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ClientOfficialVersion>3.34.0</ClientOfficialVersion>
<ClientPreviewVersion>3.34.0</ClientPreviewVersion>
<ClientPreviewSuffixVersion>preview</ClientPreviewSuffixVersion>
<DirectVersion>3.31.0</DirectVersion>
<DirectVersion>3.31.1</DirectVersion>
<EncryptionOfficialVersion>2.0.2</EncryptionOfficialVersion>
<EncryptionPreviewVersion>2.0.2</EncryptionPreviewVersion>
<EncryptionPreviewSuffixVersion>preview</EncryptionPreviewSuffixVersion>
Expand Down
1 change: 1 addition & 0 deletions Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ private async Task<TResult> RunWithDiagnosticsHelperAsync<TResult>(
databaseName: databaseName,
operationType: operationType,
requestOptions: requestOptions,
trace: trace,
sourabh1007 marked this conversation as resolved.
Show resolved Hide resolved
clientContext: this.isDisposed ? null : this))
using (new ActivityScope(Guid.NewGuid()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed class OpenTelemetryAttributeKeys
public const string DiagnosticNamespace = "Azure.Cosmos";
public const string ResourceProviderNamespace = "Microsoft.DocumentDB";
public const string OperationPrefix = "Operation";
public const string NetworkLevelPrefix = "Request";
aavasthy marked this conversation as resolved.
Show resolved Hide resolved

// Common database attributes
public const string DbSystemName = "db.system";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using global::Azure.Core;

/// <summary>
Expand All @@ -14,11 +15,12 @@ namespace Microsoft.Azure.Cosmos.Telemetry
internal struct OpenTelemetryCoreRecorder : IDisposable
{
private const string CosmosDb = "cosmosdb";

private readonly DiagnosticScope scope;
private readonly DistributedTracingOptions config;

private readonly Documents.OperationType operationType;
private readonly DiagnosticScope scope = default;
private readonly DistributedTracingOptions config = null;
private readonly Activity activity = null;

private readonly Documents.OperationType operationType = Documents.OperationType.Invalid;
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
private OpenTelemetryAttributes response = null;

internal static IDictionary<Type, Action<Exception, DiagnosticScope>> OTelCompatibleExceptions = new Dictionary<Type, Action<Exception, DiagnosticScope>>()
Expand All @@ -30,7 +32,19 @@ internal struct OpenTelemetryCoreRecorder : IDisposable
{ typeof(ChangeFeedProcessorUserException), (exception, scope) => ChangeFeedProcessorUserException.RecordOtelAttributes((ChangeFeedProcessorUserException)exception, scope)}
};

public OpenTelemetryCoreRecorder(
private OpenTelemetryCoreRecorder(DiagnosticScope scope)
{
this.scope = scope;
this.scope.Start();
}

private OpenTelemetryCoreRecorder(string operationName)
{
this.activity = new Activity(operationName);
ealsur marked this conversation as resolved.
Show resolved Hide resolved
this.activity.Start();
}

private OpenTelemetryCoreRecorder(
DiagnosticScope scope,
string operationName,
string containerName,
Expand All @@ -54,6 +68,45 @@ public OpenTelemetryCoreRecorder(
}
}

/// <summary>
/// Used for creating parent activity in scenario where there are no listeners at operation level
/// but they are present at network level
/// </summary>
public static OpenTelemetryCoreRecorder CreateNetworkLevelParentActivity(DiagnosticScope networkScope)
{
return new OpenTelemetryCoreRecorder(networkScope);
}

/// <summary>
/// Used for creating parent activity in scenario where there are no listeners at operation level and network level
/// </summary>
public static OpenTelemetryCoreRecorder CreateParentActivity(string operationName)
{
return new OpenTelemetryCoreRecorder(operationName);
}

/// <summary>
/// Used for creating parent activity in scenario where there are listeners at operation level
ealsur marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static OpenTelemetryCoreRecorder CreateOperationLevelParentActivity(
DiagnosticScope operationScope,
string operationName,
string containerName,
string databaseName,
Documents.OperationType operationType,
CosmosClientContext clientContext,
DistributedTracingOptions config)
{
return new OpenTelemetryCoreRecorder(
ealsur marked this conversation as resolved.
Show resolved Hide resolved
operationScope,
operationName,
containerName,
databaseName,
operationType,
clientContext,
config);
}

public bool IsEnabled => this.scope.IsEnabled;

public void Record(string key, string value)
Expand Down Expand Up @@ -179,6 +232,10 @@ Documents.OperationType operationType

this.scope.Dispose();
}
else
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
{
this.activity?.Stop();
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using System.Diagnostics;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Tracing;

/// <summary>
/// This class is used to generate Activities with Azure.Cosmos.Operation Source Name
Expand All @@ -15,43 +17,60 @@ internal static class OpenTelemetryRecorderFactory
/// <summary>
/// Singleton to make sure we only have one instance of the DiagnosticScopeFactory and pattern matching of listener happens only once
/// </summary>
private static DiagnosticScopeFactory ScopeFactory { get; set; }

private static readonly Lazy<DiagnosticScopeFactory> LazyOperationScopeFactory = new Lazy<DiagnosticScopeFactory>(
valueFactory: () => new DiagnosticScopeFactory(
clientNamespace: $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.{OpenTelemetryAttributeKeys.OperationPrefix}",
resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace,
isActivityEnabled: true,
suppressNestedClientActivities: true),
isThreadSafe: true);

private static readonly Lazy<DiagnosticScopeFactory> LazyNetworkScopeFactory = new Lazy<DiagnosticScopeFactory>(
valueFactory: () => new DiagnosticScopeFactory(
clientNamespace: $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.{OpenTelemetryAttributeKeys.NetworkLevelPrefix}",
resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace,
isActivityEnabled: true,
suppressNestedClientActivities: true),
isThreadSafe: true);

public static OpenTelemetryCoreRecorder CreateRecorder(string operationName,
string containerName,
string databaseName,
Documents.OperationType operationType,
RequestOptions requestOptions,
ITrace trace,
CosmosClientContext clientContext)
{
OpenTelemetryCoreRecorder openTelemetryRecorder = default;
if (clientContext is { ClientOptions.IsDistributedTracingEnabled: true })
{
OpenTelemetryRecorderFactory.ScopeFactory ??= new DiagnosticScopeFactory(clientNamespace: $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.{OpenTelemetryAttributeKeys.OperationPrefix}",
resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace,
isActivityEnabled: true,
suppressNestedClientActivities: true);

// If there is no source then it will return default otherwise a valid diagnostic scope
DiagnosticScope scope = OpenTelemetryRecorderFactory
.ScopeFactory
.CreateScope(name: operationName,
DiagnosticScope scope = LazyOperationScopeFactory.Value.CreateScope(name: operationName,
kind: clientContext.ClientOptions.ConnectionMode == ConnectionMode.Gateway ? DiagnosticScope.ActivityKind.Internal : DiagnosticScope.ActivityKind.Client);

// Record values only when we have a valid Diagnostic Scope
if (scope.IsEnabled)
{
return new OpenTelemetryCoreRecorder(
scope: scope,
openTelemetryRecorder = OpenTelemetryCoreRecorder.CreateOperationLevelParentActivity(
operationScope: scope,
operationName: operationName,
containerName: containerName,
databaseName: databaseName,
operationType: operationType,
clientContext: clientContext,
config: requestOptions?.DistributedTracingOptions ?? clientContext.ClientOptions?.DistributedTracingOptions);
}
}
#if !INTERNAL
else if (Activity.Current is null)
{
DiagnosticScope requestScope = LazyNetworkScopeFactory.Value.CreateScope(name: operationName);

return default;
openTelemetryRecorder = requestScope.IsEnabled ? OpenTelemetryCoreRecorder.CreateNetworkLevelParentActivity(networkScope: requestScope) : OpenTelemetryCoreRecorder.CreateParentActivity(operationName);
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
trace.AddDatum("DistributedTraceId", Activity.Current?.TraceId);
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
}
return openTelemetryRecorder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
│ (
│ [Client Configuration]
│ Redacted To Not Change The Baselines From Run To Run
│ [DistributedTraceId]
│ Redacted To Not Change The Baselines From Run To Run
│ )
└── Execute Next Batch(00000000-0000-0000-0000-000000000000) Batch-Component 00:00:00:000 0.00 milliseconds
├── Create Batch Request(00000000-0000-0000-0000-000000000000) Batch-Component 00:00:00:000 0.00 milliseconds
Expand All @@ -64,7 +66,8 @@
"start datetime": "0001-01-01T00:00:00Z",
"duration in milliseconds": 0,
"data": {
"Client Configuration": "Redacted To Not Change The Baselines From Run To Run"
"Client Configuration": "Redacted To Not Change The Baselines From Run To Run",
"DistributedTraceId": "Redacted To Not Change The Baselines From Run To Run"
},
"children": [
{
Expand Down
Loading