Skip to content

Commit d791f85

Browse files
Status code and sub status code
1 parent 5652879 commit d791f85

File tree

16 files changed

+277
-7
lines changed

16 files changed

+277
-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/Generated/net461/Datadog.Trace.SourceGenerators/TagListGenerator/CosmosDbTags.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ partial class CosmosDbTags
2626
private static ReadOnlySpan<byte> DatabaseIdBytes => new byte[] { 167, 100, 98, 46, 110, 97, 109, 101 };
2727
// HostBytes = MessagePack.Serialize("out.host");
2828
private static ReadOnlySpan<byte> HostBytes => new byte[] { 168, 111, 117, 116, 46, 104, 111, 115, 116 };
29+
// ResponseStatusCodeBytes = MessagePack.Serialize("db.response.status_code");
30+
private static ReadOnlySpan<byte> ResponseStatusCodeBytes => new byte[] { 183, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
31+
// ResponseSubStatusCodeBytes = MessagePack.Serialize("cosmosdb.response.sub_status_code");
32+
private static ReadOnlySpan<byte> ResponseSubStatusCodeBytes => new byte[] { 217, 33, 99, 111, 115, 109, 111, 115, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 117, 98, 95, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
2933

3034
public override string? GetTag(string key)
3135
{
@@ -37,6 +41,8 @@ partial class CosmosDbTags
3741
"cosmosdb.container" => ContainerId,
3842
"db.name" => DatabaseId,
3943
"out.host" => Host,
44+
"db.response.status_code" => ResponseStatusCode,
45+
"cosmosdb.response.sub_status_code" => ResponseSubStatusCode,
4046
_ => base.GetTag(key),
4147
};
4248
}
@@ -54,6 +60,12 @@ public override void SetTag(string key, string value)
5460
case "out.host":
5561
Host = value;
5662
break;
63+
case "db.response.status_code":
64+
ResponseStatusCode = value;
65+
break;
66+
case "cosmosdb.response.sub_status_code":
67+
ResponseSubStatusCode = value;
68+
break;
5769
case "span.kind":
5870
case "component":
5971
case "db.type":
@@ -97,6 +109,16 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
97109
processor.Process(new TagItem<string>("out.host", Host, HostBytes));
98110
}
99111

112+
if (ResponseStatusCode is not null)
113+
{
114+
processor.Process(new TagItem<string>("db.response.status_code", ResponseStatusCode, ResponseStatusCodeBytes));
115+
}
116+
117+
if (ResponseSubStatusCode is not null)
118+
{
119+
processor.Process(new TagItem<string>("cosmosdb.response.sub_status_code", ResponseSubStatusCode, ResponseSubStatusCodeBytes));
120+
}
121+
100122
base.EnumerateTags(ref processor);
101123
}
102124

@@ -144,6 +166,20 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
144166
.Append(',');
145167
}
146168

169+
if (ResponseStatusCode is not null)
170+
{
171+
sb.Append("db.response.status_code (tag):")
172+
.Append(ResponseStatusCode)
173+
.Append(',');
174+
}
175+
176+
if (ResponseSubStatusCode is not null)
177+
{
178+
sb.Append("cosmosdb.response.sub_status_code (tag):")
179+
.Append(ResponseSubStatusCode)
180+
.Append(',');
181+
}
182+
147183
base.WriteAdditionalTags(sb);
148184
}
149185
}

tracer/src/Datadog.Trace/Generated/net6.0/Datadog.Trace.SourceGenerators/TagListGenerator/CosmosDbTags.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ partial class CosmosDbTags
2626
private static ReadOnlySpan<byte> DatabaseIdBytes => new byte[] { 167, 100, 98, 46, 110, 97, 109, 101 };
2727
// HostBytes = MessagePack.Serialize("out.host");
2828
private static ReadOnlySpan<byte> HostBytes => new byte[] { 168, 111, 117, 116, 46, 104, 111, 115, 116 };
29+
// ResponseStatusCodeBytes = MessagePack.Serialize("db.response.status_code");
30+
private static ReadOnlySpan<byte> ResponseStatusCodeBytes => new byte[] { 183, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
31+
// ResponseSubStatusCodeBytes = MessagePack.Serialize("cosmosdb.response.sub_status_code");
32+
private static ReadOnlySpan<byte> ResponseSubStatusCodeBytes => new byte[] { 217, 33, 99, 111, 115, 109, 111, 115, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 117, 98, 95, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
2933

3034
public override string? GetTag(string key)
3135
{
@@ -37,6 +41,8 @@ partial class CosmosDbTags
3741
"cosmosdb.container" => ContainerId,
3842
"db.name" => DatabaseId,
3943
"out.host" => Host,
44+
"db.response.status_code" => ResponseStatusCode,
45+
"cosmosdb.response.sub_status_code" => ResponseSubStatusCode,
4046
_ => base.GetTag(key),
4147
};
4248
}
@@ -54,6 +60,12 @@ public override void SetTag(string key, string value)
5460
case "out.host":
5561
Host = value;
5662
break;
63+
case "db.response.status_code":
64+
ResponseStatusCode = value;
65+
break;
66+
case "cosmosdb.response.sub_status_code":
67+
ResponseSubStatusCode = value;
68+
break;
5769
case "span.kind":
5870
case "component":
5971
case "db.type":
@@ -97,6 +109,16 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
97109
processor.Process(new TagItem<string>("out.host", Host, HostBytes));
98110
}
99111

