-
Notifications
You must be signed in to change notification settings - Fork 790
/
Copy pathTelemetrySpan.cs
345 lines (308 loc) · 11.3 KB
/
TelemetrySpan.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace;
/// <summary>
/// Span represents a single operation within a trace.
/// </summary>
/// <remarks>TelemetrySpan is a wrapper around <see cref="System.Diagnostics.Activity"/> class.</remarks>
public class TelemetrySpan : IDisposable
{
internal static readonly TelemetrySpan NoopInstance = new(null);
internal readonly Activity? Activity;
internal TelemetrySpan(Activity? activity)
{
this.Activity = activity;
}
/// <summary>
/// Gets the span context.
/// </summary>
public SpanContext Context
=> this.Activity == null ? default : new SpanContext(this.Activity.Context);
/// <summary>
/// Gets a value indicating whether this span will be recorded.
/// </summary>
public bool IsRecording
=> this.Activity?.IsAllDataRequested == true;
/// <summary>
/// Gets the identity of the parent span id, if any.
/// </summary>
public ActivitySpanId ParentSpanId
=> this.Activity?.ParentSpanId ?? default;
/// <summary>
/// Sets the status of the span execution.
/// </summary>
/// <param name="value">Status to be set.</param>
public void SetStatus(Status value)
{
#pragma warning disable
this.Activity.SetStatus(value);
#pragma warning restore
}
/// <summary>
/// Updates the <see cref="TelemetrySpan"/> name.
///
/// If used, this will override the name provided via StartSpan method overload.
/// Upon this update, any sampling behavior based on <see cref="TelemetrySpan"/> name will depend on the
/// implementation.
/// </summary>
/// <param name="name">Name of the span.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan UpdateName(string name)
{
if (this.Activity != null)
{
this.Activity.DisplayName = name;
}
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="value">Attribute value.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, string? value)
{
this.SetAttributeInternal(key, value);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="value">Attribute value.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, int value)
{
this.SetAttributeInternal(key, value);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="value">Attribute value.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, bool value)
{
this.SetAttributeInternal(key, value);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="value">Attribute value.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, double value)
{
this.SetAttributeInternal(key, value);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="values">Attribute values.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, string?[]? values)
{
this.SetAttributeInternal(key, values);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="values">Attribute values.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, int[]? values)
{
this.SetAttributeInternal(key, values);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="values">Attribute values.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, bool[]? values)
{
this.SetAttributeInternal(key, values);
return this;
}
/// <summary>
/// Sets a new attribute on the span.
/// </summary>
/// <param name="key">Attribute key.</param>
/// <param name="values">Attribute values.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan SetAttribute(string key, double[]? values)
{
this.SetAttributeInternal(key, values);
return this;
}
/// <summary>
/// Adds a single Event to the <see cref="TelemetrySpan"/>.
/// </summary>
/// <param name="name">Name of the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name)
{
this.AddEventInternal(name);
return this;
}
/// <summary>
/// Adds a single Event to the <see cref="TelemetrySpan"/>.
/// </summary>
/// <param name="name">Name of the event.</param>
/// <param name="timestamp">Timestamp of the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name, DateTimeOffset timestamp)
{
this.AddEventInternal(name, timestamp);
return this;
}
/// <summary>
/// Adds a single Event to the <see cref="TelemetrySpan"/>.
/// </summary>
/// <param name="name">Name of the event.</param>
/// <param name="attributes">Attributes for the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name, SpanAttributes? attributes)
{
this.AddEventInternal(name, default, attributes?.Attributes);
return this;
}
/// <summary>
/// Adds a single Event to the <see cref="TelemetrySpan"/>.
/// </summary>
/// <param name="name">Name of the event.</param>
/// <param name="timestamp">Timestamp of the event.</param>
/// <param name="attributes">Attributes for the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name, DateTimeOffset timestamp, SpanAttributes? attributes)
{
this.AddEventInternal(name, timestamp, attributes?.Attributes);
return this;
}
/// <summary>
/// End the span.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void End()
{
this.Activity?.Stop();
}
/// <summary>
/// End the span.
/// </summary>
/// <param name="endTimestamp">End timestamp.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void End(DateTimeOffset endTimestamp)
{
var activity = this.Activity;
if (activity != null)
{
activity.SetEndTime(endTimestamp.UtcDateTime);
activity.Stop();
}
}
/// <summary>
/// Record Exception.
/// </summary>
/// <param name="ex">Exception to be recorded.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan RecordException(Exception? ex)
{
if (ex == null)
{
return this;
}
return this.RecordException(ex.GetType().Name, ex.Message, ex.ToInvariantString());
}
/// <summary>
/// Record Exception.
/// </summary>
/// <param name="type">Type of the exception to be recorded.</param>
/// <param name="message">Message of the exception to be recorded.</param>
/// <param name="stacktrace">Stacktrace of the exception to be recorded.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
public TelemetrySpan RecordException(string? type, string? message, string? stacktrace)
{
SpanAttributes attributes = new SpanAttributes();
if (!string.IsNullOrWhiteSpace(type))
{
attributes.Add(SemanticConventions.AttributeExceptionType, type);
}
if (!string.IsNullOrWhiteSpace(stacktrace))
{
attributes.Add(SemanticConventions.AttributeExceptionStacktrace, stacktrace);
}
if (!string.IsNullOrWhiteSpace(message))
{
attributes.Add(SemanticConventions.AttributeExceptionMessage, message);
}
if (attributes.Attributes.Count != 0)
{
this.AddEvent(SemanticConventions.AttributeExceptionEventName, attributes);
}
return this;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Marks the span as current.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Activate()
=> Activity.Current = this.Activity;
/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.Activity?.Dispose();
}
}
private void SetAttributeInternal(string key, object? value)
{
if (this.IsRecording)
{
this.Activity!.SetTag(key, value);
}
}
private void AddEventInternal(string name, DateTimeOffset timestamp = default, ActivityTagsCollection? tags = null)
{
if (this.IsRecording)
{
this.Activity!.AddEvent(new ActivityEvent(name, timestamp, tags));
}
}
}