Skip to content

Commit 88bf919

Browse files
committed
refactor
1 parent 74c28e4 commit 88bf919

File tree

5 files changed

+58
-43
lines changed

5 files changed

+58
-43
lines changed

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/CosmosDbEventSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void RecordDiagnosticsForRequests(
3636
OpenTelemetryAttributes response)
3737
{
3838
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(
39-
response: response) && CosmosDbEventSource.IsEnabled(EventLevel.Warning))
39+
response.StatusCode, response.SubStatusCode) && CosmosDbEventSource.IsEnabled(EventLevel.Warning))
4040
{
4141
CosmosDbEventSource.Singleton.FailedRequest(response.Diagnostics.ToString());
4242
}

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/Filters/DiagnosticsFilterHelper.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Microsoft.Azure.Cosmos.Telemetry.Diagnostics
66
{
77
using System;
8+
using System.Net;
89
using Documents;
9-
using static Antlr4.Runtime.TokenStreamRewriter;
1010

1111
internal static class DiagnosticsFilterHelper
1212
{
@@ -39,13 +39,13 @@ public static bool IsLatencyThresholdCrossed(
3939
/// Check if response HTTP status code is returning successful
4040
/// </summary>
4141
/// <returns>true or false</returns>
42-
public static bool IsSuccessfulResponse(OpenTelemetryAttributes response)
43-
{
44-
return response.StatusCode.IsSuccess()
45-
|| (response.StatusCode == System.Net.HttpStatusCode.NotFound && response.SubStatusCode == 0)
46-
|| (response.StatusCode == System.Net.HttpStatusCode.NotModified && response.SubStatusCode == 0)
47-
|| (response.StatusCode == System.Net.HttpStatusCode.Conflict && response.SubStatusCode == 0)
48-
|| (response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed && response.SubStatusCode == 0);
42+
public static bool IsSuccessfulResponse(HttpStatusCode statusCode, int substatusCode)
43+
{
44+
return statusCode.IsSuccess()
45+
|| (statusCode == System.Net.HttpStatusCode.NotFound && substatusCode == 0)
46+
|| (statusCode == System.Net.HttpStatusCode.NotModified && substatusCode == 0)
47+
|| (statusCode == System.Net.HttpStatusCode.Conflict && substatusCode == 0)
48+
|| (statusCode == System.Net.HttpStatusCode.PreconditionFailed && substatusCode == 0);
4949
}
5050

5151
/// <summary>

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,22 @@ public void MarkFailed(Exception exception)
178178
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionMessage, exception.Message);
179179
}
180180

181-
if (DiagnosticsFilterHelper.IsSuccessfulResponse(this.response))
181+
if (exception is CosmosException cosmosException
182+
&& DiagnosticsFilterHelper
183+
.IsSuccessfulResponse(cosmosException.StatusCode, cosmosException.SubStatusCode))
182184
{
183185
this.scope.Dispose();
184186
}
185187
else
186188
{
189+
Console.WriteLine(exception);
187190
this.scope.Failed(exception);
188191
}
189192
}
193+
else
194+
{
195+
this.activity?.Stop();
196+
}
190197
}
191198

