Distributed Tracing: Fixes dependency failure on appinsights (#4098)

* first draft

* refactor

* fix tests

* fixed condition
This commit is contained in:
Sourabh Jain 2023-09-26 19:38:47 +05:30 коммит произвёл GitHub
Родитель 9f8d84860d
Коммит 08e7f54730
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 61 добавлений и 43 удалений

Просмотреть файл

@ -36,7 +36,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
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());
}

Просмотреть файл

@ -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
{
@ -39,13 +39,13 @@ namespace Microsoft.Azure.Cosmos.Telemetry.Diagnostics
/// 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>

Просмотреть файл

@ -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
@ -177,7 +178,12 @@ namespace Microsoft.Azure.Cosmos.Telemetry
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);
}
}
}
@ -205,7 +211,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
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;
@ -228,6 +234,11 @@ namespace Microsoft.Azure.Cosmos.Telemetry
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();

Просмотреть файл

@ -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;
@ -32,35 +33,35 @@ namespace Microsoft.Azure.Cosmos.Tracing
}
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)
{
@ -68,6 +69,13 @@ namespace Microsoft.Azure.Cosmos.Tracing
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);
}
}
}

Просмотреть файл

@ -71,11 +71,10 @@ namespace Microsoft.Azure.Cosmos.Tests.Telemetry
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()}");
}
}