-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathSmokeTests.cs
667 lines (558 loc) · 24.4 KB
/
SmokeTests.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Reflection;
using IntegrationTests.Helpers;
using Xunit.Abstractions;
#if NETFRAMEWORK
using System.Net;
using System.Net.Http;
using IntegrationTests.Helpers.Compatibility;
#endif
namespace IntegrationTests;
public class SmokeTests : TestHelper
{
private const string ServiceName = "TestApplication.Smoke";
public SmokeTests(ITestOutputHelper output)
: base("Smoke", output)
{
SetEnvironmentVariable("OTEL_SERVICE_NAME", ServiceName);
}
[Fact]
[Trait("Category", "EndToEnd")]
public void SubmitsTraces()
{
VerifyTestApplicationInstrumented();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void WhenStartupHookIsNotEnabled()
{
SetEnvironmentVariable("DOTNET_STARTUP_HOOKS", null);
#if NETFRAMEWORK
VerifyTestApplicationInstrumented();
#else
// on .NET it is required to set DOTNET_STARTUP_HOOKS
VerifyTestApplicationNotInstrumented();
#endif
}
[Fact]
[Trait("Category", "EndToEnd")]
public void WhenClrProfilerIsNotEnabled()
{
SetEnvironmentVariable("COR_ENABLE_PROFILING", "0");
SetEnvironmentVariable("CORECLR_ENABLE_PROFILING", "0");
#if NETFRAMEWORK
// on .NET Framework it is required to set the CLR .NET Profiler
VerifyTestApplicationNotInstrumented();
#else
VerifyTestApplicationInstrumented();
#endif
}
#if !NETFRAMEWORK
[Theory]
[Trait("Category", "EndToEnd")]
[InlineData(TestAppStartupMode.DotnetCLI)]
[InlineData(TestAppStartupMode.Exe)]
public void WhenClrProfilerIsNotEnabledStartupHookIsEnabledApplicationIsExcluded(TestAppStartupMode testAppStartupMode)
{
switch (testAppStartupMode)
{
case TestAppStartupMode.DotnetCLI:
SetEnvironmentVariable("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", "dotnet,dotnet.exe");
break;
case TestAppStartupMode.Exe:
SetEnvironmentVariable("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", $"{EnvironmentHelper.FullTestApplicationName},{EnvironmentHelper.FullTestApplicationName}.exe");
break;
default:
throw new ArgumentException($"{testAppStartupMode} is not supported by this test", nameof(testAppStartupMode));
}
SetEnvironmentVariable("CORECLR_ENABLE_PROFILING", "0");
VerifyTestApplicationNotInstrumented(testAppStartupMode);
}
#endif
[Fact]
[Trait("Category", "EndToEnd")]
public void ApplicationIsNotExcluded()
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", string.Empty);
VerifyTestApplicationInstrumented(TestAppStartupMode.Exe);
}
[Fact]
[Trait("Category", "EndToEnd")]
public void ApplicationIsExcluded()
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", $"{EnvironmentHelper.FullTestApplicationName},{EnvironmentHelper.FullTestApplicationName}.exe");
VerifyTestApplicationNotInstrumented(TestAppStartupMode.Exe);
}
[Fact]
[Trait("Category", "EndToEnd")]
public void SubmitMetrics()
{
using var collector = new MockMetricsCollector(Output);
SetExporter(collector);
collector.Expect("MyCompany.MyProduct.MyLibrary", metric => metric.Name == "MyFruitCounter");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
RunTestApplication();
collector.AssertExpectations();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void TracesResource()
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_TRACES_EXPORTER", "otlp,console");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
var (_, _, processId) = RunTestApplication();
ExpectResources(collector.ResourceExpector, processId);
collector.ResourceExpector.AssertExpectations();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void MetricsResource()
{
using var collector = new MockMetricsCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
var (_, _, processId) = RunTestApplication();
ExpectResources(collector.ResourceExpector, processId);
collector.ResourceExpector.AssertExpectations();
}
#if NET // The feature is not supported on .NET Framework
[Fact]
[Trait("Category", "EndToEnd")]
public void LogsResource()
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
EnableBytecodeInstrumentation();
var (_, _, processId) = RunTestApplication();
ExpectResources(collector.ResourceExpector, processId);
collector.ResourceExpector.AssertExpectations();
}
#endif
[Fact]
[Trait("Category", "EndToEnd")]
public void OtlpTracesExporter()
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
collector.Expect("MyCompany.MyProduct.MyLibrary", span => span.Name == "SayHello");
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
RunTestApplication();
collector.AssertExpectations();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void ZipkinExporter()
{
using var collector = new MockZipkinCollector(Output);
collector.Expect(span => span.Name == "SayHello" && span.Tags?.GetValueOrDefault("otel.library.name") == "MyCompany.MyProduct.MyLibrary");
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_TRACES_EXPORTER", "zipkin");
SetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT", $"http://localhost:{collector.Port}/api/v2/spans");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
SetEnvironmentVariable("LONG_RUNNING", "true");
using var process = StartTestApplication();
using var helper = new ProcessHelper(process);
try
{
collector.AssertExpectations();
}
finally
{
if (helper?.Process != null && !helper.Process.HasExited)
{
helper.Process.Kill();
helper.Process.WaitForExit();
Output.WriteLine("ProcessId: " + helper.Process.Id);
Output.WriteLine("Exit Code: " + helper.Process.ExitCode);
Output.WriteResult(helper);
}
}
}
[Fact]
[Trait("Category", "EndToEnd")]
public void ZipkinAndOtlpTracesExporter()
{
using var otlpCollector = new MockSpansCollector(Output);
SetExporter(otlpCollector);
otlpCollector.Expect("MyCompany.MyProduct.MyLibrary", span => span.Name == "SayHello");
using var zipkinCollector = new MockZipkinCollector(Output);
zipkinCollector.Expect(span => span.Name == "SayHello" && span.Tags?.GetValueOrDefault("otel.library.name") == "MyCompany.MyProduct.MyLibrary");
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
SetEnvironmentVariable("OTEL_TRACES_EXPORTER", "otlp,zipkin");
SetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT", $"http://localhost:{zipkinCollector.Port}/api/v2/spans");
SetEnvironmentVariable("LONG_RUNNING", "true");
using var process = StartTestApplication();
using var helper = new ProcessHelper(process);
try
{
otlpCollector.AssertExpectations();
zipkinCollector.AssertExpectations();
}
finally
{
if (helper?.Process != null && !helper.Process.HasExited)
{
helper.Process.Kill();
helper.Process.WaitForExit();
Output.WriteLine("ProcessId: " + helper.Process.Id);
Output.WriteLine("Exit Code: " + helper.Process.ExitCode);
Output.WriteResult(helper);
}
}
}
#if NETFRAMEWORK // The test is flaky on Linux and macOS, because of https://github.com/dotnet/runtime/issues/28658#issuecomment-462062760
[Fact]
[Trait("Category", "EndToEnd")]
public async Task PrometheusExporter()
{
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("LONG_RUNNING", "true");
SetEnvironmentVariable("OTEL_METRICS_EXPORTER", "prometheus");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
const string defaultPrometheusMetricsEndpoint = "http://localhost:9464/metrics";
using var process = StartTestApplication();
using var helper = new ProcessHelper(process);
try
{
var assert = async () =>
{
var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
var response = await httpClient.GetAsync(defaultPrometheusMetricsEndpoint);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine("Raw metrics from Prometheus:");
Output.WriteLine(content);
Assert.Contains("TYPE ", content); // should export any metric
};
await AssertRepeatingExecutionDoesNotThrow(assert, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1));
var exception = await Record.ExceptionAsync(() => assert());
Assert.Null(exception);
}
finally
{
if (helper?.Process != null && !helper.Process.HasExited)
{
helper.Process.Kill();
helper.Process.WaitForExit();
Output.WriteLine("ProcessId: " + helper.Process.Id);
Output.WriteLine("Exit Code: " + helper.Process.ExitCode);
Output.WriteResult(helper);
}
}
}
#endif
#if NETFRAMEWORK
[Fact]
[Trait("Category", "EndToEnd")]
public async Task PrometheusAndOtlpMetricsExporter()
{
using var otlpCollector = new MockMetricsCollector(Output);
SetExporter(otlpCollector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("LONG_RUNNING", "true");
SetEnvironmentVariable("OTEL_METRICS_EXPORTER", "otlp,prometheus");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
const string defaultPrometheusMetricsEndpoint = "http://localhost:9464/metrics";
using var process = StartTestApplication();
using var helper = new ProcessHelper(process);
try
{
var assert = async () =>
{
var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
var response = await httpClient.GetAsync(defaultPrometheusMetricsEndpoint);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine("Raw metrics from Prometheus:");
Output.WriteLine(content);
Assert.Contains("TYPE ", content); // should export any metric
await Task.Delay(TimeSpan.FromSeconds(1));
};
await AssertRepeatingExecutionDoesNotThrow(assert, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1));
}
finally
{
if (helper?.Process != null && !helper.Process.HasExited)
{
helper.Process.Kill();
helper.Process.WaitForExit();
Output.WriteLine("ProcessId: " + helper.Process.Id);
Output.WriteLine("Exit Code: " + helper.Process.ExitCode);
Output.WriteResult(helper);
}
}
}
#endif
#if NET // The feature is not supported on .NET Framework
[Fact]
[Trait("Category", "EndToEnd")]
public void SubmitLogs()
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
collector.Expect(logRecord => Convert.ToString(logRecord.Body) == "{ \"stringValue\": \"Example log message\" }");
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", "true");
EnableBytecodeInstrumentation();
RunTestApplication();
collector.AssertExpectations();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void LogTraceCorrelation()
{
using var collector = new MockCorrelationCollector(Output);
SetEnvironmentVariable("OTEL_TRACES_EXPORTER", "otlp");
SetEnvironmentVariable("OTEL_LOGS_EXPORTER", "otlp");
SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", $"http://localhost:{collector.Port}");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", "true");
EnableBytecodeInstrumentation();
RunTestApplication();
collector.ExpectSpan(collectedSpan => collectedSpan.InstrumentationScopeName == "MyCompany.MyProduct.MyLibrary");
collector.ExpectLogRecord(record => Convert.ToString(record.Body) == "{ \"stringValue\": \"Example log message\" }");
collector.AssertCorrelation();
}
[Theory]
[InlineData("OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_DOTNET_AUTO_LOGS_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_DOTNET_AUTO_LOGS_ENABLED", "false")]
[InlineData("OTEL_LOGS_EXPORTER", "none")]
[Trait("Category", "EndToEnd")]
public void LogsNoneInstrumentations(string envVarName, string envVarVal)
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable(envVarName, envVarVal);
EnableBytecodeInstrumentation();
RunTestApplication();
collector.AssertEmpty();
}
#endif
[Theory]
[InlineData("OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_TRACES_EXPORTER", "none")]
[Trait("Category", "EndToEnd")]
public void TracesNoneInstrumentations(string envVarName, string envVarVal)
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
SetEnvironmentVariable(envVarName, envVarVal);
RunTestApplication();
collector.AssertEmpty();
}
[Theory]
[InlineData("OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_DOTNET_AUTO_METRICS_INSTRUMENTATION_ENABLED", "false")]
[InlineData("OTEL_DOTNET_AUTO_METRICS_ENABLED", "false")]
[InlineData("OTEL_METRICS_EXPORTER", "none")]
[Trait("Category", "EndToEnd")]
public void MetricsNoneInstrumentations(string envVarName, string envVarVal)
{
using var collector = new MockMetricsCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable(envVarName, envVarVal);
RunTestApplication();
collector.AssertEmpty();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void LogsDisabledInstrumentation()
{
using var collector = new MockLogsCollector(Output);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_DISABLED_INSTRUMENTATIONS", "ILogger");
EnableOnlyHttpClientTraceInstrumentation();
EnableBytecodeInstrumentation();
RunTestApplication();
collector.AssertEmpty();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void MetricsDisabledInstrumentation()
{
using var collector = new MockMetricsCollector(Output);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_HTTPCLIENT_INSTRUMENTATION_ENABLED", "false");
EnableOnlyHttpClientTraceInstrumentation();
EnableBytecodeInstrumentation();
RunTestApplication();
collector.AssertEmpty();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void TracesDisabledInstrumentation()
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_HTTPCLIENT_INSTRUMENTATION_ENABLED", "false");
RunTestApplication();
collector.AssertEmpty();
}
[Fact]
[Trait("Category", "EndToEnd")]
public void ApplicationFailFastDisabled()
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", $"dotnet,dotnet.exe,{EnvironmentHelper.FullTestApplicationName},{EnvironmentHelper.FullTestApplicationName}.exe");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_FAIL_FAST_ENABLED", "false");
VerifyTestApplicationNotInstrumented();
}
[Theory]
[Trait("Category", "EndToEnd")]
[InlineData("OTEL_TRACES_EXPORTER", "non-supported")]
[InlineData("OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES", $"dotnet,dotnet.exe,TestApplication.Smoke,TestApplication.Smoke.exe")]
public void ApplicationFailFastEnabled(string additionalVariableKey, string additionalVariableValue)
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_FAIL_FAST_ENABLED", "true");
SetEnvironmentVariable(additionalVariableKey, additionalVariableValue);
using var process = StartTestApplication();
using var helper = new ProcessHelper(process);
Assert.NotNull(process);
var processTimeout = !process!.WaitForExit((int)TestTimeout.ProcessExit.TotalMilliseconds);
if (processTimeout)
{
process.Kill();
}
Output.WriteResult(helper);
Assert.False(processTimeout, "Test application timed out");
Assert.NotEqual(0, process.ExitCode);
}
[Fact]
public void NativeLogsHaveNoSensitiveData()
{
var tempLogsDirectory = DirectoryHelpers.CreateTempDirectory();
var secretIdentificators = new[] { "API", "TOKEN", "SECRET", "KEY", "PASSWORD", "PASS", "PWD", "HEADER", "CREDENTIALS" };
EnableBytecodeInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOG_DIRECTORY", tempLogsDirectory.FullName);
SetEnvironmentVariable("OTEL_LOG_LEVEL", "debug");
foreach (var item in secretIdentificators)
{
SetEnvironmentVariable($"OTEL_{item}_VALUE", "this is secret!");
if (!EnvironmentTools.IsWindows())
{
SetEnvironmentVariable($"otel_{item.ToLowerInvariant()}_value2", "this is secret!");
}
}
try
{
RunTestApplication();
var nativeLog = tempLogsDirectory.GetFiles("otel-dotnet-auto-*-Native.log").Single();
var nativeLogContent = File.ReadAllText(nativeLog.FullName);
Assert.False(string.IsNullOrWhiteSpace(nativeLogContent), "native log should not be empty");
var environmentVariables = ParseEnvironmentVariablesLog(nativeLogContent);
Assert.NotEmpty(environmentVariables);
var secretVariables = environmentVariables
.Where(item => secretIdentificators.Any(i => item.Key.Contains(i)))
.ToList();
Assert.NotEmpty(secretVariables);
Assert.All(secretVariables, secret => Assert.Equal("<hidden>", secret.Value));
}
finally
{
tempLogsDirectory.Delete(true);
}
}
private static ICollection<KeyValuePair<string, string>> ParseEnvironmentVariablesLog(string log)
{
var lines = log.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var variables = lines
.SkipWhile(x => !x.Contains("Environment variables:"))
#if NETFRAMEWORK
.TakeWhile(x => !x.Contains(".NET Runtime: .NET Framework"))
#else
.TakeWhile(x => !x.Contains("Interface ICorProfilerInfo12 found."))
#endif
.Skip(1)
.Select(ParseEnvironmentVariableLogEntry)
.ToEnvironmentVariablesList();
return variables;
}
private static string ParseEnvironmentVariableLogEntry(string entry)
{
const string startMarker = "[debug]";
var startIndex = entry.IndexOf("[debug]") + startMarker.Length;
return entry.AsSpan().Slice(startIndex).Trim().ToString();
}
private static void ExpectResources(OtlpResourceExpector resourceExpector, int processId)
{
resourceExpector.Expect("service.name", ServiceName); // this is set via env var and App.config, but env var has precedence
#if NETFRAMEWORK
resourceExpector.Expect("deployment.environment", "test"); // this is set via App.config
#endif
resourceExpector.Expect("telemetry.sdk.name", "opentelemetry");
resourceExpector.Expect("telemetry.sdk.language", "dotnet");
resourceExpector.Expect("telemetry.sdk.version", typeof(OpenTelemetry.Resources.Resource).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion.Split('+')[0]);
resourceExpector.Expect("telemetry.distro.name", "opentelemetry-dotnet-instrumentation");
resourceExpector.Expect("telemetry.distro.version", typeof(OpenTelemetry.AutoInstrumentation.Constants).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion.Split('+')[0]);
resourceExpector.Expect("process.pid", processId);
resourceExpector.Expect("host.name", Environment.MachineName);
#if NETFRAMEWORK
resourceExpector.Expect("process.runtime.name", ".NET Framework");
#else
resourceExpector.Expect("process.runtime.name", ".NET");
#endif
var expectedPlatform = EnvironmentTools.GetOS() switch
{
"win" => "windows",
"osx" => "darwin",
"linux" => "linux",
_ => throw new PlatformNotSupportedException($"Unknown platform")
};
resourceExpector.Expect("os.type", expectedPlatform);
resourceExpector.Exist("os.build_id");
resourceExpector.Exist("os.description");
resourceExpector.Exist("os.name");
resourceExpector.Exist("os.version");
}
#if NETFRAMEWORK
private static async Task AssertRepeatingExecutionDoesNotThrow(Func<Task> assert, TimeSpan waitInterval, TimeSpan pollInterval)
{
var stopwatch = Stopwatch.StartNew();
while (stopwatch.Elapsed < pollInterval)
{
var exception = await Record.ExceptionAsync(() => assert());
Assert.Null(exception);
await Task.Delay(waitInterval);
}
}
#endif
private void VerifyTestApplicationInstrumented(TestAppStartupMode startupMode = TestAppStartupMode.Auto)
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
collector.Expect("MyCompany.MyProduct.MyLibrary");
#if NETFRAMEWORK
collector.Expect("OpenTelemetry.Instrumentation.Http.HttpWebRequest");
#else
collector.Expect("System.Net.Http");
#endif
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
RunTestApplication(new() { StartupMode = startupMode });
collector.AssertExpectations();
}
private void VerifyTestApplicationNotInstrumented(TestAppStartupMode startupMode = TestAppStartupMode.Auto)
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
EnableOnlyHttpClientTraceInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "MyCompany.MyProduct.MyLibrary");
RunTestApplication(new() { StartupMode = startupMode });
collector.AssertEmpty();
}
private void EnableOnlyHttpClientTraceInstrumentation()
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED", "false");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_HTTPCLIENT_INSTRUMENTATION_ENABLED", "true");
}
}