192199
/// <summary>
@@ -213,7 +220,7 @@ internal static bool IsExceptionRegistered(Exception exception, DiagnosticScope
213220

214221
public void Dispose()
215222
{
216-
if (this.scope.IsEnabled)
223+
if (this.IsEnabled)
217224
{
218225
Documents.OperationType operationType
219226
= (this.response == null || this.response?.OperationType == Documents.OperationType.Invalid) ? this.operationType : this.response.OperationType;

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Tracing/AssertActivity.cs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ namespace Microsoft.Azure.Cosmos.Tracing
88
using System.Collections.Generic;
99
using System.Diagnostics;
1010
using System.Linq;
11-
using global::Azure;
11+
using System.Net;
1212
using Microsoft.Azure.Cosmos.Telemetry;
13+
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;
1314
using Microsoft.Azure.Cosmos.Tests;
1415
using Microsoft.VisualStudio.TestTools.UnitTesting;
1516
using Newtonsoft.Json;
@@ -32,42 +33,50 @@ public static void IsValidOperationActivity(Activity activity)
3233
}
3334

3435
IList<string> expectedTags = new List<string>
35-
{
36-
"az.namespace",
37-
"az.schema_url",
38-
"kind",
39-
"db.system",
40-
"db.name",
41-
"db.operation",
42-
"net.peer.name",
43-
"db.cosmosdb.client_id",
44-
"db.cosmosdb.machine_id",
45-
"user_agent.original",
46-
"db.cosmosdb.connection_mode",
47-
"db.cosmosdb.operation_type",
48-
"db.cosmosdb.container",
49-
"db.cosmosdb.request_content_length_bytes",
50-
"db.cosmosdb.response_content_length_bytes",
51-
"db.cosmosdb.status_code",
52-
"db.cosmosdb.sub_status_code",
53-
"db.cosmosdb.request_charge",
54-
"db.cosmosdb.regions_contacted",
55-
"db.cosmosdb.retry_count",
56-
"db.cosmosdb.item_count",
57-
"db.cosmosdb.request_diagnostics",
58-
"exception.type",
59-
"exception.message",
60-
"exception.stacktrace",
61-
"db.cosmosdb.activity_id",
62-
"db.cosmosdb.correlated_activity_id"
63-
};
36+
{
37+
"az.namespace",
38+
"az.schema_url",
39+
"kind",
40+
"db.system",
41+
"db.name",
42+
"db.operation",
43+
"net.peer.name",
44+
"db.cosmosdb.client_id",
45+
"db.cosmosdb.machine_id",
46+
"user_agent.original",
47+
"db.cosmosdb.connection_mode",
48+
"db.cosmosdb.operation_type",
49+
"db.cosmosdb.container",
50+
"db.cosmosdb.request_content_length_bytes",
51+
"db.cosmosdb.response_content_length_bytes",
52+
"db.cosmosdb.status_code",
53+
"db.cosmosdb.sub_status_code",
54+
"db.cosmosdb.request_charge",
55+
"db.cosmosdb.regions_contacted",
56+
"db.cosmosdb.retry_count",
57+
"db.cosmosdb.item_count",
58+
"db.cosmosdb.request_diagnostics",
59+
"exception.type",
60+
"exception.message",
61+
"exception.stacktrace",
62+
"db.cosmosdb.activity_id",
63+
"db.cosmosdb.correlated_activity_id"
64+
};
6465

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

6970
AssertActivity.AssertDatabaseAndContainerName(activity.OperationName, actualTag);
7071
}
72+
73+
HttpStatusCode statusCode = (HttpStatusCode)Convert.ToInt32(activity.GetTagItem("db.cosmosdb.status_code"));
74+
int subStatusCode = Convert.ToInt32(activity.GetTagItem("db.cosmosdb.sub_status_code"));
75+
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(statusCode, subStatusCode))
76+
{
77+
Console.WriteLine(statusCode + " " + subStatusCode);
78+
Assert.AreEqual(ActivityStatusCode.Error, activity.Status);
79+
}
7180
}
7281
}
7382

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Telemetry/DiagnosticsFilterHelperTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ public void CheckReturnTrueOnFailedStatusCode()
7171

7272
Assert.IsTrue(
7373
!DiagnosticsFilterHelper
74-
.IsSuccessfulResponse(response),
74+
.IsSuccessfulResponse(response.StatusCode, response.SubStatusCode),
7575
$" Response time is {response.Diagnostics.GetClientElapsedTime().Milliseconds}ms " +
7676
$"and Configured threshold value is {distributedTracingOptions.LatencyThresholdForDiagnosticEvent.Value.Milliseconds}ms " +
7777
$"and Is response Success : {response.StatusCode.IsSuccess()}");
78-
7978
}
8079

8180
}

0 commit comments

Comments
 (0)