Skip to content

Commit 33c2603

Browse files
Status code and sub status code
1 parent 5652879 commit 33c2603

File tree

12 files changed

+133
-7
lines changed

12 files changed

+133
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <copyright file="IResponseHeaders.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using Datadog.Trace.DuckTyping;
9+
10+
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.CosmosDb
11+
{
12+
/// <summary>
13+
/// Microsoft.Azure.Cosmos.Headers interface for duck typing
14+
/// </summary>
15+
/// <remarks>
16+
/// https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Headers/Headers.cs
17+
/// </remarks>
18+
internal interface IResponseHeaders : IDuckType
19+
{
20+
[Duck(Name = "SubStatusCodeLiteral")]
21+
string? SubStatusCodeLiteral { get; }
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <copyright file="IResponseMessage.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using System.Net;
9+
using Datadog.Trace.DuckTyping;
10+
11+
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.CosmosDb
12+
{
13+
/// <summary>
14+
/// Microsoft.Azure.Cosmos.ResponseMessage interface for duck typing
15+
/// </summary>
16+
/// <remarks>
17+
/// https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Handler/ResponseMessage.cs
18+
/// </remarks>
19+
internal interface IResponseMessage : IDuckType
20+
{
21+
HttpStatusCode StatusCode { get; }
22+
23+
IResponseHeaders Headers { get; }
24+
}
25+
}

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/CosmosDb/RequestInvokerHandlerSendAsyncIntegration.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Datadog.Trace.Configuration;
1414
using Datadog.Trace.DuckTyping;
1515
using Datadog.Trace.Logging;
16+
using Datadog.Trace.Tagging;
1617
using Datadog.Trace.Util;
1718

1819
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.CosmosDb;
@@ -102,13 +103,35 @@ internal static CallTargetState OnMethodBegin<TTarget, TContainer>(TTarget insta
102103
return CallTargetState.GetDefault();
103104
}
104105

105-
internal static TReturn OnAsyncMethodEnd<TTarget, TReturn>(
106-
TTarget instance,
107-
TReturn returnValue,
108-
Exception exception,
109-
in CallTargetState state)
106+
internal static TReturn OnAsyncMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, in CallTargetState state)
107+
where TReturn : IResponseMessage
110108
{
111-
state.Scope?.DisposeWithException(exception);
109+
var scope = state.Scope;
110+
111+
if (scope != null && returnValue.Instance is not null)
112+
{
113+
try
114+
{
115+
var tags = (CosmosDbTags)scope.Span.Tags;
116+
var statusCode = (int)returnValue.StatusCode;
117+
tags.ResponseStatusCode = statusCode.ToString();
118+
119+
if (returnValue.Headers?.Instance != null)
120+
{
121+
var subStatusCode = returnValue.Headers.SubStatusCodeLiteral;
122+
if (!StringUtil.IsNullOrEmpty(subStatusCode))
123+
{
124+
tags.ResponseSubStatusCode = subStatusCode;
125+
}
126+
}
127+
}
128+
catch (Exception ex)
129+
{
130+
Log.Error(ex, "Error handling CosmosDb response.");
131+
}
132+
}
133+
134+
scope?.DisposeWithException(exception);
112135
return returnValue;
113136
}
114137
}

tracer/src/Datadog.Trace/Tagging/CosmosDbTags.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ internal partial class CosmosDbTags : InstrumentationTags
3131
[Tag(Trace.Tags.OutHost)]
3232
public string Host { get; set; }
3333

