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

Cherry-Pick Appinsights: Adds classic attribute back to cosmos db to support appinsights sdk (#4781) (#4799) #4804

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading