-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathOpenTelemetryConfigurationExtensionsTests.cs
364 lines (309 loc) · 16.7 KB
/
OpenTelemetryConfigurationExtensionsTests.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Azure.Identity;
using Azure.Monitor.OpenTelemetry.Exporter;
using FluentAssertions;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs.Script.Configuration;
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WebJobs.Script.Tests;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Xunit;
namespace Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.OpenTelemetry
{
public class OpenTelemetryConfigurationExtensionsTests
{
private readonly string _loggingPath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "Logging");
[Fact]
public void ConfigureTelemetry_Should_UseNothingIfNoKeysOrEndpointsPresent()
{
IServiceCollection sc = default;
var hostBuilder = new HostBuilder()
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((ctx, lb) => lb.ConfigureTelemetry(ctx))
.ConfigureServices(s => sc = s);
using IHost host = hostBuilder.Build();
// Assert
sc.Should().NotBeNullOrEmpty();
HasOtelServices(sc).Should().BeFalse();
host.Services.GetService<TelemetryClient>().Should().BeNull();
}
[Fact]
public void ConfigureTelemetry_Should_UseApplicationInsightsByDefaultIfKeyPresent()
{
IServiceCollection sc = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(c =>
{
c.AddInMemoryCollection(new Dictionary<string, string>
{
{ "APPINSIGHTS_INSTRUMENTATIONKEY", "some_key" },
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=some_other_key" },
{ ConfigurationPath.Combine(_loggingPath, "ApplicationInsights", "SamplingSettings", "IsEnabled"), "false" },
{ ConfigurationPath.Combine(_loggingPath, "ApplicationInsights", "SnapshotConfiguration", "IsEnabled"), "false" }
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((ctx, lb) => lb.ConfigureTelemetry(ctx))
.ConfigureServices(s => sc = s);
using IHost host = hostBuilder.Build();
// Assert
sc.Should().NotBeNullOrEmpty();
HasOtelServices(sc).Should().BeFalse();
host.Services.GetService<TelemetryClient>().Should().NotBeNull();
}
[Fact]
public void ConfigureTelemetry_Should_UseApplicationInsightsWhenModeSetAndKeysPresent()
{
IServiceCollection sc = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(c =>
{
c.AddInMemoryCollection(new Dictionary<string, string>
{
{ "APPINSIGHTS_INSTRUMENTATIONKEY", "some_key" },
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=some_key" },
{ ConfigurationPath.Combine(_loggingPath, "ApplicationInsights", "SamplingSettings", "IsEnabled"), "false" },
{ ConfigurationPath.Combine(_loggingPath, "ApplicationInsights", "SnapshotConfiguration", "IsEnabled"), "false" },
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "telemetryMode"), TelemetryMode.ApplicationInsights.ToString() },
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((ctx, lb) => lb.ConfigureTelemetry(ctx))
.ConfigureServices(s => sc = s);
using IHost host = hostBuilder.Build();
// Assert
sc.Should().NotBeNullOrEmpty();
HasOtelServices(sc).Should().BeFalse();
var telemetryClient = host.Services.GetService<TelemetryClient>();
telemetryClient.Should().NotBeNull();
var telmetryConfig = host.Services.GetService<TelemetryConfiguration>();
telmetryConfig.Should().NotBeNull();
telmetryConfig.ConnectionString.Should().Be("InstrumentationKey=some_key");
}
[Fact]
public void ConfigureTelemetry_Should_UsesOpenTelemetryWhenModeSetAndAppInsightsKeysPresent()
{
IServiceCollection sc = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(c =>
{
c.AddInMemoryCollection(new Dictionary<string, string>
{
{ "APPINSIGHTS_INSTRUMENTATIONKEY", "some_key" },
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=key" },
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "telemetryMode"), TelemetryMode.OpenTelemetry.ToString() },
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((ctx, lb) => lb.ConfigureTelemetry(ctx))
.ConfigureServices(s => sc = s);
using IHost host = hostBuilder.Build();
// Assert
sc.Should().NotBeNullOrEmpty();
HasOtelServices(sc).Should().BeTrue();
host.Services.GetService<TelemetryClient>().Should().BeNull();
host.Services.GetService<TelemetryConfiguration>().Should().BeNull();
}
[Fact]
public void ConfigureTelemetry_Should_UsesOpenTelemetryWithOtlpExporterWhenEnvVarsSet()
{
IServiceCollection sc = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(c =>
{
c.AddInMemoryCollection(new Dictionary<string, string>
{
{ EnvironmentSettingNames.AppInsightsInstrumentationKey, "some_key" },
{ EnvironmentSettingNames.AppInsightsConnectionString, "InstrumentationKey=some_key" },
{ "OTEL_EXPORTER_OTLP_ENDPOINT", "https://otlp.nr-data.net" },
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "telemetryMode"), TelemetryMode.OpenTelemetry.ToString() },
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((ctx, lb) => lb.ConfigureTelemetry(ctx))
.ConfigureServices(s => sc = s);
using IHost host = hostBuilder.Build();
// Assert
sc.Should().NotBeNullOrEmpty();
HasOtelServices(sc).Should().BeTrue();
sc.Should().Contain(sd => sd.ServiceType.FullName == "OpenTelemetry.Trace.IConfigureTracerProviderBuilder");
sc.Should().Contain(sd => sd.ServiceType.FullName == "OpenTelemetry.Logs.IConfigureLoggerProviderBuilder");
host.Services.GetService<TelemetryClient>().Should().BeNull();
// Since no OTLP endpoint was given, this should all be null as well
var otlpOptions = host.Services.GetService<OtlpExporterOptions>();
otlpOptions?.Endpoint.Should().Be("https://otlp.nr-data.net");
host.Services.GetService<IOptions<OpenTelemetryLoggerOptions>>()?.Value?.Should().NotBeNull();
host.Services.GetService<IOptions<MetricReaderOptions>>()?.Value?.Should().NotBeNull();
host.Services.GetService<IOptions<BatchExportActivityProcessorOptions>>()?.Value?.Should().NotBeNull();
host.Services.GetService<TracerProvider>().Should().NotBeNull();
var logProviders = host.Services.GetServices<ILoggerProvider>();
logProviders.Should().NotBeNullOrEmpty().And.Contain(p => p is OpenTelemetryLoggerProvider);
}
[Fact]
public void OnEnd_SanitizesTags()
{
// Arrange
var activity = new Activity("TestActivity");
activity.AddTag("url.query", "?code=secret");
activity.AddTag("url.full", "https://func.net/api/HttpTrigger?code=secret");
// Act
ActivitySanitizingProcessor.Instance.OnEnd(activity);
// Assert
Assert.Equal("[Hidden Credential]", activity.GetTagItem("url.query"));
Assert.Equal("https://func.net/api/HttpTrigger[Hidden Credential]", activity.GetTagItem("url.full"));
}
[Fact]
public void OnEnd_DoesNotSanitizeNonSensitiveTags()
{
// Arrange
var activity = new Activity("TestActivity");
activity.AddTag("non-sensitive", "data");
// Act
ActivitySanitizingProcessor.Instance.OnEnd(activity);
// Assert
Assert.Equal("data", activity.GetTagItem("non-sensitive"));
}
[Fact]
public void ResourceDetectorLocalDevelopment2()
{
using var envVariables = SetupDefaultEnvironmentVariables();
FunctionsResourceDetector detector = new FunctionsResourceDetector();
Resource resource = detector.Detect();
Assert.Equal($"/subscriptions/AAAAA-AAAAA-AAAAA-AAA/resourceGroups/rg/providers/Microsoft.Web/sites/appName",
resource.Attributes.FirstOrDefault(a => a.Key == "cloud.resource.id").Value);
Assert.Equal($"EastUS", resource.Attributes.FirstOrDefault(a => a.Key == "cloud.region").Value);
}
[Fact]
public void ResourceDetectorLocalDevelopment()
{
FunctionsResourceDetector detector = new FunctionsResourceDetector();
Resource resource = detector.Detect();
Assert.Equal(4, resource.Attributes.Count());
}
[Fact]
public void ConfigureTelemetry_Should_UseOpenTelemetryWhenModeSetAndAppInsightsAuthStringClientIdPresent()
{
// Arrange
var clientId = Guid.NewGuid();
IServiceCollection serviceCollection = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
{ "APPLICATIONINSIGHTS_AUTHENTICATION_STRING", $"Authorization=AAD;ClientId={clientId}" },
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=key" },
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "telemetryMode"), TelemetryMode.OpenTelemetry.ToString() }
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((context, loggingBuilder) => loggingBuilder.ConfigureTelemetry(context))
.ConfigureServices(services => serviceCollection = services);
using var host = hostBuilder.Build();
// Act
var tracerProviderDescriptors = GetTracerProviderDescriptors(serviceCollection);
var resolvedClient = ExtractClientFromDescriptors(tracerProviderDescriptors);
// Extract the clientId from the client object
var clientIdValue = resolvedClient?.GetType().GetProperty("ClientId", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(resolvedClient)?.ToString();
// Assert
serviceCollection.Should().NotBeNullOrEmpty();
clientIdValue.Should().Be(clientId.ToString());
resolvedClient.GetType().Name.Should().Be("ManagedIdentityClient");
}
[Fact]
public void ConfigureTelemetry_Should_UseOpenTelemetryWhenModeSetAndAppInsightsAuthStringPresent()
{
// Arrange
IServiceCollection serviceCollection = default;
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
{ "APPLICATIONINSIGHTS_AUTHENTICATION_STRING", $"Authorization=AAD" },
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=key" },
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "telemetryMode"), TelemetryMode.OpenTelemetry.ToString() }
});
})
.ConfigureDefaultTestWebScriptHost()
.ConfigureLogging((context, loggingBuilder) => loggingBuilder.ConfigureTelemetry(context))
.ConfigureServices(services => serviceCollection = services);
using var host = hostBuilder.Build();
// Act
var tracerProviderDescriptors = GetTracerProviderDescriptors(serviceCollection);
var resolvedClient = ExtractClientFromDescriptors(tracerProviderDescriptors);
// Extract the clientId from the client object
var clientIdValue = resolvedClient?.GetType().GetProperty("ClientId", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(resolvedClient)?.ToString();
// Assert
serviceCollection.Should().NotBeNullOrEmpty();
// No clientId should be present as it was not provided
clientIdValue.Should().BeNull();
resolvedClient.GetType().Name.Should().Be("ManagedIdentityClient");
}
// The OpenTelemetryEventListener is fine because it's a no-op if there are no otel events to listen to
private bool HasOtelServices(IServiceCollection sc) => sc.Any(sd => sd.ServiceType != typeof(OpenTelemetryEventListener) && sd.ServiceType.FullName.Contains("OpenTelemetry"));
private static IDisposable SetupDefaultEnvironmentVariables()
{
return new TestScopedEnvironmentVariable(new Dictionary<string, string>
{
{ "WEBSITE_SITE_NAME", "appName" },
{ "WEBSITE_RESOURCE_GROUP", "rg" },
{ "WEBSITE_OWNER_NAME", "AAAAA-AAAAA-AAAAA-AAA+appName-EastUSwebspace" },
{ "REGION_NAME", "EastUS" }
});
}
private static List<ServiceDescriptor> GetTracerProviderDescriptors(IServiceCollection services)
{
return services
.Where(descriptor =>
descriptor.Lifetime == ServiceLifetime.Singleton &&
descriptor.ServiceType.Name == "IConfigureTracerProviderBuilder" &&
descriptor.ImplementationInstance?.GetType().Name == "ConfigureTracerProviderBuilderCallbackWrapper")
.ToList();
}
private static object ExtractClientFromDescriptors(List<ServiceDescriptor> descriptors)
{
foreach (var descriptor in descriptors)
{
var implementation = descriptor.ImplementationInstance;
if (implementation is null)
{
continue;
}
// Reflection starts here
var configureField = implementation.GetType().GetField("configure", BindingFlags.Instance | BindingFlags.NonPublic);
if (configureField?.GetValue(implementation) is Action<IServiceProvider, TracerProviderBuilder> configureDelegate)
{
var targetType = configureDelegate.Target.GetType();
var configureDelegateTarget = targetType.GetField("configure", BindingFlags.Instance | BindingFlags.Public);
if (configureDelegateTarget?.GetValue(configureDelegate.Target) is Action<AzureMonitorExporterOptions> exporterOptionsDelegate)
{
var credentialField = exporterOptionsDelegate.Target.GetType().GetField("credential", BindingFlags.Instance | BindingFlags.Public);
if (credentialField?.GetValue(exporterOptionsDelegate.Target) is ManagedIdentityCredential managedIdentityCredential)
{
var clientProperty = managedIdentityCredential.GetType().GetProperty("Client", BindingFlags.Instance | BindingFlags.NonPublic);
return clientProperty?.GetValue(managedIdentityCredential);
}
}
}
}
return null;
}
}
}