-
Notifications
You must be signed in to change notification settings - Fork 772
/
LogRecordExtensions.cs
243 lines (216 loc) · 10.4 KB
/
LogRecordExtensions.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
// <copyright file="LogRecordExtensions.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.Runtime.CompilerServices;
using Google.Protobuf;
using OpenTelemetry.Logs;
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;
using OtlpResource = OpenTelemetry.Proto.Resource.V1;
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
internal static class LogRecordExtensions
{
internal static void AddBatch(
this OtlpCollector.ExportLogsServiceRequest request,
SdkLimitOptions sdkLimitOptions,
OtlpResource.Resource processResource,
in Batch<LogRecord> logRecordBatch)
{
var resourceLogs = new OtlpLogs.ResourceLogs
{
Resource = processResource,
};
request.ResourceLogs.Add(resourceLogs);
var scopeLogs = new OtlpLogs.ScopeLogs();
resourceLogs.ScopeLogs.Add(scopeLogs);
foreach (var logRecord in logRecordBatch)
{
var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions);
if (otlpLogRecord != null)
{
scopeLogs.LogRecords.Add(otlpLogRecord);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitOptions sdkLimitOptions)
{
OtlpLogs.LogRecord otlpLogRecord = null;
try
{
var timestamp = (ulong)logRecord.Timestamp.ToUnixTimeNanoseconds();
otlpLogRecord = new OtlpLogs.LogRecord
{
TimeUnixNano = timestamp,
ObservedTimeUnixNano = timestamp,
SeverityNumber = GetSeverityNumber(logRecord.Severity),
};
if (!string.IsNullOrWhiteSpace(logRecord.SeverityText))
{
otlpLogRecord.SeverityText = logRecord.SeverityText;
}
else if (logRecord.Severity.HasValue)
{
otlpLogRecord.SeverityText = logRecord.Severity.Value.ToShortName();
}
var attributeValueLengthLimit = sdkLimitOptions.LogRecordAttributeValueLengthLimit;
var attributeCountLimit = sdkLimitOptions.LogRecordAttributeCountLimit ?? int.MaxValue;
/*
// Removing this temporarily for stable release
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4776
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/3491
// First add the generic attributes like Category, EventId and Exception,
// so they are less likely being dropped because of AttributeCountLimit.
if (!string.IsNullOrEmpty(logRecord.CategoryName))
{
// TODO:
// 1. Track the following issue, and map CategoryName to Name
// if it makes it to log data model.
// https://github.com/open-telemetry/opentelemetry-specification/issues/2398
// 2. Confirm if this name for attribute is good.
otlpLogRecord.AddStringAttribute("dotnet.ilogger.category", logRecord.CategoryName, attributeValueLengthLimit, attributeCountLimit);
}
if (logRecord.EventId.Id != default)
{
otlpLogRecord.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id, attributeCountLimit);
}
if (!string.IsNullOrEmpty(logRecord.EventId.Name))
{
otlpLogRecord.AddStringAttribute(nameof(logRecord.EventId.Name), logRecord.EventId.Name, attributeValueLengthLimit, attributeCountLimit);
}
if (logRecord.Exception != null)
{
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name, attributeValueLengthLimit, attributeCountLimit);
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message, attributeValueLengthLimit, attributeCountLimit);
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString(), attributeValueLengthLimit, attributeCountLimit);
}
*/
bool bodyPopulatedFromFormattedMessage = false;
if (logRecord.FormattedMessage != null)
{
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = logRecord.FormattedMessage };
bodyPopulatedFromFormattedMessage = true;
}
if (logRecord.Attributes != null)
{
foreach (var attribute in logRecord.Attributes)
{
// Special casing {OriginalFormat}
// See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182
// for explanation.
if (attribute.Key.Equals("{OriginalFormat}") && !bodyPopulatedFromFormattedMessage)
{
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = attribute.Value as string };
}
else if (OtlpKeyValueTransformer.Instance.TryTransformTag(attribute, out var result, attributeValueLengthLimit))
{
otlpLogRecord.AddAttribute(result, attributeCountLimit);
}
}
}
if (logRecord.TraceId != default && logRecord.SpanId != default)
{
byte[] traceIdBytes = new byte[16];
byte[] spanIdBytes = new byte[8];
logRecord.TraceId.CopyTo(traceIdBytes);
logRecord.SpanId.CopyTo(spanIdBytes);
otlpLogRecord.TraceId = UnsafeByteOperations.UnsafeWrap(traceIdBytes);
otlpLogRecord.SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes);
otlpLogRecord.Flags = (uint)logRecord.TraceFlags;
}
logRecord.ForEachScope(ProcessScope, otlpLogRecord);
void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
{
foreach (var scopeItem in scope)
{
if (scopeItem.Key.Equals("{OriginalFormat}") || string.IsNullOrEmpty(scopeItem.Key))
{
// Ignore if the scope key is empty.
// Ignore if the scope key is {OriginalFormat}
// Attributes should not contain duplicates,
// and it is expensive to de-dup, so this
// exporter is going to pass the scope items as is.
// {OriginalFormat} is going to be the key
// if one uses formatted string for scopes
// and if there are nested scopes, this is
// guaranteed to create duplicate keys.
// Similar for empty keys, which is what the
// key is going to be if user simply
// passes a string as scope.
// To summarize this exporter only allows
// IReadOnlyList<KeyValuePair<string, object?>>
// or IEnumerable<KeyValuePair<string, object?>>.
// and expect users to provide unique keys.
// Note: It is possible that we allow users
// to override this exporter feature. So not blocking
// empty/{OriginalFormat} in the SDK itself.
}
else
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
{
otlpLog.AddAttribute(result, attributeCountLimit);
}
}
}
}
}
catch (Exception ex)
{
OpenTelemetryProtocolExporterEventSource.Log.CouldNotTranslateLogRecord(ex.Message);
}
return otlpLogRecord;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddAttribute(this OtlpLogs.LogRecord logRecord, OtlpCommon.KeyValue attribute, int maxAttributeCount)
{
if (logRecord.Attributes.Count < maxAttributeCount)
{
logRecord.Attributes.Add(attribute);
}
else
{
logRecord.DroppedAttributesCount++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddStringAttribute(this OtlpLogs.LogRecord logRecord, string key, string value, int? maxValueLength, int maxAttributeCount)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
{
logRecord.AddAttribute(result, maxAttributeCount);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddIntAttribute(this OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result))
{
logRecord.AddAttribute(result, maxAttributeCount);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpLogs.SeverityNumber GetSeverityNumber(LogRecordSeverity? severity)
{
if (!severity.HasValue)
{
return OtlpLogs.SeverityNumber.Unspecified;
}
return (OtlpLogs.SeverityNumber)(int)severity.Value;
}
}