-
Notifications
You must be signed in to change notification settings - Fork 495
/
OpenTelemetryCoreRecorder.cs
258 lines (225 loc) · 11.2 KB
/
OpenTelemetryCoreRecorder.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
//------------------------------------------------------------
// 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 ref. https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058
/// </summary>
internal struct OpenTelemetryCoreRecorder : IDisposable
{
private const string CosmosDb = "cosmosdb";
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 OpenTelemetryAttributes response = null;
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)
{
this.scope = scope;
this.config = config;
this.operationType = operationType;
this.connectionModeCache = Enum.GetName(typeof(ConnectionMode), clientContext.ClientOptions.ConnectionMode);
if (scope.IsEnabled)
{
this.scope.Start();
this.Record(
operationName: operationName,
containerName: containerName,
databaseName: databaseName,
clientContext: clientContext);
}
}
/// <summary>
/// Used for creating parent activity in scenario where there are no listeners at operation level
/// but they are present at network level
/// </summary>
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)
{
return new OpenTelemetryCoreRecorder(
operationScope,
operationName,
containerName,
databaseName,
operationType,
clientContext,
config);
}
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)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbOperation, operationName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbName, databaseName);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ContainerName, containerName);
// Other information
this.scope.AddAttribute(OpenTelemetryAttributeKeys.DbSystemName, OpenTelemetryCoreRecorder.CosmosDb);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.MachineId, VmMetadataApiHandler.GetMachineId());
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ServerAddress, clientContext.Client?.Endpoint?.Host);
// Client Information
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ClientId, clientContext?.Client?.Id);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.UserAgent, clientContext.UserAgent);
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;
this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, Enum.GetName(typeof(OperationType), operationType));
if (this.response != null)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.RequestContentLength, this.response.RequestContentLength);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.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);
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();
}
}
}
}