From f20a583de230cdea980da2725649b14d3b23ab5d Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 13 Sep 2023 21:09:07 -0700 Subject: [PATCH 1/6] remove metric telemetry for resource --- .../src/Internals/ResourceExtensions.cs | 15 +- .../ResourceExtensionsTests.cs | 168 ++++++++++++------ 2 files changed, 125 insertions(+), 58 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs index a1a665530bee0..bb41da189e66f 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs @@ -119,7 +119,20 @@ internal static class ResourceExtensions } } - if (metricsData != null) + bool shouldReportMetricTelemetry = false; + try + { + var exportResource = Environment.GetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES"); + if (exportResource.Equals("true", StringComparison.OrdinalIgnoreCase)) + { + shouldReportMetricTelemetry = true; + } + } + catch + { + } + + if (shouldReportMetricTelemetry && metricsData != null) { azureMonitorResource.MetricTelemetry = new TelemetryItem(DateTime.UtcNow, azureMonitorResource, instrumentationKey!) { diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs index 30624d827afeb..4eba9df0aa155 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Collections.Generic; using System.Net; using Azure.Monitor.OpenTelemetry.Exporter.Internals; @@ -30,12 +31,20 @@ public void NullResource(string? instrumentationKey) [InlineData(InstrumentationKey)] public void DefaultResource(string? instrumentationKey) { - var resource = CreateTestResource(); - var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); + try + { + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); + var resource = CreateTestResource(); + var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); - Assert.StartsWith("unknown_service", azMonResource?.RoleName); - Assert.Equal(Dns.GetHostName(), azMonResource?.RoleInstance); - Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); + Assert.StartsWith("unknown_service", azMonResource?.RoleName); + Assert.Equal(Dns.GetHostName(), azMonResource?.RoleInstance); + Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); + } + finally + { + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + } } [Fact] @@ -185,28 +194,37 @@ public void EmptyPrefixResourceDoesNotSetSdkPrefix() [Fact] public void SdkPrefixIsNotInResourceMetrics() { - // SDK version is static, preserve to clean up later. - var sdkVersion = SdkVersionUtils.s_sdkVersion; - var testAttributes = new Dictionary + try { - {"foo", "bar" }, - { "ai.sdk.prefix", "pre_" } - }; + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); - var resource = ResourceBuilder.CreateDefault().AddAttributes(testAttributes).Build(); - var azMonResource = resource.CreateAzureMonitorResource(InstrumentationKey); + // SDK version is static, preserve to clean up later. + var sdkVersion = SdkVersionUtils.s_sdkVersion; + var testAttributes = new Dictionary + { + {"foo", "bar" }, + { "ai.sdk.prefix", "pre_" } + }; - Assert.Equal("Metric", azMonResource!.MetricTelemetry!.Name); + var resource = ResourceBuilder.CreateDefault().AddAttributes(testAttributes).Build(); + var azMonResource = resource.CreateAzureMonitorResource(InstrumentationKey); - var monitorBase = azMonResource.MetricTelemetry.Data; - var metricsData = monitorBase.BaseData as MetricsData; + Assert.Equal("Metric", azMonResource!.MetricTelemetry!.Name); - var metricDataPoint = metricsData?.Metrics[0]; - Assert.Equal("bar", metricsData?.Properties["foo"]); - Assert.False(metricsData?.Properties.ContainsKey("ai.sdk.prefix")); + var monitorBase = azMonResource.MetricTelemetry.Data; + var metricsData = monitorBase.BaseData as MetricsData; - // Clean up - SdkVersionUtils.s_sdkVersion = sdkVersion; + var metricDataPoint = metricsData?.Metrics[0]; + Assert.Equal("bar", metricsData?.Properties["foo"]); + Assert.False(metricsData?.Properties.ContainsKey("ai.sdk.prefix")); + + // Clean up + SdkVersionUtils.s_sdkVersion = sdkVersion; + } + finally + { + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + } } [Theory] @@ -214,44 +232,53 @@ public void SdkPrefixIsNotInResourceMetrics() [InlineData(InstrumentationKey)] public void MetricTelemetryHasAllResourceAttributes(string? instrumentationKey) { - var testAttributes = new Dictionary + try { - {SemanticConventions.AttributeServiceName, "my-service" }, - {SemanticConventions.AttributeServiceNamespace, "my-namespace" }, - {SemanticConventions.AttributeServiceInstance, "my-instance" }, - {SemanticConventions.AttributeK8sDeployment, "my-deployment" }, - {SemanticConventions.AttributeK8sPod, "my-pod" }, - { "foo", "bar" } - }; - - var resource = ResourceBuilder.CreateEmpty().AddAttributes(testAttributes).Build(); - var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); - - Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); - - if (instrumentationKey != null) + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); + + var testAttributes = new Dictionary + { + {SemanticConventions.AttributeServiceName, "my-service" }, + {SemanticConventions.AttributeServiceNamespace, "my-namespace" }, + {SemanticConventions.AttributeServiceInstance, "my-instance" }, + {SemanticConventions.AttributeK8sDeployment, "my-deployment" }, + {SemanticConventions.AttributeK8sPod, "my-pod" }, + { "foo", "bar" } + }; + + var resource = ResourceBuilder.CreateEmpty().AddAttributes(testAttributes).Build(); + var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); + + Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); + + if (instrumentationKey != null) + { + Assert.Equal("Metric", azMonResource!.MetricTelemetry!.Name); + Assert.Equal(3, azMonResource.MetricTelemetry.Tags.Count); + Assert.NotNull(azMonResource.MetricTelemetry.Data); + + var monitorBase = azMonResource.MetricTelemetry.Data; + var metricsData = monitorBase.BaseData as MetricsData; + + Assert.NotNull(metricsData?.Metrics); + + var metricDataPoint = metricsData?.Metrics[0]; + Assert.Equal("_OTELRESOURCE_", metricDataPoint?.Name); + Assert.Equal(0, metricDataPoint?.Value); + + Assert.Equal(6, metricsData?.Properties.Count); + + Assert.Equal("my-service", metricsData?.Properties[SemanticConventions.AttributeServiceName]); + Assert.Equal("my-namespace", metricsData?.Properties[SemanticConventions.AttributeServiceNamespace]); + Assert.Equal("my-instance", metricsData?.Properties[SemanticConventions.AttributeServiceInstance]); + Assert.Equal("my-deployment", metricsData?.Properties[SemanticConventions.AttributeK8sDeployment]); + Assert.Equal("my-pod", metricsData?.Properties[SemanticConventions.AttributeK8sPod]); + Assert.Equal("bar", metricsData?.Properties["foo"]); + } + } + finally { - Assert.Equal("Metric", azMonResource!.MetricTelemetry!.Name); - Assert.Equal(3, azMonResource.MetricTelemetry.Tags.Count); - Assert.NotNull(azMonResource.MetricTelemetry.Data); - - var monitorBase = azMonResource.MetricTelemetry.Data; - var metricsData = monitorBase.BaseData as MetricsData; - - Assert.NotNull(metricsData?.Metrics); - - var metricDataPoint = metricsData?.Metrics[0]; - Assert.Equal("_OTELRESOURCE_", metricDataPoint?.Name); - Assert.Equal(0, metricDataPoint?.Value); - - Assert.Equal(6, metricsData?.Properties.Count); - - Assert.Equal("my-service", metricsData?.Properties[SemanticConventions.AttributeServiceName]); - Assert.Equal("my-namespace", metricsData?.Properties[SemanticConventions.AttributeServiceNamespace]); - Assert.Equal("my-instance", metricsData?.Properties[SemanticConventions.AttributeServiceInstance]); - Assert.Equal("my-deployment", metricsData?.Properties[SemanticConventions.AttributeK8sDeployment]); - Assert.Equal("my-pod", metricsData?.Properties[SemanticConventions.AttributeK8sPod]); - Assert.Equal("bar", metricsData?.Properties["foo"]); + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); } } @@ -312,6 +339,33 @@ public void ResourceWithEmptyKubernetesAttributes() Assert.Equal(Dns.GetHostName(), azMonResource?.RoleInstance); } + [Theory] + [InlineData("true")] + [InlineData("false")] + public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) + { + try + { + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", envVarValue); + + var resource = ResourceBuilder.CreateDefault().Build(); + var azMonResource = resource.CreateAzureMonitorResource(InstrumentationKey); + + if (envVarValue == "true") + { + Assert.NotNull(azMonResource?.MetricTelemetry); + } + else + { + Assert.Null(azMonResource?.MetricTelemetry); + } + } + finally + { + Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + } + } + /// /// If SERVICE.NAME is not defined, it will fall-back to "unknown_service". /// (https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value). From 40248a427a8cd2888d4a69def14cce8f4101effd Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 13 Sep 2023 22:00:21 -0700 Subject: [PATCH 2/6] distro test --- .../E2EAzureMonitorDistroTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2EAzureMonitorDistroTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2EAzureMonitorDistroTests.cs index 7a1a5951199cf..e86fc7d441128 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2EAzureMonitorDistroTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2EAzureMonitorDistroTests.cs @@ -54,14 +54,14 @@ public async Task ValidateTelemetryExport() // Telemetry is serialized as json, and then byte encoded. // Need to parse the request content into something assertable. var data = ParseJsonRequestContent(transport.Requests); - Assert.Equal(16, data.Count); // Total telemetry items + Assert.Equal(15, data.Count); // Total telemetry items // Group all parsed telemetry by name and get the count per name. var summary = data.GroupBy(x => x.name).ToDictionary(x => x.Key!, x => x.Count()); Assert.Equal(4, summary.Count); // Total unique telemetry items Assert.Equal(8, summary["Message"]); // Count of telemetry items - Assert.Equal(6, summary["Metric"]); + Assert.Equal(5, summary["Metric"]); Assert.Equal(1, summary["RemoteDependency"]); Assert.Equal(1, summary["Request"]); From 4aa7a533a9288e07b56d834aa0ab3d7d44ee478e Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 14 Sep 2023 11:12:15 -0700 Subject: [PATCH 3/6] rename env var --- .../src/Internals/ResourceExtensions.cs | 2 +- .../ResourceExtensionsTests.cs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs index bb41da189e66f..a76b2d6eedcdb 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs @@ -122,7 +122,7 @@ internal static class ResourceExtensions bool shouldReportMetricTelemetry = false; try { - var exportResource = Environment.GetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES"); + var exportResource = Environment.GetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS"); if (exportResource.Equals("true", StringComparison.OrdinalIgnoreCase)) { shouldReportMetricTelemetry = true; diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs index 4eba9df0aa155..cc03eb53b447a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs @@ -33,7 +33,7 @@ public void DefaultResource(string? instrumentationKey) { try { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); var resource = CreateTestResource(); var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); @@ -43,7 +43,7 @@ public void DefaultResource(string? instrumentationKey) } finally { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); } } @@ -196,7 +196,7 @@ public void SdkPrefixIsNotInResourceMetrics() { try { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); // SDK version is static, preserve to clean up later. var sdkVersion = SdkVersionUtils.s_sdkVersion; @@ -223,7 +223,7 @@ public void SdkPrefixIsNotInResourceMetrics() } finally { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); } } @@ -234,7 +234,7 @@ public void MetricTelemetryHasAllResourceAttributes(string? instrumentationKey) { try { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", "true"); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); var testAttributes = new Dictionary { @@ -278,7 +278,7 @@ public void MetricTelemetryHasAllResourceAttributes(string? instrumentationKey) } finally { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); } } @@ -346,7 +346,7 @@ public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) { try { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", envVarValue); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", envVarValue); var resource = ResourceBuilder.CreateDefault().Build(); var azMonResource = resource.CreateAzureMonitorResource(InstrumentationKey); @@ -362,7 +362,7 @@ public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) } finally { - Environment.SetEnvironmentVariable("EXPORT_METRIC_FOR_RESOURCE_ATTRIBUTES", null); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); } } From 5da27795ab633fbfba00ca819c4af2941e5c32c1 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 14 Sep 2023 11:16:41 -0700 Subject: [PATCH 4/6] test --- .../ResourceExtensionsTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs index cc03eb53b447a..cea1167e894d5 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs @@ -340,6 +340,7 @@ public void ResourceWithEmptyKubernetesAttributes() } [Theory] + [InlineData(null)] [InlineData("true")] [InlineData("false")] public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) From 4f496a3c31b41f12bcc35f482b37d3385650bfaf Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 14 Sep 2023 11:20:59 -0700 Subject: [PATCH 5/6] update test --- .../ResourceExtensionsTests.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs index cea1167e894d5..7ca4ba06223a8 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs @@ -27,19 +27,30 @@ public void NullResource(string? instrumentationKey) } [Theory] - [InlineData(null)] - [InlineData(InstrumentationKey)] - public void DefaultResource(string? instrumentationKey) + [InlineData(null, "true")] + [InlineData(null, "false")] + [InlineData(null, null)] + [InlineData(InstrumentationKey, "false")] + [InlineData(InstrumentationKey, "true")] + [InlineData(InstrumentationKey, null)] + public void DefaultResource(string? instrumentationKey, string envVarValue) { try { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); + Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", envVarValue); var resource = CreateTestResource(); var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); Assert.StartsWith("unknown_service", azMonResource?.RoleName); Assert.Equal(Dns.GetHostName(), azMonResource?.RoleInstance); - Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); + if (envVarValue == "true") + { + Assert.Equal(instrumentationKey != null, azMonResource?.MetricTelemetry != null); + } + else + { + Assert.Null(azMonResource?.MetricTelemetry); + } } finally { From 9cc243f9dfb30b80e2b2138ad31258c68d8cdd29 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 14 Sep 2023 11:25:59 -0700 Subject: [PATCH 6/6] const --- .../Internals/EnvironmentVariableConstants.cs | 5 +++++ .../src/Internals/ResourceExtensions.cs | 2 +- .../ResourceExtensionsTests.cs | 16 ++++++++-------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs index 3b097b0080eb5..000f17a3bc08c 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs @@ -55,5 +55,10 @@ internal static class EnvironmentVariableConstants /// INTERNAL ONLY. Used by Statsbeat to get the App Service Website Name. /// public const string WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"; + + /// + /// When set to true, exporter will emit resources as metric telemetry. + /// + public const string EXPORT_RESOURCE_METRIC = "OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS"; } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs index a76b2d6eedcdb..b4c24d207a7ce 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs @@ -122,7 +122,7 @@ internal static class ResourceExtensions bool shouldReportMetricTelemetry = false; try { - var exportResource = Environment.GetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS"); + var exportResource = Environment.GetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC); if (exportResource.Equals("true", StringComparison.OrdinalIgnoreCase)) { shouldReportMetricTelemetry = true; diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs index 7ca4ba06223a8..5591ebc9e1b34 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/ResourceExtensionsTests.cs @@ -37,7 +37,7 @@ public void DefaultResource(string? instrumentationKey, string envVarValue) { try { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", envVarValue); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, envVarValue); var resource = CreateTestResource(); var azMonResource = resource.CreateAzureMonitorResource(instrumentationKey); @@ -54,7 +54,7 @@ public void DefaultResource(string? instrumentationKey, string envVarValue) } finally { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, null); } } @@ -207,7 +207,7 @@ public void SdkPrefixIsNotInResourceMetrics() { try { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, "true"); // SDK version is static, preserve to clean up later. var sdkVersion = SdkVersionUtils.s_sdkVersion; @@ -234,7 +234,7 @@ public void SdkPrefixIsNotInResourceMetrics() } finally { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, null); } } @@ -245,7 +245,7 @@ public void MetricTelemetryHasAllResourceAttributes(string? instrumentationKey) { try { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", "true"); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, "true"); var testAttributes = new Dictionary { @@ -289,7 +289,7 @@ public void MetricTelemetryHasAllResourceAttributes(string? instrumentationKey) } finally { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, null); } } @@ -358,7 +358,7 @@ public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) { try { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", envVarValue); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, envVarValue); var resource = ResourceBuilder.CreateDefault().Build(); var azMonResource = resource.CreateAzureMonitorResource(InstrumentationKey); @@ -374,7 +374,7 @@ public void MetricTelemetryIsAddedToResourceBasedOnEnvVar(string envVarValue) } finally { - Environment.SetEnvironmentVariable("OTEL_DOTNET_AZURE_MONITOR_ENABLE_RESOURCE_METRICS", null); + Environment.SetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC, null); } }