-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathOpenTelemetryCoreRecorder.cs
More file actions
308 lines (273 loc) · 14.4 KB
/
OpenTelemetryCoreRecorder.cs
File metadata and controls
308 lines (273 loc) · 14.4 KB
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;
using Microsoft.Azure.Documents;
/// <summary>
/// This class is used to add information in an Activity tags for OpenTelemetry.
/// Refer to <see href="https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058"/> for more details.
/// </summary>
internal struct OpenTelemetryCoreRecorder : IDisposable
{
internal const string CosmosDb = "cosmosdb";
private static readonly string otelStabilityMode = Environment.GetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN");
private readonly DiagnosticScope scope = default;
private readonly CosmosThresholdOptions config = null;
private readonly Activity activity = null;
private readonly OperationType operationType = OperationType.Invalid;
private readonly string connectionModeCache = null;
private readonly QueryTextMode? queryTextMode = null;
private OpenTelemetryAttributes response = null;
/// <summary>
/// Maps exception types to actions that record their OpenTelemetry attributes.
/// </summary>
internal static IDictionary<Type, Action<Exception, DiagnosticScope>> OTelCompatibleExceptions = new Dictionary<Type, Action<Exception, DiagnosticScope>>()
{
{ typeof(CosmosNullReferenceException), (exception, scope) => CosmosNullReferenceException.RecordOtelAttributes((CosmosNullReferenceException)exception, scope)},
{ typeof(CosmosObjectDisposedException), (exception, scope) => CosmosObjectDisposedException.RecordOtelAttributes((CosmosObjectDisposedException)exception, scope)},
{ typeof(CosmosOperationCanceledException), (exception, scope) => CosmosOperationCanceledException.RecordOtelAttributes((CosmosOperationCanceledException)exception, scope)},
{ typeof(CosmosException), (exception, scope) => CosmosException.RecordOtelAttributes((CosmosException)exception, scope)},
{ typeof(ChangeFeedProcessorUserException), (exception, scope) => ChangeFeedProcessorUserException.RecordOtelAttributes((ChangeFeedProcessorUserException)exception, scope)}
};
private OpenTelemetryCoreRecorder(DiagnosticScope scope)
{
this.scope = scope;
this.scope.Start();
}
private OpenTelemetryCoreRecorder(string operationName)
{
this.activity = new Activity(operationName);
this.activity.Start();
}
private OpenTelemetryCoreRecorder(
DiagnosticScope scope,
string operationName,
string containerName,
string databaseName,
OperationType operationType,
CosmosClientContext clientContext,
CosmosThresholdOptions config,
QueryTextMode queryTextMode)
{
this.scope = scope;
this.config = config;
this.operationType = operationType;
this.connectionModeCache = Enum.GetName(typeof(ConnectionMode), clientContext.ClientOptions.ConnectionMode);
this.queryTextMode = queryTextMode;
if (scope.IsEnabled)
{
this.scope.Start();
this.Record(
operationName: operationName,
containerName: containerName,
databaseName: databaseName,
clientContext: clientContext);
}
}
/// <summary>
/// Creates a parent activity for scenarios where there are no listeners at the operation level but are present at the network level.
/// </summary>
/// <param name="networkScope">The network-level diagnostic scope.</param>
/// <returns>An instance of <see cref="OpenTelemetryCoreRecorder"/>.</returns>
public static OpenTelemetryCoreRecorder CreateNetworkLevelParentActivity(DiagnosticScope networkScope)
{
return new OpenTelemetryCoreRecorder(networkScope);
}
/// <summary>
/// Used for creating parent activity in scenario where there are no listeners at operation level and network level
/// </summary>
public static OpenTelemetryCoreRecorder CreateParentActivity(string operationName)
{
return new OpenTelemetryCoreRecorder(operationName);
}
/// <summary>
/// Used for creating parent activity in scenario where there are listeners at operation level
/// </summary>
public static OpenTelemetryCoreRecorder CreateOperationLevelParentActivity(
DiagnosticScope operationScope,
string operationName,
string containerName,
string databaseName,
Documents.OperationType operationType,
CosmosClientContext clientContext,
CosmosThresholdOptions config,
QueryTextMode queryTextMode)
{
return new OpenTelemetryCoreRecorder(
operationScope,
operationName,
containerName,
databaseName,
operationType,
clientContext,
config,
queryTextMode);
}
public bool IsEnabled => this.scope.IsEnabled;
public void Record(string key, string value)
{
if (this.IsEnabled)
{
this.scope.AddAttribute(key, value);
}
}
/// <summary>
/// Recording information
/// </summary>
/// <param name="operationName"></param>
/// <param name="containerName"></param>
/// <param name="databaseName"></param>
/// <param name="clientContext"></param>
public void Record(
string operationName,
string containerName,
string databaseName,
CosmosClientContext clientContext)
{
if (this.IsEnabled)
{
if (otelStabilityMode == OpenTelemetryStablityModes.DatabaseDupe)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ContainerName, containerName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ServerPort, clientContext.Client?.Endpoint?.Port.ToString());
this.scope.AddAttribute(OpenTelemetryAttributeKeys.UserAgent, clientContext.UserAgent);
}
else
{
// Classic Appinsights Support
this.scope.AddAttribute(AppInsightClassicAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.ContainerName, containerName);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.UserAgent, clientContext.UserAgent);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.MachineId, VmMetadataApiHandler.GetMachineId());
}
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbSystemName, OpenTelemetryCoreRecorder.CosmosDb);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ClientId, clientContext?.Client?.Id);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ConnectionMode, this.connectionModeCache);
}
}
/// <summary>
/// Record attributes from response
/// </summary>
/// <param name="response"></param>
public void Record(OpenTelemetryAttributes response)
{
if (this.IsEnabled)
{
this.response = response;
}
}
/// <summary>
/// Record attributes during exception
/// </summary>
/// <param name="exception"></param>
public void MarkFailed(Exception exception)
{
if (this.IsEnabled)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionStacktrace, exception.StackTrace);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionType, exception.GetType().Name);
// If Exception is not registered with open Telemetry
if (!OpenTelemetryCoreRecorder.IsExceptionRegistered(exception, this.scope))
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionMessage, exception.Message);
}
if (exception is not CosmosException || (exception is CosmosException cosmosException
&& !DiagnosticsFilterHelper
.IsSuccessfulResponse(cosmosException.StatusCode, cosmosException.SubStatusCode)))
{
this.scope.Failed(exception);
}
}
}
/// <summary>
/// Checking if passed exception is registered with Open telemetry or Not
/// </summary>
/// <param name="exception"></param>
/// <param name="scope"></param>
/// <returns>tru/false</returns>
internal static bool IsExceptionRegistered(Exception exception, DiagnosticScope scope)
{
foreach (KeyValuePair<Type, Action<Exception, DiagnosticScope>> registeredExceptionHandlers in OpenTelemetryCoreRecorder.OTelCompatibleExceptions)
{
Type exceptionType = exception.GetType();
if (registeredExceptionHandlers.Key.IsAssignableFrom(exceptionType))
{
registeredExceptionHandlers.Value(exception, scope);
return true;
}
}
return false;
}
public void Dispose()
{
if (this.IsEnabled)
{
OperationType operationType
= (this.response == null || this.response?.OperationType == OperationType.Invalid) ? this.operationType : this.response.OperationType;
if (otelStabilityMode != OpenTelemetryStablityModes.DatabaseDupe)
{
string operationName = Enum.GetName(typeof(OperationType), operationType);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.OperationType, operationName);
}
if (this.response != null)
{
if (this.response.BatchSize is not null)
{
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.BatchSize, (int)this.response.BatchSize);
}
if (otelStabilityMode == OpenTelemetryStablityModes.DatabaseDupe)
{
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.StatusCode, (int)this.response.StatusCode);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.RequestContentLength, this.response.RequestContentLength);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
}
else
{
this.scope.AddAttribute(AppInsightClassicAttributeKeys.RequestContentLength, this.response.RequestContentLength);
this.scope.AddAttribute(AppInsightClassicAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
this.scope.AddIntegerAttribute(AppInsightClassicAttributeKeys.StatusCode, (int)this.response.StatusCode);
}
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.SubStatusCode, this.response.SubStatusCode);
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.RequestCharge, (int)this.response.RequestCharge);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ItemCount, this.response.ItemCount);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ActivityId, this.response.ActivityId);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.CorrelatedActivityId, this.response.CorrelatedActivityId);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ConsistencyLevel, this.response.ConsistencyLevel);
if (this.response.QuerySpec is not null)
{
if (this.queryTextMode == QueryTextMode.All ||
(this.queryTextMode == QueryTextMode.ParameterizedOnly && this.response.QuerySpec.ShouldSerializeParameters()))
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.QueryText, this.response.QuerySpec?.QueryText);
}
}
if (this.response.Diagnostics != null)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.Region, ClientTelemetryHelper.GetContactedRegions(this.response.Diagnostics.GetContactedRegions()));
CosmosDbEventSource.RecordDiagnosticsForRequests(this.config, operationType, this.response);
}
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(this.response.StatusCode, this.response.SubStatusCode))
{
this.scope.Failed($"{(int)this.response.StatusCode}/{this.response.SubStatusCode}");
}
}
this.scope.Dispose();
}
else
{
this.activity?.Stop();
}
}
}
}