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

Distributed Tracing: Fixes dependency failure on appinsights #4098

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
Expand Up @@ -36,7 +36,7 @@ public static void RecordDiagnosticsForRequests(
OpenTelemetryAttributes response)
{
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(
response: response) && CosmosDbEventSource.IsEnabled(EventLevel.Warning))
response.StatusCode, response.SubStatusCode) && CosmosDbEventSource.IsEnabled(EventLevel.Warning))
{
CosmosDbEventSource.Singleton.FailedRequest(response.Diagnostics.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Microsoft.Azure.Cosmos.Telemetry.Diagnostics
{
using System;
using System.Net;
using Documents;
using static Antlr4.Runtime.TokenStreamRewriter;

internal static class DiagnosticsFilterHelper
{
Expand Down Expand Up @@ -39,13 +39,13 @@ public static bool IsLatencyThresholdCrossed(
/// Check if response HTTP status code is returning successful
/// </summary>
/// <returns>true or false</returns>
public static bool IsSuccessfulResponse(OpenTelemetryAttributes response)
{
return response.StatusCode.IsSuccess()
|| (response.StatusCode == System.Net.HttpStatusCode.NotFound && response.SubStatusCode == 0)
|| (response.StatusCode == System.Net.HttpStatusCode.NotModified && response.SubStatusCode == 0)
|| (response.StatusCode == System.Net.HttpStatusCode.Conflict && response.SubStatusCode == 0)
|| (response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed && response.SubStatusCode == 0);
public static bool IsSuccessfulResponse(HttpStatusCode statusCode, int substatusCode)
{
return statusCode.IsSuccess()
|| (statusCode == System.Net.HttpStatusCode.NotFound && substatusCode == 0)
|| (statusCode == System.Net.HttpStatusCode.NotModified && substatusCode == 0)
|| (statusCode == System.Net.HttpStatusCode.Conflict && substatusCode == 0)
|| (statusCode == System.Net.HttpStatusCode.PreconditionFailed && substatusCode == 0);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System.Collections.Generic;
using System.Diagnostics;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;

/// <summary>
/// This class is used to add information in an Activity tags ref. https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058
Expand Down Expand Up @@ -177,7 +178,12 @@ public void MarkFailed(Exception exception)
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionMessage, exception.Message);
}

this.scope.Failed(exception);
if (exception is not CosmosException || (exception is CosmosException cosmosException
&& !DiagnosticsFilterHelper
.IsSuccessfulResponse(cosmosException.StatusCode, cosmosException.SubStatusCode)))
{
this.scope.Failed(exception);
}
}
}

Expand Down Expand Up @@ -205,7 +211,7 @@ internal static bool IsExceptionRegistered(Exception exception, DiagnosticScope

public void Dispose()
{
if (this.scope.IsEnabled)
if (this.IsEnabled)
{
Documents.OperationType operationType
= (this.response == null || this.response?.OperationType == Documents.OperationType.Invalid) ? this.operationType : this.response.OperationType;
Expand All @@ -228,6 +234,11 @@ Documents.OperationType operationType
this.scope.AddAttribute(OpenTelemetryAttributeKeys.Region, ClientTelemetryHelper.GetContactedRegions(this.response.Diagnostics.GetContactedRegions()));
CosmosDbEventSource.RecordDiagnosticsForRequests(this.config, operationType, this.response);
}

if (!DiagnosticsFilterHelper.IsSuccessfulResponse(this.response.StatusCode, this.response.SubStatusCode))
{
this.scope.Failed();
}
}

this.scope.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace Microsoft.Azure.Cosmos.Tracing
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using global::Azure;
using System.Net;
using Microsoft.Azure.Cosmos.Telemetry;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;
using Microsoft.Azure.Cosmos.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
Expand All @@ -32,42 +33,49 @@ public static void IsValidOperationActivity(Activity activity)
}

IList<string> expectedTags = new List<string>
{
"az.namespace",
"az.schema_url",
"kind",
"db.system",
"db.name",
"db.operation",
"net.peer.name",
"db.cosmosdb.client_id",
"db.cosmosdb.machine_id",
"user_agent.original",
"db.cosmosdb.connection_mode",
"db.cosmosdb.operation_type",
"db.cosmosdb.container",
"db.cosmosdb.request_content_length_bytes",
"db.cosmosdb.response_content_length_bytes",
"db.cosmosdb.status_code",
"db.cosmosdb.sub_status_code",
"db.cosmosdb.request_charge",
"db.cosmosdb.regions_contacted",
"db.cosmosdb.retry_count",
"db.cosmosdb.item_count",
"db.cosmosdb.request_diagnostics",
"exception.type",
"exception.message",
"exception.stacktrace",
"db.cosmosdb.activity_id",
"db.cosmosdb.correlated_activity_id"
};
{
"az.namespace",
"az.schema_url",
"kind",
"db.system",
"db.name",
"db.operation",
"net.peer.name",
"db.cosmosdb.client_id",
"db.cosmosdb.machine_id",
"user_agent.original",
"db.cosmosdb.connection_mode",
"db.cosmosdb.operation_type",
"db.cosmosdb.container",
"db.cosmosdb.request_content_length_bytes",
"db.cosmosdb.response_content_length_bytes",
"db.cosmosdb.status_code",
"db.cosmosdb.sub_status_code",
"db.cosmosdb.request_charge",
"db.cosmosdb.regions_contacted",
"db.cosmosdb.retry_count",
"db.cosmosdb.item_count",
"db.cosmosdb.request_diagnostics",
"exception.type",
"exception.message",
"exception.stacktrace",
"db.cosmosdb.activity_id",
"db.cosmosdb.correlated_activity_id"
};

foreach (KeyValuePair<string, string> actualTag in activity.Tags)
{
Assert.IsTrue(expectedTags.Contains(actualTag.Key), $"{actualTag.Key} is not allowed for {activity.OperationName}");

AssertActivity.AssertDatabaseAndContainerName(activity.OperationName, actualTag);
}

HttpStatusCode statusCode = (HttpStatusCode)Convert.ToInt32(activity.GetTagItem("db.cosmosdb.status_code"));
int subStatusCode = Convert.ToInt32(activity.GetTagItem("db.cosmosdb.sub_status_code"));
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(statusCode, subStatusCode))
{
Assert.AreEqual(ActivityStatusCode.Error, activity.Status);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public void CheckReturnTrueOnFailedStatusCode()

Assert.IsTrue(
!DiagnosticsFilterHelper
.IsSuccessfulResponse(response),
.IsSuccessfulResponse(response.StatusCode, response.SubStatusCode),
$" Response time is {response.Diagnostics.GetClientElapsedTime().Milliseconds}ms " +
$"and Configured threshold value is {distributedTracingOptions.LatencyThresholdForDiagnosticEvent.Value.Milliseconds}ms " +
$"and Is response Success : {response.StatusCode.IsSuccess()}");

}

}
Expand Down
Loading