From c20a0735a300470bb49784d42217e88d45bf8dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 14:55:30 +0200 Subject: [PATCH 01/12] Handle OTEL_SERVICE_NAME environmental variable --- src/OpenTelemetry/CHANGELOG.md | 3 + src/OpenTelemetry/README.md | 18 ++++-- .../OtelServiceNameEnvVarDetector.cs | 41 ++++++++++++ .../Resources/ResourceBuilderExtensions.cs | 6 +- .../OtelServiceNameEnvVarDetectorTests.cs | 62 ++++++++++++++++++ .../Resources/ResourceTest.cs | 63 +++++++++++++++++-- 6 files changed, 180 insertions(+), 13 deletions(-) create mode 100644 src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs create mode 100644 test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 988d8b0216e..0c88c397a17 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* `ResourceBuilder.AddEnvironmentVariableDetector` handles `OTEL_SERVICE_NAME` + environmental variable. ([#2209](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2209)) + * Removes upper constraint for Microsoft.Extensions.Logging dependencies. ([#2179](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2179)) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index 94744f5cadc..f58ef0b986d 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -5,10 +5,10 @@ * [Installation](#installation) * [Introduction](#introduction) -* [Getting started with Logs](#getting-started-with-logging) -* [Getting started with Traces](#getting-started-with-tracing) -* [Tracing Configuration](#tracing-configuration) - * [ActivitySource](#activity-source) +* [Getting started with Logging](#getting-started-with-logging) +* [Getting started with Tracing](#getting-started-with-tracing) +* [Tracing configuration](#tracing-configuration) + * [Activity Source](#activity-source) * [Instrumentation](#instrumentation) * [Processor](#processor) * [Resource](#resource) @@ -16,6 +16,8 @@ * [Advanced topics](#advanced-topics) * [Propagators](#propagators) * [Troubleshooting](#troubleshooting) + * [Configuration Parameters](#configuration-parameters) + * [Remarks](#remarks) * [References](#references) ## Installation @@ -243,6 +245,14 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` +It is also to configure the resource by using `AddEnvironmentVariableDetector` +together with following environmental variables: + +| Environment variable | Description | +| ------------------------ | ------------------------------------------------- | +| OTEL_RESOURCE_ATTRIBUTES | Key-value pairs to be used as resource attributes. | +| OTEL_SERVICE_NAME | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | + ### Sampler [Samplers](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#sampler) diff --git a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs new file mode 100644 index 00000000000..05b4eb3b5e0 --- /dev/null +++ b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs @@ -0,0 +1,41 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; + +namespace OpenTelemetry.Resources +{ + internal class OtelServiceNameEnvVarDetector : IResourceDetector + { + private const string EnvVarKey = "OTEL_SERVICE_NAME"; + public Resource Detect() + { + var resource = Resource.Empty; + + string envResourceAttributeValue = Environment.GetEnvironmentVariable(EnvVarKey); + if (!string.IsNullOrEmpty(envResourceAttributeValue)) + { + resource = new Resource(new Dictionary + { + [ResourceSemanticConventions.AttributeServiceName] = envResourceAttributeValue, + }); + } + + return resource; + } + } +} diff --git a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs index 8eb500597da..9dd30fc1236 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs @@ -110,8 +110,8 @@ public static ResourceBuilder AddAttributes(this ResourceBuilder resourceBuilder } /// - /// Adds resource attributes parsed from an environment variable to a - /// following the following the Resource /// SDK. /// @@ -119,7 +119,7 @@ public static ResourceBuilder AddAttributes(this ResourceBuilder resourceBuilder /// Returns for chaining. public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilder resourceBuilder) { - return resourceBuilder.AddDetector(new OtelEnvResourceDetector()); + return resourceBuilder.AddDetector(new OtelEnvResourceDetector()).AddDetector(new OtelServiceNameEnvVarDetector()); } private static string GetFileVersion() diff --git a/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs new file mode 100644 index 00000000000..57377bd90a0 --- /dev/null +++ b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs @@ -0,0 +1,62 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; +using Xunit; + +namespace OpenTelemetry.Resources.Tests +{ + public class OtelServiceNameEnvVarDetectorTests : IDisposable + { + private const string OtelEnvVarKey = "OTEL_SERVICE_NAME"; + + public OtelServiceNameEnvVarDetectorTests() + { + Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + } + + public void Dispose() + { + Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + } + + [Fact] + public void OtelServiceNameEnvVar_Null() + { + // Act + var resource = new OtelServiceNameEnvVarDetector().Detect(); + + // Assert + Assert.Equal(Resource.Empty, resource); + } + + [Fact] + public void OtelServiceNameEnvVar_WithValue() + { + // Arrange + var envVarValue = "my-service"; + Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); + + // Act + var resource = new OtelServiceNameEnvVarDetector().Detect(); + + // Assert + Assert.NotEqual(Resource.Empty, resource); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, envVarValue), resource.Attributes); + } + } +} diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 624d0c3f404..0b4cc3a4363 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -25,11 +25,17 @@ public class ResourceTest : IDisposable { private const string KeyName = "key"; private const string ValueName = "value"; - private const string OtelEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; + private const string ResourceAttributesEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; + private const string ServiceNameEnvVarKey = "OTEL_SERVICE_NAME"; public ResourceTest() { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + ClearEnvVars(); + } + + public void Dispose() + { + ClearEnvVars(); } [Fact] @@ -421,10 +427,10 @@ public void GetResourceWithDefaultAttributes_ResourceWithAttrs() } [Fact] - public void GetResourceWithDefaultAttributes_WithEnvVar() + public void GetResourceWithDefaultAttributes_WithResourceEnvVar() { // Arrange - Environment.SetEnvironmentVariable(OtelEnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2"); + Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2"); var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert @@ -436,9 +442,54 @@ public void GetResourceWithDefaultAttributes_WithEnvVar() Assert.Contains(new KeyValuePair("EVKey2", "EVVal2"), attributes); } - public void Dispose() + [Fact] + public void GetResource_WithServiceEnvVar() + { + // Arrange + Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "some-service"); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); + + // Assert + var attributes = resource.Attributes; + Assert.Equal(3, attributes.Count()); + ValidateAttributes(attributes, 0, 1); + Assert.Contains(new KeyValuePair("service.name", "some-service"), attributes); + } + + [Fact] + public void GetResource_WithServiceNameSetWithTwoEnvVars() + { + // Arrange + Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "service.name=from-resource-attr"); + Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "from-service-name"); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); + + // Assert + var attributes = resource.Attributes; + Assert.Equal(3, attributes.Count()); + ValidateAttributes(attributes, 0, 1); + Assert.Contains(new KeyValuePair("service.name", "from-service-name"), attributes); + } + + [Fact] + public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode() + { + // Arrange + Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "service.name=from-resource-attr"); + Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "from-service-name"); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddService("from-code").AddAttributes(this.CreateAttributes(2)).Build(); + + // Assert + var attributes = resource.Attributes; + Assert.Equal(4, attributes.Count()); + ValidateAttributes(attributes, 0, 1); + Assert.Contains(new KeyValuePair("service.name", "from-code"), attributes); + } + + private void ClearEnvVars() { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, null); + Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, null); } private static void AddAttributes(Dictionary attributes, int attributeCount, int startIndex = 0) From 756ec0b06055be74be1183add242c3c660769efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 15:01:55 +0200 Subject: [PATCH 02/12] Fix lint issues --- src/OpenTelemetry/README.md | 2 ++ src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs | 3 ++- .../Resources/OtelServiceNameEnvVarDetectorTests.cs | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index f58ef0b986d..b7a7aa67dc7 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -248,10 +248,12 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder() It is also to configure the resource by using `AddEnvironmentVariableDetector` together with following environmental variables: + | Environment variable | Description | | ------------------------ | ------------------------------------------------- | | OTEL_RESOURCE_ATTRIBUTES | Key-value pairs to be used as resource attributes. | | OTEL_SERVICE_NAME | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | + ### Sampler diff --git a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs index 05b4eb3b5e0..fe82831d554 100644 --- a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs +++ b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,7 @@ namespace OpenTelemetry.Resources internal class OtelServiceNameEnvVarDetector : IResourceDetector { private const string EnvVarKey = "OTEL_SERVICE_NAME"; + public Resource Detect() { var resource = Resource.Empty; diff --git a/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs index 57377bd90a0..45595b8fa97 100644 --- a/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs +++ b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); From b76da0741ddef1b36d88889e9d38eb9a0ae1c9b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 15:02:32 +0200 Subject: [PATCH 03/12] Refine markdown --- src/OpenTelemetry/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index b7a7aa67dc7..8715cf5e57b 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -249,10 +249,10 @@ It is also to configure the resource by using `AddEnvironmentVariableDetector` together with following environmental variables: -| Environment variable | Description | -| ------------------------ | ------------------------------------------------- | -| OTEL_RESOURCE_ATTRIBUTES | Key-value pairs to be used as resource attributes. | -| OTEL_SERVICE_NAME | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | +| Environment variable | Description | +| -------------------------- | ------------------------------------------------- | +| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. | +| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | ### Sampler From edca8cde12fa418f7e79f31af44442e3b6e657be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 15:06:33 +0200 Subject: [PATCH 04/12] Fix build --- src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs index fe82831d554..ad7f278e8a0 100644 --- a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs +++ b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs @@ -22,7 +22,7 @@ namespace OpenTelemetry.Resources internal class OtelServiceNameEnvVarDetector : IResourceDetector { private const string EnvVarKey = "OTEL_SERVICE_NAME"; - + public Resource Detect() { var resource = Resource.Empty; From bec5a63361d468785685c90de05094560bccf6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 15:14:41 +0200 Subject: [PATCH 05/12] dotnet-format --- test/OpenTelemetry.Tests/Resources/ResourceTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 0b4cc3a4363..1de29c53b0d 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -32,7 +32,7 @@ public ResourceTest() { ClearEnvVars(); } - + public void Dispose() { ClearEnvVars(); From 960408fcd9a99f9a0e8f3cc5e749ece15a63acc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 15:21:19 +0200 Subject: [PATCH 06/12] Fix warnings --- test/OpenTelemetry.Tests/Resources/ResourceTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 1de29c53b0d..39310d90535 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -39,7 +39,7 @@ public void Dispose() } [Fact] - public static void CreateResource_NullAttributeCollection() + public void CreateResource_NullAttributeCollection() { // Act and Assert var resource = new Resource(null); @@ -486,7 +486,7 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode() Assert.Contains(new KeyValuePair("service.name", "from-code"), attributes); } - private void ClearEnvVars() + private static void ClearEnvVars() { Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, null); Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, null); From a05f90e87f6249dcf8e977daecb7c5e50ca101b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 29 Jul 2021 18:11:26 +0200 Subject: [PATCH 07/12] Update src/OpenTelemetry/README.md Co-authored-by: Reiley Yang --- src/OpenTelemetry/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index 8715cf5e57b..0f408bad8fe 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -250,7 +250,7 @@ together with following environmental variables: | Environment variable | Description | -| -------------------------- | ------------------------------------------------- | +| -------------------------- | -------------------------------------------------- | | `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. | | `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | From b6989e5f83068c773aab5fa76a042c33e1d88106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 30 Jul 2021 08:32:41 +0200 Subject: [PATCH 08/12] Update src/OpenTelemetry/README.md Co-authored-by: Paulo Janotti --- src/OpenTelemetry/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index 0f408bad8fe..0d39bdfa4c3 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -245,7 +245,7 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` -It is also to configure the resource by using `AddEnvironmentVariableDetector` +It is also possible to configure the `Resource` by using `AddEnvironmentVariableDetector` together with following environmental variables: From d5198652166c11a4ae5cfb893b466094eba8dffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 30 Jul 2021 08:49:13 +0200 Subject: [PATCH 09/12] Update README.md --- src/OpenTelemetry/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index 0d39bdfa4c3..33a840986a0 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -251,7 +251,7 @@ together with following environmental variables: | Environment variable | Description | | -------------------------- | -------------------------------------------------- | -| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. | +| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. | | `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | From 3268d715d580846c60e3d72eed99e09f8dd073c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 30 Jul 2021 20:26:11 +0200 Subject: [PATCH 10/12] Make resource env vars public --- .../Resources/OtelEnvResourceDetector.cs | 4 +-- .../OtelServiceNameEnvVarDetector.cs | 2 +- .../Resources/OtelEnvResourceDetectorTest.cs | 28 ++++++++++++------- .../OtelServiceNameEnvVarDetectorTests.cs | 18 ++++++++---- .../Resources/ResourceTest.cs | 18 ++++++------ 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs index 236762ad098..8aa803df06a 100644 --- a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs +++ b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs @@ -22,7 +22,7 @@ namespace OpenTelemetry.Resources { internal class OtelEnvResourceDetector : IResourceDetector { - private const string OTelResourceEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; + public const string EnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; private const char AttributeListSplitter = ','; private const char AttributeKeyValueSplitter = '='; @@ -32,7 +32,7 @@ public Resource Detect() try { - string envResourceAttributeValue = Environment.GetEnvironmentVariable(OTelResourceEnvVarKey); + string envResourceAttributeValue = Environment.GetEnvironmentVariable(EnvVarKey); if (!string.IsNullOrEmpty(envResourceAttributeValue)) { var attributes = ParseResourceAttributes(envResourceAttributeValue); diff --git a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs index ad7f278e8a0..0839f19e6d1 100644 --- a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs +++ b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs @@ -21,7 +21,7 @@ namespace OpenTelemetry.Resources { internal class OtelServiceNameEnvVarDetector : IResourceDetector { - private const string EnvVarKey = "OTEL_SERVICE_NAME"; + public const string EnvVarKey = "OTEL_SERVICE_NAME"; public Resource Detect() { diff --git a/test/OpenTelemetry.Tests/Resources/OtelEnvResourceDetectorTest.cs b/test/OpenTelemetry.Tests/Resources/OtelEnvResourceDetectorTest.cs index 8ca21cdb0b9..dc1a8a376e2 100644 --- a/test/OpenTelemetry.Tests/Resources/OtelEnvResourceDetectorTest.cs +++ b/test/OpenTelemetry.Tests/Resources/OtelEnvResourceDetectorTest.cs @@ -22,11 +22,24 @@ namespace OpenTelemetry.Resources.Tests { public class OtelEnvResourceDetectorTest : IDisposable { - private const string OtelEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; - public OtelEnvResourceDetectorTest() { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, null); + } + + public void Dispose() + { + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, null); + } + + [Fact] + public void OtelEnvResource_EnvVarKey() + { + // Act + var resource = new OtelServiceNameEnvVarDetector().Detect(); + + // Assert + Assert.Equal("OTEL_RESOURCE_ATTRIBUTES", OtelEnvResourceDetector.EnvVarKey); } [Fact] @@ -44,7 +57,7 @@ public void OtelEnvResource_WithEnvVar_1() { // Arrange var envVarValue = "Key1=Val1,Key2=Val2"; - Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, envVarValue); var resource = new OtelEnvResourceDetector().Detect(); // Assert @@ -57,7 +70,7 @@ public void OtelEnvResource_WithEnvVar_2() { // Arrange var envVarValue = "Key1,Key2=Val2"; - Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, envVarValue); var resource = new OtelEnvResourceDetector().Detect(); // Assert @@ -65,10 +78,5 @@ public void OtelEnvResource_WithEnvVar_2() Assert.Single(resource.Attributes); Assert.Contains(new KeyValuePair("Key2", "Val2"), resource.Attributes); } - - public void Dispose() - { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); - } } } diff --git a/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs index 45595b8fa97..27ef814c3af 100644 --- a/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs +++ b/test/OpenTelemetry.Tests/Resources/OtelServiceNameEnvVarDetectorTests.cs @@ -22,16 +22,24 @@ namespace OpenTelemetry.Resources.Tests { public class OtelServiceNameEnvVarDetectorTests : IDisposable { - private const string OtelEnvVarKey = "OTEL_SERVICE_NAME"; - public OtelServiceNameEnvVarDetectorTests() { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, null); } public void Dispose() { - Environment.SetEnvironmentVariable(OtelEnvVarKey, null); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, null); + } + + [Fact] + public void OtelServiceNameEnvVar_EnvVarKey() + { + // Act + var resource = new OtelServiceNameEnvVarDetector().Detect(); + + // Assert + Assert.Equal("OTEL_SERVICE_NAME", OtelServiceNameEnvVarDetector.EnvVarKey); } [Fact] @@ -49,7 +57,7 @@ public void OtelServiceNameEnvVar_WithValue() { // Arrange var envVarValue = "my-service"; - Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, envVarValue); // Act var resource = new OtelServiceNameEnvVarDetector().Detect(); diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 39310d90535..12288630a65 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -25,8 +25,6 @@ public class ResourceTest : IDisposable { private const string KeyName = "key"; private const string ValueName = "value"; - private const string ResourceAttributesEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; - private const string ServiceNameEnvVarKey = "OTEL_SERVICE_NAME"; public ResourceTest() { @@ -430,7 +428,7 @@ public void GetResourceWithDefaultAttributes_ResourceWithAttrs() public void GetResourceWithDefaultAttributes_WithResourceEnvVar() { // Arrange - Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2"); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2"); var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert @@ -446,7 +444,7 @@ public void GetResourceWithDefaultAttributes_WithResourceEnvVar() public void GetResource_WithServiceEnvVar() { // Arrange - Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "some-service"); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, "some-service"); var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert @@ -460,8 +458,8 @@ public void GetResource_WithServiceEnvVar() public void GetResource_WithServiceNameSetWithTwoEnvVars() { // Arrange - Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "service.name=from-resource-attr"); - Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "from-service-name"); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, "service.name=from-resource-attr"); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, "from-service-name"); var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert @@ -475,8 +473,8 @@ public void GetResource_WithServiceNameSetWithTwoEnvVars() public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode() { // Arrange - Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, "service.name=from-resource-attr"); - Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, "from-service-name"); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, "service.name=from-resource-attr"); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, "from-service-name"); var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddService("from-code").AddAttributes(this.CreateAttributes(2)).Build(); // Assert @@ -488,8 +486,8 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode() private static void ClearEnvVars() { - Environment.SetEnvironmentVariable(ResourceAttributesEnvVarKey, null); - Environment.SetEnvironmentVariable(ServiceNameEnvVarKey, null); + Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, null); + Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, null); } private static void AddAttributes(Dictionary attributes, int attributeCount, int startIndex = 0) From e0a9aa4011aaec4eca76ed6219da017cf11c524f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 30 Jul 2021 20:33:06 +0200 Subject: [PATCH 11/12] OtelServiceNameEnvVarDetector handles SecurityException --- .../Resources/OtelEnvResourceDetector.cs | 2 +- .../OtelServiceNameEnvVarDetector.cs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs index 8aa803df06a..4d5b7e6a949 100644 --- a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs +++ b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs @@ -41,7 +41,7 @@ public Resource Detect() } catch (Exception ex) { - OpenTelemetrySdkEventSource.Log.ResourceDetectorFailed("OtelEnvResourceDetector", ex.Message); + OpenTelemetrySdkEventSource.Log.ResourceDetectorFailed(nameof(OtelEnvResourceDetector), ex.Message); } return resource; diff --git a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs index 0839f19e6d1..806ced7c793 100644 --- a/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs +++ b/src/OpenTelemetry/Resources/OtelServiceNameEnvVarDetector.cs @@ -16,6 +16,8 @@ using System; using System.Collections.Generic; +using System.Security; +using OpenTelemetry.Internal; namespace OpenTelemetry.Resources { @@ -27,13 +29,20 @@ public Resource Detect() { var resource = Resource.Empty; - string envResourceAttributeValue = Environment.GetEnvironmentVariable(EnvVarKey); - if (!string.IsNullOrEmpty(envResourceAttributeValue)) + try { - resource = new Resource(new Dictionary + string envResourceAttributeValue = Environment.GetEnvironmentVariable(EnvVarKey); + if (!string.IsNullOrEmpty(envResourceAttributeValue)) { - [ResourceSemanticConventions.AttributeServiceName] = envResourceAttributeValue, - }); + resource = new Resource(new Dictionary + { + [ResourceSemanticConventions.AttributeServiceName] = envResourceAttributeValue, + }); + } + } + catch (SecurityException ex) + { + OpenTelemetrySdkEventSource.Log.ResourceDetectorFailed(nameof(OtelServiceNameEnvVarDetector), ex.Message); } return resource; From b54fe524ab6dda37a63dfebd823ce79c557a8dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 6 Aug 2021 10:58:14 +0200 Subject: [PATCH 12/12] OtelEnvResourceDetector catches SecurityException --- src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs index 4d5b7e6a949..cd424e2d034 100644 --- a/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs +++ b/src/OpenTelemetry/Resources/OtelEnvResourceDetector.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Security; using OpenTelemetry.Internal; namespace OpenTelemetry.Resources @@ -39,7 +40,7 @@ public Resource Detect() resource = new Resource(attributes); } } - catch (Exception ex) + catch (SecurityException ex) { OpenTelemetrySdkEventSource.Log.ResourceDetectorFailed(nameof(OtelEnvResourceDetector), ex.Message); }