Skip to content

Commit

Permalink
[Internal] AI Integration: Refactors to Operation prefix and add tests (
Browse files Browse the repository at this point in the history
#3583)

* add tests for otel and custome listener

* clean up

* null pointer fix

* fix tets

* handle event generation also at operation level

* added documentation

* wip

* change event sourcename

* rename event Name

Co-authored-by: Sourabh Jain <sourabhjain@microsoft.com>
  • Loading branch information
sourabh1007 and sourabh1007 committed Dec 5, 2022
1 parent 28318b0 commit a34bac7
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using global::Azure.Core.Diagnostics;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;

/// <summary>
/// This class is used to generate events with Azure.Cosmos.Operation Source Name
/// </summary>
[EventSource(Name = EventSourceName)]
internal sealed class CosmosDbEventSource : AzureEventSource
{
private const string EventSourceName = OpenTelemetryAttributeKeys.DiagnosticNamespace;
internal const string EventSourceName = "Azure-Cosmos-Operation-Request-Diagnostics";

private static CosmosDbEventSource Singleton { get; } = new CosmosDbEventSource();

private CosmosDbEventSource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class OpenTelemetryAttributeKeys
// Azure defaults
public const string DiagnosticNamespace = "Azure.Cosmos";
public const string ResourceProviderNamespace = "Microsoft.DocumentDB";
public const string OperationPrefix = "Cosmos";
public const string OperationPrefix = "Operation";

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

/// <summary>
/// This class is used to add information in an Activity tags ref. https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058
/// </summary>
internal struct OpenTelemetryCoreRecorder : IDisposable
{
private const string CosmosDb = "cosmosdb";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ namespace Microsoft.Azure.Cosmos.Telemetry
{
using global::Azure.Core.Pipeline;

/// <summary>
/// This class is used to generate Activities with Azure.Cosmos.Operation Source Name
/// </summary>
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; }

public static OpenTelemetryCoreRecorder CreateRecorder(string operationName,
Expand All @@ -22,11 +28,14 @@ public static OpenTelemetryCoreRecorder CreateRecorder(string operationName,
ScopeFactory = new DiagnosticScopeFactory(clientNamespace: OpenTelemetryAttributeKeys.DiagnosticNamespace,
resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace,
isActivityEnabled: true);

// If there is no source then it will return default otherwise a valid diagnostic scope
DiagnosticScope scope = OpenTelemetryRecorderFactory
.ScopeFactory
.CreateScope(name: $"{OpenTelemetryAttributeKeys.OperationPrefix}.{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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
}
]
}]]></Json>
<OTelActivities><ACTIVITY><OPERATION>Cosmos.ExecuteAsync</OPERATION><ATTRIBUTE-KEY>kind</ATTRIBUTE-KEY><ATTRIBUTE-KEY>az.namespace</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.operation</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.name</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.container</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.operation_type</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.system</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.machine_id</ATTRIBUTE-KEY><ATTRIBUTE-KEY>net.peer.name</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.client_id</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.user_agent</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.connection_mode</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.request_content_length_bytes</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.response_content_length_bytes</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.status_code</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.sub_status_code</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.request_charge</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.item_count</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.regions_contacted</ATTRIBUTE-KEY></ACTIVITY>
<OTelActivities><ACTIVITY><OPERATION>Operation.ExecuteAsync</OPERATION><ATTRIBUTE-KEY>kind</ATTRIBUTE-KEY><ATTRIBUTE-KEY>az.namespace</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.operation</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.name</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.container</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.operation_type</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.system</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.machine_id</ATTRIBUTE-KEY><ATTRIBUTE-KEY>net.peer.name</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.client_id</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.user_agent</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.connection_mode</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.request_content_length_bytes</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.response_content_length_bytes</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.status_code</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.sub_status_code</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.request_charge</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.item_count</ATTRIBUTE-KEY><ATTRIBUTE-KEY>db.cosmosdb.regions_contacted</ATTRIBUTE-KEY></ACTIVITY>
<EVENT>Ideally, this should contain request diagnostics but request diagnostics is subject to change with each request as it contains few unique id. So just putting this tag with this static text to make sure event is getting generated for each test.</EVENT>
</OTelActivities>
</Output>
Expand Down
Loading

0 comments on commit a34bac7

Please sign in to comment.