34+
[Tag(Trace.Tags.DbResponseStatusCode)]
35+
public string ResponseStatusCode { get; set; }
36+
37+
[Tag(Trace.Tags.CosmosDbResponseSubStatusCode)]
38+
public string ResponseSubStatusCode { get; set; }
39+
3440
public virtual void SetEndpoint(Uri endpoint)
3541
{
3642
Host = endpoint?.ToString();

tracer/src/Datadog.Trace/Tags.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,16 @@ public static partial class Tags
630630
/// </summary>
631631
internal const string CosmosDbContainer = "cosmosdb.container";
632632

633+
/// <summary>
634+
/// The HTTP status code of the database response.
635+
/// </summary>
636+
internal const string DbResponseStatusCode = "db.response.status_code";
637+
638+
/// <summary>
639+
/// The CosmosDb sub-status code of the response.
640+
/// </summary>
641+
internal const string CosmosDbResponseSubStatusCode = "cosmosdb.response.sub_status_code";
642+
633643
/// <summary>
634644
/// If a span was involved with an application security event
635645
/// </summary>

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/CosmosVnextTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public async Task InitializeAsync()
115115

116116
private static VerifySettings ApplyCosmosDbScrubbers(VerifySettings settings)
117117
{
118-
// Normalize cosmosdb host between localhost, x64, and ARM64
119118
settings.AddSimpleScrubber("out.host: https://localhost:00000/", "out.host: https://cosmosdb-emulator:8081/");
120119
settings.AddSimpleScrubber("out.host: https://cosmosdb-emulator_arm64:8081/", "out.host: https://cosmosdb-emulator:8081/");
121120
settings.AddSimpleScrubber("out.host: localhost", "out.host: cosmosdb-emulator");

tracer/test/Datadog.Trace.TestHelpers/SpanMetadataV0Rules.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ public static Result IsCosmosDbV0(this MockSpan span) => Result.FromSpan(span)
470470
.IsOptional("db.name")
471471
.Matches("db.type", "cosmosdb")
472472
.IsPresent("out.host")
473+
.IsOptional("db.response.status_code")
474+
.IsOptional("cosmosdb.response.sub_status_code")
473475
.IsOptional("_dd.base_service")
474476
.Matches("component", "CosmosDb")
475477
.Matches("span.kind", "client"));

tracer/test/Datadog.Trace.TestHelpers/SpanMetadataV1Rules.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ public static Result IsCosmosDbV1(this MockSpan span) => Result.FromSpan(span)
350350
.IsPresent("out.port")
351351
.IsPresent("peer.service")
352352
.IsOptional("peer.service.remapped_from")
353+
.IsOptional("db.response.status_code")
354+
.IsOptional("cosmosdb.response.sub_status_code")
353355
.IsOptional("_dd.base_service")
354356
.MatchesOneOf("_dd.peer.service.source", "db.name", "out.host", "peer.service")
355357
.Matches("component", "CosmosDb")

tracer/test/snapshots/CosmosVnextTests.SubmitTracesCRUD_SchemaV0.verified.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Type: sql,
99
Tags: {
1010
component: CosmosDb,
11+
db.response.status_code: 201,
1112
db.type: cosmosdb,
1213
env: integration_tests,
1314
language: dotnet,
@@ -32,6 +33,7 @@
3233
Type: sql,
3334
Tags: {
3435
component: CosmosDb,
36+
db.response.status_code: 201,
3537
db.type: cosmosdb,
3638
env: integration_tests,
3739
language: dotnet,
@@ -58,6 +60,7 @@
5860
component: CosmosDb,
5961
cosmosdb.container: items,
6062
db.name: db,
63+
db.response.status_code: 201,
6164
db.type: cosmosdb,
6265
env: integration_tests,
6366
language: dotnet,
@@ -82,6 +85,7 @@
8285
Type: sql,
8386
Tags: {
8487
component: CosmosDb,
88+
db.response.status_code: 204,
8589
db.type: cosmosdb,
8690
env: integration_tests,
8791
language: dotnet,
@@ -108,6 +112,7 @@
108112
component: CosmosDb,
109113
cosmosdb.container: items,
110114
db.name: db,
115+
db.response.status_code: 204,
111116
db.type: cosmosdb,
112117
env: integration_tests,
113118
language: dotnet,
@@ -132,6 +137,7 @@
132137
Type: sql,
133138
Tags: {
134139
component: CosmosDb,
140+
db.response.status_code: 404,
135141
db.type: cosmosdb,
136142
env: integration_tests,
137143
language: dotnet,
@@ -156,6 +162,7 @@
156162
Type: sql,
157163
Tags: {
158164
component: CosmosDb,
165+
db.response.status_code: 404,
159166
db.type: cosmosdb,
160167
env: integration_tests,
161168
language: dotnet,
@@ -182,6 +189,7 @@
182189
component: CosmosDb,
183190
cosmosdb.container: items,
184191
db.name: db,
192+
db.response.status_code: 200,
185193
db.type: cosmosdb,
186194
env: integration_tests,
187195
language: dotnet,
@@ -208,6 +216,7 @@
208216
component: CosmosDb,
209217
cosmosdb.container: items,
210218
db.name: db,
219+
db.response.status_code: 200,
211220
db.type: cosmosdb,
212221
env: integration_tests,
213222
language: dotnet,

tracer/test/snapshots/CosmosVnextTests.SubmitTracesCRUD_SchemaV1.verified.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Type: sql,
99
Tags: {
1010
component: CosmosDb,
11+
db.response.status_code: 201,
1112
db.type: cosmosdb,
1213
env: integration_tests,
1314
language: dotnet,
@@ -35,6 +36,7 @@
3536
Type: sql,
3637
Tags: {
3738
component: CosmosDb,
39+
db.response.status_code: 201,
3840
db.type: cosmosdb,
3941
env: integration_tests,
4042
language: dotnet,
@@ -64,6 +66,7 @@
6466
component: CosmosDb,
6567
cosmosdb.container: items,
6668
db.name: db,
69+
db.response.status_code: 201,
6770
db.type: cosmosdb,
6871
env: integration_tests,
6972
language: dotnet,
@@ -91,6 +94,7 @@
9194
Type: sql,
9295
Tags: {
9396
component: CosmosDb,
97+
db.response.status_code: 204,
9498
db.type: cosmosdb,
9599
env: integration_tests,
96100
language: dotnet,
@@ -120,6 +124,7 @@
120124
component: CosmosDb,
121125
cosmosdb.container: items,
122126
db.name: db,
127+
db.response.status_code: 204,
123128
db.type: cosmosdb,
124129
env: integration_tests,
125130
language: dotnet,
@@ -147,6 +152,7 @@
147152
Type: sql,
148153
Tags: {
149154
component: CosmosDb,
155+
db.response.status_code: 404,
150156
db.type: cosmosdb,
151157
env: integration_tests,
152158
language: dotnet,
@@ -174,6 +180,7 @@
174180
Type: sql,
175181
Tags: {
176182
component: CosmosDb,
183+
db.response.status_code: 404,
177184
db.type: cosmosdb,
178185
env: integration_tests,
179186
language: dotnet,
@@ -203,6 +210,7 @@
203210
component: CosmosDb,
204211
cosmosdb.container: items,
205212
db.name: db,
213+
db.response.status_code: 200,
206214
db.type: cosmosdb,
207215
env: integration_tests,
208216
language: dotnet,
@@ -232,6 +240,7 @@
232240
component: CosmosDb,
233241
cosmosdb.container: items,
234242
db.name: db,
243+
db.response.status_code: 200,
235244
db.type: cosmosdb,
236245
env: integration_tests,
237246
language: dotnet,

0 commit comments

Comments
 (0)