|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Azure.Monitor.OpenTelemetry.Exporter; |
| 11 | +using Microsoft.Extensions.Configuration; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.Extensions.Options; |
| 14 | +using OpenTelemetry.Trace; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Azure.Monitor.OpenTelemetry.AspNetCore.Tests |
| 18 | +{ |
| 19 | + public class OpenTelemetryBuilderExtensionsTests |
| 20 | + { |
| 21 | +#if !NETFRAMEWORK |
| 22 | + private const string ConnectionStringEnvironmentVariable = "APPLICATIONINSIGHTS_CONNECTION_STRING"; |
| 23 | + |
| 24 | + [Theory] |
| 25 | + [InlineData(false, false, false, false, false, false, null)] // If nothing set, ConnectionString will be null. |
| 26 | + [InlineData(true, false, false, false, false, false, null)] // If nothing set, ConnectionString will be null. |
| 27 | + [InlineData(false, true, false, false, false, false, "testJsonValue")] // only AzureMonitor in json |
| 28 | + [InlineData(true, true, false, false, false, false, null)] // only AzureMonitor in json |
| 29 | + [InlineData(false, false, true, false, false, false, "testJsonEnvVarValue")] // only EnvVar in json |
| 30 | + [InlineData(true, false, true, false, false, false, "testJsonEnvVarValue")] // only EnvVar in json |
| 31 | + [InlineData(false, true, true, false, false, false, "testJsonEnvVarValue")] // both AzureMonitor & EnvVar in json |
| 32 | + [InlineData(true, true, true, false, false, false, "testJsonEnvVarValue")] // both AzureMonitor & EnvVar in json |
| 33 | + [InlineData(false, false, false, true, false, false, "testInMemoryCollectionValue")] // only IConfig InMemoryCollection |
| 34 | + [InlineData(true, false, false, true, false, false, "testInMemoryCollectionValue")] // only IConfig InMemoryCollection |
| 35 | + [InlineData(false, false, false, false, true, false, null)] // only IConfig EnvVars, without EnvVar set. |
| 36 | + [InlineData(true, false, false, false, true, false, null)] // only IConfig EnvVars, without EnvVar set. |
| 37 | + [InlineData(false, false, false, false, true, true, "testEnvVarValue")] // only IConfig EnvVars, with EnvVar set. |
| 38 | + [InlineData(true, false, false, false, true, true, "testEnvVarValue")] // only IConfig EnvVars, with EnvVar set. |
| 39 | + [InlineData(false, false, false, true, true, true, "testEnvVarValue")] // both IConfig InMemoryCollection & IConfig EnvVars with EnvVar set |
| 40 | + [InlineData(true, false, false, true, true, true, "testEnvVarValue")] // both IConfig InMemoryCollection & IConfig EnvVars with EnvVar set |
| 41 | + [InlineData(false, false, false, true, true, false, "testInMemoryCollectionValue")] // both IConfig InMemoryCollection & IConfig EnvVars without EnvVar set |
| 42 | + [InlineData(true, false, false, true, true, false, "testInMemoryCollectionValue")] // both IConfig InMemoryCollection & IConfig EnvVars without EnvVar set |
| 43 | + [InlineData(false, false, false, false, false, true, "testEnvVarValue")] // only EnvironmentVariable |
| 44 | + [InlineData(true, false, false, false, false, true, "testEnvVarValue")] // only EnvironmentVariable |
| 45 | + [InlineData(false, true, true, true, true, true, "testEnvVarValue")] |
| 46 | + [InlineData(true, true, true, true, true, true, "testEnvVarValue")] |
| 47 | + public void Verify_UseAzureMonitor_SetsConnectionString(bool useConfigure, bool jsonAzureMonitor, bool jsonEnvVar, bool iconfigCollection, bool iconfigEnvVar, bool setEnvVar, string expectedConnectionStringValue) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + Environment.SetEnvironmentVariable(ConnectionStringEnvironmentVariable, setEnvVar ? "testEnvVarValue" : null); |
| 52 | + |
| 53 | + // BUILD JSON STRING |
| 54 | + var jsonString = "{"; |
| 55 | + |
| 56 | + if (jsonAzureMonitor) |
| 57 | + { |
| 58 | + jsonString += @"""AzureMonitor"":{ ""ConnectionString"" : ""testJsonValue"" }"; |
| 59 | + } |
| 60 | + |
| 61 | + if (jsonAzureMonitor && jsonEnvVar) |
| 62 | + { |
| 63 | + jsonString += ","; |
| 64 | + } |
| 65 | + |
| 66 | + if (jsonEnvVar) |
| 67 | + { |
| 68 | + jsonString += @"""APPLICATIONINSIGHTS_CONNECTION_STRING"" : ""testJsonEnvVarValue"""; |
| 69 | + } |
| 70 | + |
| 71 | + jsonString += "}"; |
| 72 | + |
| 73 | + // BUILD CONFIGURATION OBJECT |
| 74 | + var configBulider = new ConfigurationBuilder(); |
| 75 | + bool usesConfigBuilder = false; |
| 76 | + |
| 77 | + if (jsonAzureMonitor || jsonEnvVar) |
| 78 | + { |
| 79 | + configBulider.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(jsonString))); |
| 80 | + usesConfigBuilder = true; |
| 81 | + } |
| 82 | + |
| 83 | + if (iconfigCollection) |
| 84 | + { |
| 85 | + configBulider.AddInMemoryCollection(new Dictionary<string, string?> { [ConnectionStringEnvironmentVariable] = "testInMemoryCollectionValue" }); |
| 86 | + usesConfigBuilder = true; |
| 87 | + } |
| 88 | + |
| 89 | + if (iconfigEnvVar) |
| 90 | + { |
| 91 | + configBulider.AddEnvironmentVariables(); |
| 92 | + } |
| 93 | + |
| 94 | + // ACT |
| 95 | + var serviceCollection = new ServiceCollection(); |
| 96 | + |
| 97 | + if (usesConfigBuilder) |
| 98 | + { |
| 99 | + var configuration = configBulider.Build(); |
| 100 | + serviceCollection.AddSingleton<IConfiguration>(configuration); |
| 101 | + } |
| 102 | + |
| 103 | + if (useConfigure) |
| 104 | + { |
| 105 | + serviceCollection.AddOpenTelemetry().UseAzureMonitor(x => { }); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + serviceCollection.AddOpenTelemetry().UseAzureMonitor(); |
| 110 | + } |
| 111 | + |
| 112 | + using var serviceProvider = serviceCollection.BuildServiceProvider(); |
| 113 | + |
| 114 | + // ASSERT |
| 115 | + var azureMonitorOptions = serviceProvider.GetServices<IOptions<AzureMonitorOptions>>().Single().Value; |
| 116 | + Assert.Equal(expectedConnectionStringValue, azureMonitorOptions.ConnectionString); |
| 117 | + |
| 118 | + var azureMonitorExporterOptions = serviceProvider.GetServices<IOptions<AzureMonitorExporterOptions>>().Single().Value; |
| 119 | + Assert.Equal(expectedConnectionStringValue, azureMonitorExporterOptions.ConnectionString); |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + Environment.SetEnvironmentVariable(ConnectionStringEnvironmentVariable, null); |
| 124 | + } |
| 125 | + } |
| 126 | +#endif |
| 127 | + } |
| 128 | +} |
0 commit comments