112+
if (ResponseStatusCode is not null)
113+
{
114+
processor.Process(new TagItem<string>("db.response.status_code", ResponseStatusCode, ResponseStatusCodeBytes));
115+
}
116+
117+
if (ResponseSubStatusCode is not null)
118+
{
119+
processor.Process(new TagItem<string>("cosmosdb.response.sub_status_code", ResponseSubStatusCode, ResponseSubStatusCodeBytes));
120+
}
121+
100122
base.EnumerateTags(ref processor);
101123
}
102124

@@ -144,6 +166,20 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
144166
.Append(',');
145167
}
146168

169+
if (ResponseStatusCode is not null)
170+
{
171+
sb.Append("db.response.status_code (tag):")
172+
.Append(ResponseStatusCode)
173+
.Append(',');
174+
}
175+
176+
if (ResponseSubStatusCode is not null)
177+
{
178+
sb.Append("cosmosdb.response.sub_status_code (tag):")
179+
.Append(ResponseSubStatusCode)
180+
.Append(',');
181+
}
182+
147183
base.WriteAdditionalTags(sb);
148184
}
149185
}

tracer/src/Datadog.Trace/Generated/netcoreapp3.1/Datadog.Trace.SourceGenerators/TagListGenerator/CosmosDbTags.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ partial class CosmosDbTags
2626
private static ReadOnlySpan<byte> DatabaseIdBytes => new byte[] { 167, 100, 98, 46, 110, 97, 109, 101 };
2727
// HostBytes = MessagePack.Serialize("out.host");
2828
private static ReadOnlySpan<byte> HostBytes => new byte[] { 168, 111, 117, 116, 46, 104, 111, 115, 116 };
29+
// ResponseStatusCodeBytes = MessagePack.Serialize("db.response.status_code");
30+
private static ReadOnlySpan<byte> ResponseStatusCodeBytes => new byte[] { 183, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
31+
// ResponseSubStatusCodeBytes = MessagePack.Serialize("cosmosdb.response.sub_status_code");
32+
private static ReadOnlySpan<byte> ResponseSubStatusCodeBytes => new byte[] { 217, 33, 99, 111, 115, 109, 111, 115, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 117, 98, 95, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
2933

3034
public override string? GetTag(string key)
3135
{
@@ -37,6 +41,8 @@ partial class CosmosDbTags
3741
"cosmosdb.container" => ContainerId,
3842
"db.name" => DatabaseId,
3943
"out.host" => Host,
44+
"db.response.status_code" => ResponseStatusCode,
45+
"cosmosdb.response.sub_status_code" => ResponseSubStatusCode,
4046
_ => base.GetTag(key),
4147
};
4248
}
@@ -54,6 +60,12 @@ public override void SetTag(string key, string value)
5460
case "out.host":
5561
Host = value;
5662
break;
63+
case "db.response.status_code":
64+
ResponseStatusCode = value;
65+
break;
66+
case "cosmosdb.response.sub_status_code":
67+
ResponseSubStatusCode = value;
68+
break;
5769
case "span.kind":
5870
case "component":
5971
case "db.type":
@@ -97,6 +109,16 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
97109
processor.Process(new TagItem<string>("out.host", Host, HostBytes));
98110
}
99111

