-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtelInstrumentation.cs
More file actions
231 lines (209 loc) · 11 KB
/
Copy pathOtelInstrumentation.cs
File metadata and controls
231 lines (209 loc) · 11 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Identity.Client.Cache;
using Microsoft.Identity.Client.Core;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.TelemetryCore;
using Microsoft.Identity.Client.TelemetryCore.Internal.Events;
using Microsoft.Identity.Client.TelemetryCore.OpenTelemetry;
namespace Microsoft.Identity.Client.Platforms.Features.OpenTelemetry
{
/// <summary>
/// Class to hold the OpenTelemetry objects used by MSAL.
/// </summary>
internal class OtelInstrumentation : IOtelInstrumentation
{
/// <summary>
/// Constant to hold the name of the Meter.
/// </summary>
public const string MeterName = "MicrosoftIdentityClient_Common_Meter";
private const string SuccessCounterName = "MsalSuccess";
private const string FailedCounterName = "MsalFailure";
private const string TotalDurationHistogramName = "MsalTotalDuration.1A";
private const string DurationInL1CacheHistogramName = "MsalDurationInL1CacheInUs.1B";
private const string DurationInL2CacheHistogramName = "MsalDurationInL2Cache.1A";
private const string DurationInHttpHistogramName = "MsalDurationInHttp.1A";
private const string DurationInExtensionInMsHistogram = "MsalDurationInExtensionInMs.1B";
/// <summary>
/// Meter to hold the MSAL metrics.
/// </summary>
internal static readonly Meter Meter = new Meter(MeterName, "1.0.0");
/// <summary>
/// Counter to hold the number of successful token acquisition calls.
/// </summary>
internal static readonly Lazy<Counter<long>> s_successCounter = new(() => Meter.CreateCounter<long>(
SuccessCounterName,
description: "Number of successful token acquisition calls"));
/// <summary>
/// Counter to hold the number of failed token acquisition calls.
/// </summary>
internal static readonly Lazy<Counter<long>> s_failureCounter = new(() => Meter.CreateCounter<long>(
FailedCounterName,
description: "Number of failed token acquisition calls"));
/// <summary>
/// Histogram to record total duration in milliseconds of token acquisition calls.
/// </summary>
internal static readonly Lazy<Histogram<long>> s_durationTotal = new(() => Meter.CreateHistogram<long>(
TotalDurationHistogramName,
unit: "ms",
description: "Performance of token acquisition calls total latency"));
/// <summary>
/// Histogram to record total duration of token acquisition calls in microseconds(us) when token is fetched from L1 cache.
/// </summary>
internal static readonly Lazy<Histogram<long>> s_durationInL1CacheInUs = new(() => Meter.CreateHistogram<long>(
DurationInL1CacheHistogramName,
unit: "us",
description: "Performance of token acquisition calls total latency in microseconds when L1 cache is used."));
/// <summary>
/// Histogram to record duration in L2 cache for token acquisition calls.
/// </summary>
internal static readonly Lazy<Histogram<long>> s_durationInL2Cache = new Lazy<Histogram<long>>(() => Meter.CreateHistogram<long>(
DurationInL2CacheHistogramName,
unit: "ms",
description: "Performance of token acquisition calls cache latency"));
/// <summary>
/// Histogram to record duration in milliseconds in http when the token is fetched from identity provider.
/// </summary>
internal static readonly Lazy<Histogram<long>> s_durationInHttp = new Lazy<Histogram<long>>(() => Meter.CreateHistogram<long>(
DurationInHttpHistogramName,
unit: "ms",
description: "Performance of token acquisition calls network latency"));
/// <summary>
/// Histogram to record total duration of extension modifications in microseconds(us).
/// </summary>
internal static readonly Lazy<Histogram<long>> s_durationInExtensionInMs = new(() => Meter.CreateHistogram<long>(
DurationInExtensionInMsHistogram,
unit: "us",
description: "Performance of token acquisition calls extension latency."));
public OtelInstrumentation()
{
// Needed to fail fast if the runtime, like in-process Azure Functions, doesn't support OpenTelemetry
_ = Meter.Version;
}
// Aggregates the successful requests based on token source and cache refresh reason.
public void LogSuccessMetrics(
string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
CacheLevel cacheLevel,
long totalDurationInUs,
AuthenticationResultMetadata authResultMetadata,
ILoggerAdapter logger)
{
IncrementSuccessCounter(
platform,
apiId,
callerSdkId,
callerSdkVersion,
authResultMetadata.TokenSource,
authResultMetadata.CacheRefreshReason,
cacheLevel,
logger,
authResultMetadata.TelemetryTokenType);
if (s_durationTotal.Value.Enabled)
{
s_durationTotal.Value.Record(authResultMetadata.DurationTotalInMs,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.TokenSource, authResultMetadata.TokenSource),
new(TelemetryConstants.CacheLevel, cacheLevel),
new(TelemetryConstants.CacheRefreshReason, authResultMetadata.CacheRefreshReason),
new(TelemetryConstants.TokenType, authResultMetadata.TelemetryTokenType));
}
// Only log cache duration if L2 cache was used.
if (s_durationInL2Cache.Value.Enabled && cacheLevel == CacheLevel.L2Cache)
{
s_durationInL2Cache.Value.Record(authResultMetadata.DurationInCacheInMs,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.CacheRefreshReason, authResultMetadata.CacheRefreshReason));
}
// Only log duration in HTTP when token is fetched from IDP.
if (s_durationInHttp.Value.Enabled && authResultMetadata.TokenSource == TokenSource.IdentityProvider)
{
s_durationInHttp.Value.Record(authResultMetadata.DurationInHttpInMs,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.TokenType, authResultMetadata.TelemetryTokenType));
}
// Only log duration in microseconds when the cache level is L1.
if (s_durationInL1CacheInUs.Value.Enabled && authResultMetadata.TokenSource == TokenSource.Cache
&& authResultMetadata.CacheLevel.Equals(CacheLevel.L1Cache))
{
s_durationInL1CacheInUs.Value.Record(totalDurationInUs,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.TokenSource, authResultMetadata.TokenSource),
new(TelemetryConstants.CacheLevel, authResultMetadata.CacheLevel),
new(TelemetryConstants.CacheRefreshReason, authResultMetadata.CacheRefreshReason));
}
if (s_durationInExtensionInMs.Value.Enabled)
{
s_durationInExtensionInMs.Value.Record(authResultMetadata.DurationCreatingExtendedTokenInUs,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.TokenSource, authResultMetadata.TokenSource),
new(TelemetryConstants.CacheLevel, authResultMetadata.CacheLevel),
new(TelemetryConstants.TokenType, authResultMetadata.TelemetryTokenType));
}
}
public void IncrementSuccessCounter(string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
TokenSource tokenSource,
CacheRefreshReason cacheRefreshReason,
CacheLevel cacheLevel,
ILoggerAdapter logger,
int tokenType)
{
if (s_successCounter.Value.Enabled)
{
s_successCounter.Value.Add(1,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.CallerSdkId, callerSdkId ?? string.Empty + "," + callerSdkVersion ?? string.Empty),
new(TelemetryConstants.TokenSource, tokenSource),
new(TelemetryConstants.CacheRefreshReason, cacheRefreshReason),
new(TelemetryConstants.CacheLevel, cacheLevel),
new(TelemetryConstants.TokenType, tokenType));
logger.Verbose(() => "[OpenTelemetry] Completed incrementing to success counter.");
}
}
public void LogFailureMetrics(string platform,
string errorCode,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
CacheRefreshReason cacheRefreshReason,
int tokenType,
string rawStsErrorCode = null)
{
if (!s_failureCounter.Value.Enabled)
return;
var tags = new TagList
{
{ TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion() },
{ TelemetryConstants.Platform, platform },
{ TelemetryConstants.ErrorCode, errorCode },
{ TelemetryConstants.ApiId, apiId },
{ TelemetryConstants.CallerSdkId, callerSdkId ?? string.Empty + "," + callerSdkVersion ?? string.Empty },
{ TelemetryConstants.CacheRefreshReason, cacheRefreshReason },
{ TelemetryConstants.TokenType, tokenType }
};
if (!string.IsNullOrEmpty(rawStsErrorCode))
tags.Add(TelemetryConstants.RawStsErrorCode, rawStsErrorCode);
s_failureCounter.Value.Add(1, in tags);
}
}
}