Skip to content

Commit

Permalink
Cherry-Pick Appinsights: Adds classic attribute back to cosmos db to …
Browse files Browse the repository at this point in the history
…support appinsights sdk (#4781) (#4799) (#4804)

## Description

As part of this PR, adding back attributes required by appinsights sdk.
It broke the customer experience with appinsight as Appinsight SDK
supports very specific set of attributes.

This implementation will change in future release, where open telemetry
attributes will be controlled by Env Variable as mentioned here.



https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#semantic-conventions-for-database-client-calls

**4.42.3**



![image](https://github.com/user-attachments/assets/6c19eab0-4884-4648-b6d9-c941d58e8e0b)

**v4.43.0**



![image](https://github.com/user-attachments/assets/fd2a70b4-ed37-4618-b1a5-f654587bf6ab)

**v4.43.1**



![image](https://github.com/user-attachments/assets/5bbe54ec-34f3-4538-a7b4-abff1b1853ea)

After this PR:



![image](https://github.com/user-attachments/assets/2ea64a34-4e57-4dec-a29e-32ffcd77919a)

After this change, customer has to set `OTEL_SEMCONV_STABILITY_OPT_IN`
to `database/dup`, in order to see the otel (new and old) attributes.
otherwise SDK will emit only classic attributes which would be
compatible with appinsights sdk also.

## Type of change
- [] Bug fix (non-breaking change which fixes an issue)

# Pull Request Template

## Description

Please include a summary of the change and which issue is fixed. Include
samples if adding new API, and include relevant motivation and context.
List any dependencies that are required for this change.

## Type of change

Please delete options that are not relevant.

- [] Bug fix (non-breaking change which fixes an issue)
- [] New feature (non-breaking change which adds functionality)
- [] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [] This change requires a documentation update

## Closing issues

To automatically close an issue: closes #IssueNumber

Co-authored-by: Kiran Kumar Kolli <kirankk@microsoft.com>
  • Loading branch information
sourabh1007 and kirankumarkolli authored Oct 15, 2024
1 parent 307be70 commit 9055f93
Show file tree
Hide file tree
Showing 14 changed files with 1,087 additions and 944 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Telemetry
{
internal sealed class AppInsightClassicAttributeKeys
{
/// <summary>
/// Represents the diagnostic namespace for Azure Cosmos.
/// </summary>
public const string DbName = "db.name";

/// <summary>
/// Represents the name of the database operation.
/// </summary>
public const string DbOperation = "db.operation";

/// <summary>
/// Represents the server address.
/// </summary>
public const string ServerAddress = "net.peer.name";

/// <summary>
/// Represents the name of the container in Cosmos DB.
/// </summary>
public const string ContainerName = "db.cosmosdb.container";

/// <summary>
/// Represents the status code of the response.
/// </summary>
public const string StatusCode = "db.cosmosdb.status_code";

/// <summary>
/// Represents the user agent
/// </summary>
public const string UserAgent = "db.cosmosdb.user_agent";

/// <summary>
/// Represents the machine ID for Cosmos DB.
/// </summary>
public const string MachineId = "db.cosmosdb.machine_id";

/// <summary>
/// Represents the type of operation for Cosmos DB.
/// </summary>
public const string OperationType = "db.cosmosdb.operation_type";

/// <summary>
/// Represents the sub-status code of the response.
/// </summary>
public const string SubStatusCode = "db.cosmosdb.sub_status_code";

/// <summary>
/// Represents the content length of the response.
/// </summary>
public const string ResponseContentLength = "db.cosmosdb.response_content_length_bytes";

/// <summary>
/// Represents the client ID for Cosmos DB.
/// </summary>
public const string ClientId = "db.cosmosdb.client_id";

/// <summary>
/// Represents the request charge for the operation.
/// </summary>
public const string RequestCharge = "db.cosmosdb.request_charge";

/// <summary>
/// Represents the activity ID for the operation.
/// </summary>
public const string ActivityId = "db.cosmosdb.activity_id";

/// <summary>
/// Represents the connection mode for Cosmos DB.
/// </summary>
public const string ConnectionMode = "db.cosmosdb.connection_mode";

/// <summary>
/// Represents the regions contacted for the operation.
/// </summary>
public const string Region = "db.cosmosdb.regions_contacted";

/// <summary>
/// Represents the item count in the operation.
/// </summary>
public const string ItemCount = "db.cosmosdb.item_count";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Antlr4.Runtime.Misc;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;
using Microsoft.Azure.Documents;
Expand All @@ -19,6 +20,8 @@ internal struct OpenTelemetryCoreRecorder : IDisposable
{
private const string CosmosDb = "cosmosdb";

private static readonly string otelStabilityMode = Environment.GetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN");

private readonly DiagnosticScope scope = default;
private readonly CosmosThresholdOptions config = null;
private readonly Activity activity = null;
Expand Down Expand Up @@ -149,19 +152,33 @@ public void Record(
{
if (this.IsEnabled)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ContainerName, containerName);

if (otelStabilityMode == OpenTelemetryStablityModes.DatabaseDupe)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ContainerName, containerName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.UserAgent, clientContext.UserAgent);

}
else
{
// Classic Appinsights Support
this.scope.AddAttribute(AppInsightClassicAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.ContainerName, containerName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.UserAgent, clientContext.UserAgent);
}

// Other information
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbSystemName, OpenTelemetryCoreRecorder.CosmosDb);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.MachineId, VmMetadataApiHandler.GetMachineId());
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);

// Client Information
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ClientId, clientContext?.Client?.Id);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.UserAgent, clientContext.UserAgent);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ConnectionMode, this.connectionModeCache);

}
}

