-
Notifications
You must be signed in to change notification settings - Fork 774
/
LogRecordAttributeList.cs
334 lines (288 loc) · 10.7 KB
/
LogRecordAttributeList.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
#if NET && EXPOSE_EXPERIMENTAL_FEATURES
using System.Diagnostics.CodeAnalysis;
#endif
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Logs;
#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
/// Stores attributes to be added to a log message.
/// </summary>
/// <remarks><inheritdoc cref="Logger" path="/remarks"/></remarks>
#if NET
[Experimental(DiagnosticDefinitions.LogsBridgeExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
#endif
public
#else
/// <summary>
/// Stores attributes to be added to a log message.
/// </summary>
internal
#endif
struct LogRecordAttributeList : IReadOnlyList<KeyValuePair<string, object?>>
{
internal const int OverflowMaxCount = 8;
internal const int OverflowAdditionalCapacity = 16;
internal List<KeyValuePair<string, object?>>? OverflowAttributes;
private static readonly IReadOnlyList<KeyValuePair<string, object?>> Empty = Array.Empty<KeyValuePair<string, object?>>();
private KeyValuePair<string, object?> attribute1;
private KeyValuePair<string, object?> attribute2;
private KeyValuePair<string, object?> attribute3;
private KeyValuePair<string, object?> attribute4;
private KeyValuePair<string, object?> attribute5;
private KeyValuePair<string, object?> attribute6;
private KeyValuePair<string, object?> attribute7;
private KeyValuePair<string, object?> attribute8;
private int count;
/// <inheritdoc/>
public readonly int Count => this.count;
/// <inheritdoc/>
public KeyValuePair<string, object?> this[int index]
{
readonly get
{
if (this.OverflowAttributes is not null)
{
Debug.Assert(index < this.OverflowAttributes.Count, "Invalid index accessed.");
return this.OverflowAttributes[index];
}
if ((uint)index >= (uint)this.count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return index switch
{
0 => this.attribute1,
1 => this.attribute2,
2 => this.attribute3,
3 => this.attribute4,
4 => this.attribute5,
5 => this.attribute6,
6 => this.attribute7,
7 => this.attribute8,
_ => default, // we shouldn't come here anyway.
};
}
set
{
if (this.OverflowAttributes is not null)
{
Debug.Assert(index < this.OverflowAttributes.Count, "Invalid index accessed.");
this.OverflowAttributes[index] = value;
return;
}
if ((uint)index >= (uint)this.count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
switch (index)
{
case 0: this.attribute1 = value; break;
case 1: this.attribute2 = value; break;
case 2: this.attribute3 = value; break;
case 3: this.attribute4 = value; break;
case 4: this.attribute5 = value; break;
case 5: this.attribute6 = value; break;
case 6: this.attribute7 = value; break;
case 7: this.attribute8 = value; break;
default:
Debug.Assert(false, "Unreachable code executed.");
break;
}
}
}
/// <summary>
/// Add an attribute.
/// </summary>
/// <param name="key">Attribute name.</param>
/// <returns>Attribute value.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public object? this[string key]
{
// Note: This only exists to enable collection initializer syntax
// like { ["key"] = value }.
set => this.Add(new KeyValuePair<string, object?>(key, value));
}
/// <summary>
/// Create a <see cref="LogRecordAttributeList"/> collection from an enumerable.
/// </summary>
/// <param name="attributes">Source attributes.</param>
/// <returns><see cref="LogRecordAttributeList"/>.</returns>
public static LogRecordAttributeList CreateFromEnumerable(IEnumerable<KeyValuePair<string, object?>> attributes)
{
Guard.ThrowIfNull(attributes);
LogRecordAttributeList logRecordAttributes = default;
logRecordAttributes.OverflowAttributes = new(attributes);
logRecordAttributes.count = logRecordAttributes.OverflowAttributes.Count;
return logRecordAttributes;
}
/// <summary>
/// Add an attribute.
/// </summary>
/// <param name="key">Attribute name.</param>
/// <param name="value">Attribute value.</param>
public void Add(string key, object? value)
=> this.Add(new KeyValuePair<string, object?>(key, value));
/// <summary>
/// Add an attribute.
/// </summary>
/// <param name="attribute">Attribute.</param>
public void Add(KeyValuePair<string, object?> attribute)
{
var count = this.count++;
if (count <= OverflowMaxCount)
{
switch (count)
{
case 0: this.attribute1 = attribute; return;
case 1: this.attribute2 = attribute; return;
case 2: this.attribute3 = attribute; return;
case 3: this.attribute4 = attribute; return;
case 4: this.attribute5 = attribute; return;
case 5: this.attribute6 = attribute; return;
case 6: this.attribute7 = attribute; return;
case 7: this.attribute8 = attribute; return;
case 8:
this.MoveAttributesToTheOverflowList();
break;
}
}
Debug.Assert(this.OverflowAttributes is not null, "Overflow attributes creation failure.");
this.OverflowAttributes!.Add(attribute);
}
/// <summary>
/// Removes all elements from the <see cref="LogRecordAttributeList"/>.
/// </summary>
public void Clear()
{
this.count = 0;
this.OverflowAttributes?.Clear();
}
/// <summary>
/// Adds attributes representing an <see cref="Exception"/> to the list.
/// </summary>
/// <param name="exception"><see cref="Exception"/>.</param>
public void RecordException(Exception exception)
{
Guard.ThrowIfNull(exception);
this.Add(SemanticConventions.AttributeExceptionType, exception.GetType().Name);
this.Add(SemanticConventions.AttributeExceptionMessage, exception.Message);
this.Add(SemanticConventions.AttributeExceptionStacktrace, exception.ToInvariantString());
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="LogRecordAttributeList"/>.
/// </summary>
/// <returns><see cref="Enumerator"/>.</returns>
public readonly Enumerator GetEnumerator()
=> new(in this);
/// <inheritdoc/>
readonly IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => this.GetEnumerator();
/// <inheritdoc/>
readonly IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
internal readonly IReadOnlyList<KeyValuePair<string, object?>> Export(ref List<KeyValuePair<string, object?>>? attributeStorage)
{
int count = this.count;
if (count <= 0)
{
return Empty;
}
var overflowAttributes = this.OverflowAttributes;
if (overflowAttributes != null)
{
// An allocation has already occurred, just use the list.
return overflowAttributes;
}
Debug.Assert(count <= 8, "Invalid size detected.");
attributeStorage ??= new List<KeyValuePair<string, object?>>(OverflowAdditionalCapacity);
// TODO: Perf test this, adjust as needed.
if (count > 0)
{
attributeStorage.Add(this.attribute1);
if (count == 1)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute2);
if (count == 2)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute3);
if (count == 3)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute4);
if (count == 4)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute5);
if (count == 5)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute6);
if (count == 6)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute7);
if (count == 7)
{
return attributeStorage;
}
attributeStorage.Add(this.attribute8);
}
return attributeStorage;
}
private void MoveAttributesToTheOverflowList()
{
Debug.Assert(this.count - 1 == OverflowMaxCount, "count did not match OverflowMaxCount");
var attributes = this.OverflowAttributes ??= new(OverflowAdditionalCapacity);
attributes.Add(this.attribute1);
attributes.Add(this.attribute2);
attributes.Add(this.attribute3);
attributes.Add(this.attribute4);
attributes.Add(this.attribute5);
attributes.Add(this.attribute6);
attributes.Add(this.attribute7);
attributes.Add(this.attribute8);
}
/// <summary>
/// Enumerates the elements of a <see cref="LogRecordAttributeList"/>.
/// </summary>
public struct Enumerator : IEnumerator<KeyValuePair<string, object?>>, IEnumerator
{
private LogRecordAttributeList attributes;
private int index;
internal Enumerator(in LogRecordAttributeList attributes)
{
this.index = -1;
this.attributes = attributes;
}
/// <inheritdoc/>
public readonly KeyValuePair<string, object?> Current
=> this.attributes[this.index];
/// <inheritdoc/>
readonly object IEnumerator.Current => this.Current;
/// <inheritdoc/>
public bool MoveNext()
{
this.index++;
return this.index < this.attributes.Count;
}
/// <inheritdoc/>
public readonly void Dispose()
{
}
/// <inheritdoc/>
readonly void IEnumerator.Reset()
=> throw new NotSupportedException();
}
}