-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathGenevaLogExporter.cs
453 lines (399 loc) · 16.8 KB
/
GenevaLogExporter.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// <copyright file="GenevaLogExporter.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>
#if NETSTANDARD2_0 || NET461
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
namespace OpenTelemetry.Exporter.Geneva;
public class GenevaLogExporter : GenevaBaseExporter<LogRecord>
{
private const int BUFFER_SIZE = 65360; // the maximum ETW payload (inclusive)
private readonly IReadOnlyDictionary<string, object> m_customFields;
private readonly string m_defaultEventName = "Log";
private readonly IReadOnlyDictionary<string, object> m_prepopulatedFields;
private readonly List<string> m_prepopulatedFieldKeys;
private static readonly ThreadLocal<byte[]> m_buffer = new ThreadLocal<byte[]>(() => null);
private readonly byte[] m_bufferEpilogue;
private static readonly string[] logLevels = new string[7]
{
"Trace", "Debug", "Information", "Warning", "Error", "Critical", "None",
};
private readonly IDataTransport m_dataTransport;
private bool isDisposed;
private Func<object, string> convertToJson;
public GenevaLogExporter(GenevaExporterOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (string.IsNullOrWhiteSpace(options.ConnectionString))
{
throw new ArgumentException($"{nameof(options.ConnectionString)} is invalid.");
}
// TODO: Validate mappings for reserved tablenames etc.
if (options.TableNameMappings != null)
{
var tempTableMappings = new Dictionary<string, string>(options.TableNameMappings.Count, StringComparer.Ordinal);
foreach (var kv in options.TableNameMappings)
{
if (Encoding.UTF8.GetByteCount(kv.Value) != kv.Value.Length)
{
throw new ArgumentException("The value: \"{tableName}\" provided for TableNameMappings option contains non-ASCII characters", kv.Value);
}
if (kv.Key == "*")
{
this.m_defaultEventName = kv.Value;
}
else
{
tempTableMappings[kv.Key] = kv.Value;
}
}
this.m_tableMappings = tempTableMappings;
}
var connectionStringBuilder = new ConnectionStringBuilder(options.ConnectionString);
switch (connectionStringBuilder.Protocol)
{
case TransportProtocol.Etw:
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new ArgumentException("ETW cannot be used on non-Windows operating systems.");
}
this.m_dataTransport = new EtwDataTransport(connectionStringBuilder.EtwSession);
break;
case TransportProtocol.Unix:
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new ArgumentException("Unix domain socket should not be used on Windows.");
}
var unixDomainSocketPath = connectionStringBuilder.ParseUnixDomainSocketPath();
this.m_dataTransport = new UnixDomainSocketDataTransport(unixDomainSocketPath);
break;
case TransportProtocol.Tcp:
throw new ArgumentException("TCP transport is not supported yet.");
case TransportProtocol.Udp:
throw new ArgumentException("UDP transport is not supported yet.");
default:
throw new ArgumentOutOfRangeException(nameof(connectionStringBuilder.Protocol));
}
this.convertToJson = options.ConvertToJson;
if (options.PrepopulatedFields != null)
{
this.m_prepopulatedFieldKeys = new List<string>();
var tempPrepopulatedFields = new Dictionary<string, object>(options.PrepopulatedFields.Count, StringComparer.Ordinal);
foreach (var kv in options.PrepopulatedFields)
{
tempPrepopulatedFields[kv.Key] = kv.Value;
this.m_prepopulatedFieldKeys.Add(kv.Key);
}
this.m_prepopulatedFields = tempPrepopulatedFields;
}
// TODO: Validate custom fields (reserved name? etc).
if (options.CustomFields != null)
{
var customFields = new Dictionary<string, object>(StringComparer.Ordinal);
foreach (var name in options.CustomFields)
{
customFields[name] = true;
}
this.m_customFields = customFields;
}
var buffer = new byte[BUFFER_SIZE];
var cursor = MessagePackSerializer.Serialize(buffer, 0, new Dictionary<string, object> { { "TimeFormat", "DateTime" } });
this.m_bufferEpilogue = new byte[cursor - 0];
Buffer.BlockCopy(buffer, 0, this.m_bufferEpilogue, 0, cursor - 0);
}
private readonly IReadOnlyDictionary<string, string> m_tableMappings;
public override ExportResult Export(in Batch<LogRecord> batch)
{
var result = ExportResult.Success;
foreach (var logRecord in batch)
{
try
{
var cursor = this.SerializeLogRecord(logRecord);
this.m_dataTransport.Send(m_buffer.Value, cursor - 0);
}
catch (Exception ex)
{
ExporterEventSource.Log.ExporterException(ex); // TODO: preallocate exception or no exception
result = ExportResult.Failure;
}
}
return result;
}
protected override void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
if (disposing)
{
// DO NOT Dispose m_buffer as it is a static type
try
{
(this.m_dataTransport as IDisposable)?.Dispose();
this.m_prepopulatedFieldKeys.Clear();
}
catch (Exception ex)
{
ExporterEventSource.Log.ExporterException(ex);
}
}
this.isDisposed = true;
base.Dispose(disposing);
}
internal bool IsUsingUnixDomainSocket
{
get => this.m_dataTransport is UnixDomainSocketDataTransport;
}
internal int SerializeLogRecord(LogRecord logRecord)
{
IReadOnlyList<KeyValuePair<string, object>> listKvp;
if (logRecord.State == null)
{
// When State is null, OTel SDK guarantees StateValues is populated
// TODO: Debug.Assert?
listKvp = logRecord.StateValues;
}
else
{
// Attempt to see if State could be ROL_KVP.
listKvp = logRecord.State as IReadOnlyList<KeyValuePair<string, object>>;
}
var name = logRecord.CategoryName;
// If user configured explicit TableName, use it.
if (this.m_tableMappings == null || !this.m_tableMappings.TryGetValue(name, out var eventName))
{
eventName = this.m_defaultEventName;
}
var buffer = m_buffer.Value;
if (buffer == null)
{
buffer = new byte[BUFFER_SIZE]; // TODO: handle OOM
m_buffer.Value = buffer;
}
/* Fluentd Forward Mode:
[
"Log",
[
[ <timestamp>, { "env_ver": "4.0", ... } ]
],
{ "TimeFormat": "DateTime" }
]
*/
// Structured log.
// 2 scenarios.
// 1. Structured logging with template
// eg:
// body
// "Hello from {food} {price}."
// part c
// food = onion
// price = 100
// TODO: 2. Structured with strongly typed logging.
var timestamp = logRecord.Timestamp;
var cursor = 0;
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 3);
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, eventName);
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 1);
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 2);
cursor = MessagePackSerializer.SerializeUtcDateTime(buffer, cursor, timestamp);
cursor = MessagePackSerializer.WriteMapHeader(buffer, cursor, ushort.MaxValue); // Note: always use Map16 for perf consideration
ushort cntFields = 0;
var idxMapSizePatch = cursor - 2;
if (this.m_prepopulatedFieldKeys != null)
{
for (int i = 0; i < this.m_prepopulatedFieldKeys.Count; i++)
{
var key = this.m_prepopulatedFieldKeys[i];
var value = this.m_prepopulatedFields[key];
switch (value)
{
case bool vb:
case byte vui8:
case sbyte vi8:
case short vi16:
case ushort vui16:
case int vi32:
case uint vui32:
case long vi64:
case ulong vui64:
case float vf:
case double vd:
case string vs:
break;
default:
value = this.convertToJson(value);
break;
}
cursor = AddPartAField(buffer, cursor, key, value);
cntFields += 1;
}
}
// Part A - core envelope
cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Name, eventName);
cntFields += 1;
cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Time, timestamp);
cntFields += 1;
// Part A - dt extension
if (logRecord.TraceId != default)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_dt_traceId");
// Note: ToHexString returns the pre-calculated hex representation without allocation
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, logRecord.TraceId.ToHexString());
cntFields += 1;
}
if (logRecord.SpanId != default)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_dt_spanId");
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, logRecord.SpanId.ToHexString());
cntFields += 1;
}
// Part A - ex extension
if (logRecord.Exception != null)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_ex_type");
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, logRecord.Exception.GetType().FullName);
cntFields += 1;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_ex_msg");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, logRecord.Exception.Message);
cntFields += 1;
}
// Part B
var logLevel = logRecord.LogLevel;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "severityText");
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, logLevels[(int)logLevel]);
cntFields += 1;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "severityNumber");
cursor = MessagePackSerializer.SerializeUInt8(buffer, cursor, GetSeverityNumber(logLevel));
cntFields += 1;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "name");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, name);
cntFields += 1;
bool hasEnvProperties = false;
bool bodyPopulated = false;
for (int i = 0; i < listKvp?.Count; i++)
{
var entry = listKvp[i];
// Iteration #1 - Get those fields which become dedicated columns
// i.e all Part B fields and opt-in Part C fields.
if (entry.Key == "{OriginalFormat}")
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "body");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, logRecord.FormattedMessage ?? Convert.ToString(entry.Value, CultureInfo.InvariantCulture));
cntFields += 1;
bodyPopulated = true;
continue;
}
else if (this.m_customFields == null || this.m_customFields.ContainsKey(entry.Key))
{
// TODO: the above null check can be optimized and avoided inside foreach.
if (entry.Value != null)
{
// null is not supported.
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, entry.Key);
cursor = MessagePackSerializer.Serialize(buffer, cursor, entry.Value);
cntFields += 1;
}
}
else
{
hasEnvProperties = true;
continue;
}
}
if (!bodyPopulated && logRecord.FormattedMessage != null)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "body");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, logRecord.FormattedMessage);
cntFields += 1;
}
if (hasEnvProperties)
{
// Iteration #2 - Get all "other" fields and collapse them into single field
// named "env_properties".
ushort envPropertiesCount = 0;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_properties");
cursor = MessagePackSerializer.WriteMapHeader(buffer, cursor, ushort.MaxValue);
int idxMapSizeEnvPropertiesPatch = cursor - 2;
for (int i = 0; i < listKvp.Count; i++)
{
var entry = listKvp[i];
if (entry.Key == "{OriginalFormat}" || this.m_customFields.ContainsKey(entry.Key))
{
continue;
}
else
{
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, entry.Key);
cursor = MessagePackSerializer.Serialize(buffer, cursor, entry.Value);
envPropertiesCount += 1;
}
}
cntFields += 1;
MessagePackSerializer.WriteUInt16(buffer, idxMapSizeEnvPropertiesPatch, envPropertiesCount);
}
var eventId = logRecord.EventId;
if (eventId != default)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "eventId");
cursor = MessagePackSerializer.SerializeInt32(buffer, cursor, eventId.Id);
cntFields += 1;
}
MessagePackSerializer.WriteUInt16(buffer, idxMapSizePatch, cntFields);
Buffer.BlockCopy(this.m_bufferEpilogue, 0, buffer, cursor, this.m_bufferEpilogue.Length);
cursor += this.m_bufferEpilogue.Length;
return cursor;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte GetSeverityNumber(LogLevel logLevel)
{
// Maps the Ilogger LogLevel to OpenTelemetry logging level.
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#mapping-of-severitynumber
// TODO: for improving perf simply do ((int)loglevel * 4) + 1
// or ((int)logLevel << 2) + 1
switch (logLevel)
{
case LogLevel.Trace:
return 1;
case LogLevel.Debug:
return 5;
case LogLevel.Information:
return 9;
case LogLevel.Warning:
return 13;
case LogLevel.Error:
return 17;
case LogLevel.Critical:
return 21;
// we reach default only for LogLevel.None
// but that is filtered out anyway.
// should we throw here then?
default:
return 1;
}
}
}
#endif