Expand Down Expand Up @@ -243,7 +260,16 @@ OperationType operationType
}
this.scope.AddAttribute(OpenTelemetryAttributeKeys.RequestContentLength, this.response.RequestContentLength);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.StatusCode, (int)this.response.StatusCode);

if (otelStabilityMode == OpenTelemetryStablityModes.DatabaseDupe)
{
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.StatusCode, (int)this.response.StatusCode);
}
else
{
this.scope.AddIntegerAttribute(AppInsightClassicAttributeKeys.StatusCode, (int)this.response.StatusCode);
}

this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.SubStatusCode, this.response.SubStatusCode);
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.RequestCharge, (int)this.response.RequestCharge);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ItemCount, this.response.ItemCount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Telemetry
{
/// <summary>
/// For More information, Ref https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#semantic-conventions-for-database-client-calls
/// </summary>
internal sealed class OpenTelemetryStablityModes
{
/// <summary>
/// emit the new, stable database conventions, and stop emitting the old experimental database conventions that the instrumentation emitted previously.
/// </summary>
public const string Database = "database";

/// <summary>
/// emit both the old and the stable database conventions, allowing for a seamless transition.
/// </summary>
public const string DatabaseDupe = "database/dup";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@
<ACTIVITY source="Azure.Cosmos.Operation" operationName="Operation.execute_batch" displayName="execute_batch containerName">
<ATTRIBUTE key="az.namespace">Microsoft.DocumentDB</ATTRIBUTE>
<ATTRIBUTE key="az.schema_url">https://opentelemetry.io/schemas/1.23.0</ATTRIBUTE>
<ATTRIBUTE key="db.operation.name">execute_batch</ATTRIBUTE>
<ATTRIBUTE key="db.namespace">databaseName</ATTRIBUTE>
<ATTRIBUTE key="db.collection.name">containerName</ATTRIBUTE>
<ATTRIBUTE key="db.operation">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.name">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.container">Some Value</ATTRIBUTE>
<ATTRIBUTE key="net.peer.name">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.user_agent">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.system">cosmosdb</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.machine_id">Some Value</ATTRIBUTE>
<ATTRIBUTE key="server.address">127.0.0.1</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.client_id">Some Value</ATTRIBUTE>
<ATTRIBUTE key="user_agent.original">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.connection_mode">Direct</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.operation_type">Batch</ATTRIBUTE>
<ATTRIBUTE key="db.operation.batch_size">90</ATTRIBUTE>
Expand Down Expand Up @@ -295,14 +295,14 @@
<ACTIVITY source="Azure.Cosmos.Operation" operationName="Operation.execute_batch" displayName="execute_batch containerName">
<ATTRIBUTE key="az.namespace">Microsoft.DocumentDB</ATTRIBUTE>
<ATTRIBUTE key="az.schema_url">https://opentelemetry.io/schemas/1.23.0</ATTRIBUTE>
<ATTRIBUTE key="db.operation.name">execute_batch</ATTRIBUTE>
<ATTRIBUTE key="db.namespace">databaseName</ATTRIBUTE>
<ATTRIBUTE key="db.collection.name">containerName</ATTRIBUTE>
<ATTRIBUTE key="db.operation">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.name">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.container">Some Value</ATTRIBUTE>
<ATTRIBUTE key="net.peer.name">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.user_agent">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.system">cosmosdb</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.machine_id">Some Value</ATTRIBUTE>
<ATTRIBUTE key="server.address">127.0.0.1</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.client_id">Some Value</ATTRIBUTE>
<ATTRIBUTE key="user_agent.original">Some Value</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.connection_mode">Direct</ATTRIBUTE>
<ATTRIBUTE key="db.cosmosdb.operation_type">Batch</ATTRIBUTE>
<ATTRIBUTE key="db.operation.batch_size">50</ATTRIBUTE>
Expand Down
Loading

0 comments on commit 9055f93

Please sign in to comment.