-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathOpenTelemetryExtensions.cs
More file actions
71 lines (60 loc) · 2.9 KB
/
OpenTelemetryExtensions.cs
File metadata and controls
71 lines (60 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Sentry.Internal.Extensions;
using Sentry.Internal.OpenTelemetry;
namespace Sentry.OpenTelemetry;
internal static class OpenTelemetryExtensions
{
public static SpanId AsSentrySpanId(this ActivitySpanId id) => SpanId.Parse(id.ToHexString());
public static ActivitySpanId AsActivitySpanId(this SpanId id) =>
ActivitySpanId.CreateFromString(id.ToString().AsSpan());
public static SentryId AsSentryId(this ActivityTraceId id) => SentryId.Parse(id.ToHexString());
public static ActivityTraceId AsActivityTraceId(this SentryId id) =>
ActivityTraceId.CreateFromString(id.ToString().AsSpan());
public static BaggageHeader AsBaggageHeader(this IEnumerable<KeyValuePair<string, string?>> baggage,
bool useSentryPrefix = false) =>
BaggageHeader.Create(
baggage.Where(member => member.Value != null)
.Select(kvp => (KeyValuePair<string, string>)kvp!),
useSentryPrefix
);
/// <summary>
/// The names that OpenTelemetry gives to attributes, by convention, have changed over time so we often need to
/// check for both the new attribute and any obsolete ones.
/// </summary>
/// <param name="attributes">The attributes to be searched</param>
/// <param name="attributeNames">The list of possible names for the attribute you want to retrieve</param>
/// <typeparam name="T">The expected type of the attribute</typeparam>
/// <returns>The first attribute it finds matching one of the supplied <paramref name="attributeNames"/>
/// or null, if no matching attribute is found
/// </returns>
private static T? GetFirstMatchingAttribute<T>(this IDictionary<string, object?> attributes,
params string[] attributeNames)
{
foreach (var name in attributeNames)
{
if (attributes.TryGetTypedValue(name, out T value))
{
return value;
}
}
return default;
}
public static string? HttpMethodAttribute(this IDictionary<string, object?> attributes) =>
attributes.GetFirstMatchingAttribute<string>(
OtelSemanticConventions.AttributeHttpRequestMethod,
OtelSemanticConventions.AttributeHttpMethod // Fallback pre-1.5.0
);
public static string? UrlFullAttribute(this IDictionary<string, object?> attributes) =>
attributes.GetFirstMatchingAttribute<string>(
OtelSemanticConventions.AttributeUrlFull,
OtelSemanticConventions.AttributeHttpUrl // Fallback pre-1.5.0
);
public static short? HttpResponseStatusCodeAttribute(this IDictionary<string, object?> attributes)
{
var statusCode = attributes.GetFirstMatchingAttribute<int?>(
OtelSemanticConventions.AttributeHttpResponseStatusCode
);
return statusCode is >= short.MinValue and <= short.MaxValue
? (short)statusCode.Value
: null;
}
}