112+
if (ResponseStatusCode is not null)
113+
{
114+
processor.Process(new TagItem<string>("db.response.status_code", ResponseStatusCode, ResponseStatusCodeBytes));
115+
}
116+
117+
if (ResponseSubStatusCode is not null)
118+
{
119+
processor.Process(new TagItem<string>("cosmosdb.response.sub_status_code", ResponseSubStatusCode, ResponseSubStatusCodeBytes));
120+
}
121+
100122
base.EnumerateTags(ref processor);
101123
}
102124

@@ -144,6 +166,20 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
144166
.Append(',');
145167
}
146168

169+
if (ResponseStatusCode is not null)
170+
{
171+
sb.Append("db.response.status_code (tag):")
172+
.Append(ResponseStatusCode)
173+
.Append(',');
174+
}
175+
176+
if (ResponseSubStatusCode is not null)
177+
{
178+
sb.Append("cosmosdb.response.sub_status_code (tag):")
179+
.Append(ResponseSubStatusCode)
180+
.Append(',');
181+
}
182+
147183
base.WriteAdditionalTags(sb);
148184
}
149185
}

tracer/src/Datadog.Trace/Generated/netstandard2.0/Datadog.Trace.SourceGenerators/TagListGenerator/CosmosDbTags.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ partial class CosmosDbTags
2626
private static ReadOnlySpan<byte> DatabaseIdBytes => new byte[] { 167, 100, 98, 46, 110, 97, 109, 101 };
2727
// HostBytes = MessagePack.Serialize("out.host");
2828
private static ReadOnlySpan<byte> HostBytes => new byte[] { 168, 111, 117, 116, 46, 104, 111, 115, 116 };
29+
// ResponseStatusCodeBytes = MessagePack.Serialize("db.response.status_code");
30+
private static ReadOnlySpan<byte> ResponseStatusCodeBytes => new byte[] { 183, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
31+
// ResponseSubStatusCodeBytes = MessagePack.Serialize("cosmosdb.response.sub_status_code");
32+
private static ReadOnlySpan<byte> ResponseSubStatusCodeBytes => new byte[] { 217, 33, 99, 111, 115, 109, 111, 115, 100, 98, 46, 114, 101, 115, 112, 111, 110, 115, 101, 46, 115, 117, 98, 95, 115, 116, 97, 116, 117, 115, 95, 99, 111, 100, 101 };
2933

3034
public override string? GetTag(string key)
3135
{
@@ -37,6 +41,8 @@ partial class CosmosDbTags
3741
"cosmosdb.container" => ContainerId,
3842
"db.name" => DatabaseId,
3943
"out.host" => Host,
44+
"db.response.status_code" => ResponseStatusCode,
45+
"cosmosdb.response.sub_status_code" => ResponseSubStatusCode,
4046
_ => base.GetTag(key),
4147
};
4248
}
@@ -54,6 +60,12 @@ public override void SetTag(string key, string value)
5460
case "out.host":
5561
Host = value;
5662
break;
63+
case "db.response.status_code":
64+
ResponseStatusCode = value;
65+
break;
66+
case "cosmosdb.response.sub_status_code":
67+
ResponseSubStatusCode = value;
68+
break;
5769
case "span.kind":
5870
case "component":
5971
case "db.type":
@@ -97,6 +109,16 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
97109
processor.Process(new TagItem<string>("out.host", Host, HostBytes));
98110
}
99111

112+
if (ResponseStatusCode is not null)
113+
{
114+
processor.Process(new TagItem<string>("db.response.status_code", ResponseStatusCode, ResponseStatusCodeBytes));
115+
}
116+
117+
if (ResponseSubStatusCode is not null)
118+
{
119+
processor.Process(new TagItem<string>("cosmosdb.response.sub_status_code", ResponseSubStatusCode, ResponseSubStatusCodeBytes));
120+
}
121+
100122
base.EnumerateTags(ref processor);
101123
}
102124

@@ -144,6 +166,20 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
144166
.Append(',');
145167
}
146168

169+
if (ResponseStatusCode is not null)
170+
{
171+
sb.Append("db.response.status_code (tag):")
172+
.Append(ResponseStatusCode)
173+
.Append(',');
174+
}
175+
176+
if (ResponseSubStatusCode is not null)
177+
{
178+
sb.Append("cosmosdb.response.sub_status_code (tag):")
179+
.Append(ResponseSubStatusCode)
180+
.Append(',');
181+
}
182+
147183
base.WriteAdditionalTags(sb);
148184
}
149185
}

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();

0 commit comments

Comments
 (0)