-
Notifications
You must be signed in to change notification settings - Fork 764
/
ActivityHelperExtensions.cs
381 lines (315 loc) · 17.1 KB
/
ActivityHelperExtensions.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// <copyright file="ActivityHelperExtensions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace
{
/// <summary>
/// Extension methods on Activity.
/// </summary>
internal static class ActivityHelperExtensions
{
/// <summary>
/// Gets the status of activity execution.
/// Activity class in .NET does not support 'Status'.
/// This extension provides a workaround to retrieve Status from special tags with key name otel.status_code and otel.status_description.
/// </summary>
/// <param name="activity">Activity instance.</param>
/// <param name="statusCode"><see cref="StatusCode"/>.</param>
/// <param name="statusDescription">Status description.</param>
/// <returns><see langword="true"/> if <see cref="Status"/> was found on the supplied Activity.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static bool TryGetStatus(this Activity activity, out StatusCode statusCode, out string statusDescription)
{
Debug.Assert(activity != null, "Activity should not be null");
ActivityStatusTagEnumerator state = default;
ActivityTagsEnumeratorFactory<ActivityStatusTagEnumerator>.Enumerate(activity, ref state);
if (!state.StatusCode.HasValue)
{
statusCode = default;
statusDescription = null;
return false;
}
statusCode = state.StatusCode.Value;
statusDescription = state.StatusDescription;
return true;
}
/// <summary>
/// Gets the value of a specific tag on an <see cref="Activity"/>.
/// </summary>
/// <param name="activity">Activity instance.</param>
/// <param name="tagName">Case-sensitive tag name to retrieve.</param>
/// <returns>Tag value or null if a match was not found.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static object GetTagValue(this Activity activity, string tagName)
{
Debug.Assert(activity != null, "Activity should not be null");
ActivitySingleTagEnumerator state = new ActivitySingleTagEnumerator(tagName);
ActivityTagsEnumeratorFactory<ActivitySingleTagEnumerator>.Enumerate(activity, ref state);
return state.Value;
}
/// <summary>
/// Checks if the user provided tag name is the first tag of the <see cref="Activity"/> and retrieves the tag value.
/// </summary>
/// <param name="activity">Activity instance.</param>
/// <param name="tagName">Tag name.</param>
/// <param name="tagValue">Tag value.</param>
/// <returns><see langword="true"/> if the first tag of the supplied Activity matches the user provide tag name.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryCheckFirstTag(this Activity activity, string tagName, out object tagValue)
{
Debug.Assert(activity != null, "Activity should not be null");
ActivityFirstTagEnumerator state = new ActivityFirstTagEnumerator(tagName);
ActivityTagsEnumeratorFactory<ActivityFirstTagEnumerator>.Enumerate(activity, ref state);
if (state.Value == null)
{
tagValue = null;
return false;
}
tagValue = state.Value;
return true;
}
/// <summary>
/// Enumerates all the key/value pairs on an <see cref="Activity"/> without performing an allocation.
/// </summary>
/// <typeparam name="T">The struct <see cref="IActivityEnumerator{T}"/> implementation to use for the enumeration.</typeparam>
/// <param name="activity">Activity instance.</param>
/// <param name="tagEnumerator">Tag enumerator.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static void EnumerateTags<T>(this Activity activity, ref T tagEnumerator)
where T : struct, IActivityEnumerator<KeyValuePair<string, object>>
{
Debug.Assert(activity != null, "Activity should not be null");
ActivityTagsEnumeratorFactory<T>.Enumerate(activity, ref tagEnumerator);
}
/// <summary>
/// Enumerates all the <see cref="ActivityLink"/>s on an <see cref="Activity"/> without performing an allocation.
/// </summary>
/// <typeparam name="T">The struct <see cref="IActivityEnumerator{T}"/> implementation to use for the enumeration.</typeparam>
/// <param name="activity">Activity instance.</param>
/// <param name="linkEnumerator">Link enumerator.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static void EnumerateLinks<T>(this Activity activity, ref T linkEnumerator)
where T : struct, IActivityEnumerator<ActivityLink>
{
Debug.Assert(activity != null, "Activity should not be null");
ActivityLinksEnumeratorFactory<T>.Enumerate(activity, ref linkEnumerator);
}
/// <summary>
/// Enumerates all the key/value pairs on an <see cref="ActivityLink"/> without performing an allocation.
/// </summary>
/// <typeparam name="T">The struct <see cref="IActivityEnumerator{T}"/> implementation to use for the enumeration.</typeparam>
/// <param name="activityLink">ActivityLink instance.</param>
/// <param name="tagEnumerator">Tag enumerator.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static void EnumerateTags<T>(this ActivityLink activityLink, ref T tagEnumerator)
where T : struct, IActivityEnumerator<KeyValuePair<string, object>>
{
ActivityTagsEnumeratorFactory<T>.Enumerate(activityLink, ref tagEnumerator);
}
/// <summary>
/// Enumerates all the <see cref="ActivityEvent"/>s on an <see cref="Activity"/> without performing an allocation.
/// </summary>
/// <typeparam name="T">The struct <see cref="IActivityEnumerator{T}"/> implementation to use for the enumeration.</typeparam>
/// <param name="activity">Activity instance.</param>
/// <param name="eventEnumerator">Event enumerator.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static void EnumerateEvents<T>(this Activity activity, ref T eventEnumerator)
where T : struct, IActivityEnumerator<ActivityEvent>
{
Debug.Assert(activity != null, "Activity should not be null");
ActivityEventsEnumeratorFactory<T>.Enumerate(activity, ref eventEnumerator);
}
/// <summary>
/// Enumerates all the key/value pairs on an <see cref="ActivityEvent"/> without performing an allocation.
/// </summary>
/// <typeparam name="T">The struct <see cref="IActivityEnumerator{T}"/> implementation to use for the enumeration.</typeparam>
/// <param name="activityEvent">ActivityEvent instance.</param>
/// <param name="tagEnumerator">Tag enumerator.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ActivityProcessor is hot path")]
public static void EnumerateTags<T>(this ActivityEvent activityEvent, ref T tagEnumerator)
where T : struct, IActivityEnumerator<KeyValuePair<string, object>>
{
ActivityTagsEnumeratorFactory<T>.Enumerate(activityEvent, ref tagEnumerator);
}
private struct ActivitySingleTagEnumerator : IActivityEnumerator<KeyValuePair<string, object>>
{
public object Value;
private readonly string tagName;
public ActivitySingleTagEnumerator(string tagName)
{
this.tagName = tagName;
this.Value = null;
}
public bool ForEach(KeyValuePair<string, object> item)
{
if (item.Key == this.tagName)
{
this.Value = item.Value;
return false;
}
return true;
}
}
private struct ActivityStatusTagEnumerator : IActivityEnumerator<KeyValuePair<string, object>>
{
public StatusCode? StatusCode;
public string StatusDescription;
public bool ForEach(KeyValuePair<string, object> item)
{
switch (item.Key)
{
case SpanAttributeConstants.StatusCodeKey:
this.StatusCode = StatusHelper.GetStatusCodeForTagValue(item.Value as string);
break;
case SpanAttributeConstants.StatusDescriptionKey:
this.StatusDescription = item.Value as string;
break;
}
return !this.StatusCode.HasValue || this.StatusDescription == null;
}
}
private struct ActivityFirstTagEnumerator : IActivityEnumerator<KeyValuePair<string, object>>
{
public object Value;
private readonly string tagName;
public ActivityFirstTagEnumerator(string tagName)
{
this.tagName = tagName;
this.Value = null;
}
public bool ForEach(KeyValuePair<string, object> item)
{
if (item.Key == this.tagName)
{
this.Value = item.Value;
}
return false;
}
}
private static class ActivityTagsEnumeratorFactory<TState>
where TState : struct, IActivityEnumerator<KeyValuePair<string, object>>
{
private static readonly object EmptyActivityTagObjects = typeof(Activity).GetField("s_emptyTagObjects", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
private static readonly object EmptyActivityEventTags = typeof(ActivityEvent).GetField("s_emptyTags", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
private static readonly DictionaryEnumerator<string, object, TState>.AllocationFreeForEachDelegate
ActivityTagObjectsEnumerator = DictionaryEnumerator<string, object, TState>.BuildAllocationFreeForEachDelegate(
typeof(Activity).GetField("_tags", BindingFlags.Instance | BindingFlags.NonPublic).FieldType);
private static readonly DictionaryEnumerator<string, object, TState>.AllocationFreeForEachDelegate
ActivityTagsCollectionEnumerator = DictionaryEnumerator<string, object, TState>.BuildAllocationFreeForEachDelegate(typeof(ActivityTagsCollection));
private static readonly DictionaryEnumerator<string, object, TState>.ForEachDelegate ForEachTagValueCallbackRef = ForEachTagValueCallback;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enumerate(Activity activity, ref TState state)
{
var tagObjects = activity.TagObjects;
if (ReferenceEquals(tagObjects, EmptyActivityTagObjects))
{
return;
}
ActivityTagObjectsEnumerator(
tagObjects,
ref state,
ForEachTagValueCallbackRef);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enumerate(ActivityLink activityLink, ref TState state)
{
var tags = activityLink.Tags;
if (tags is null)
{
return;
}
ActivityTagsCollectionEnumerator(
tags,
ref state,
ForEachTagValueCallbackRef);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enumerate(ActivityEvent activityEvent, ref TState state)
{
var tags = activityEvent.Tags;
if (ReferenceEquals(tags, EmptyActivityEventTags))
{
return;
}
ActivityTagsCollectionEnumerator(
tags,
ref state,
ForEachTagValueCallbackRef);
}
private static bool ForEachTagValueCallback(ref TState state, KeyValuePair<string, object> item)
=> state.ForEach(item);
}
private static class ActivityLinksEnumeratorFactory<TState>
where TState : struct, IActivityEnumerator<ActivityLink>
{
private static readonly object EmptyActivityLinks = typeof(Activity).GetField("s_emptyLinks", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
private static readonly ListEnumerator<ActivityLink, TState>.AllocationFreeForEachDelegate
ActivityLinksEnumerator = ListEnumerator<ActivityLink, TState>.BuildAllocationFreeForEachDelegate(
typeof(Activity).GetField("_links", BindingFlags.Instance | BindingFlags.NonPublic).FieldType);
private static readonly ListEnumerator<ActivityLink, TState>.ForEachDelegate ForEachLinkCallbackRef = ForEachLinkCallback;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enumerate(Activity activity, ref TState state)
{
var activityLinks = activity.Links;
if (ReferenceEquals(activityLinks, EmptyActivityLinks))
{
return;
}
ActivityLinksEnumerator(
activityLinks,
ref state,
ForEachLinkCallbackRef);
}
private static bool ForEachLinkCallback(ref TState state, ActivityLink item)
=> state.ForEach(item);
}
private static class ActivityEventsEnumeratorFactory<TState>
where TState : struct, IActivityEnumerator<ActivityEvent>
{
private static readonly object EmptyActivityEvents = typeof(Activity).GetField("s_emptyEvents", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
private static readonly ListEnumerator<ActivityEvent, TState>.AllocationFreeForEachDelegate
ActivityEventsEnumerator = ListEnumerator<ActivityEvent, TState>.BuildAllocationFreeForEachDelegate(
typeof(Activity).GetField("_events", BindingFlags.Instance | BindingFlags.NonPublic).FieldType);
private static readonly ListEnumerator<ActivityEvent, TState>.ForEachDelegate ForEachEventCallbackRef = ForEachEventCallback;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enumerate(Activity activity, ref TState state)
{
var activityEvents = activity.Events;
if (ReferenceEquals(activityEvents, EmptyActivityEvents))
{
return;
}
ActivityEventsEnumerator(
activityEvents,
ref state,
ForEachEventCallbackRef);
}
private static bool ForEachEventCallback(ref TState state, ActivityEvent item)
=> state.ForEach(item);
}
}
}