From 18db60d8c7a044e89427262a6fb5edb84e34ee80 Mon Sep 17 00:00:00 2001 From: Austin Tan Date: Mon, 1 Feb 2021 12:51:11 -0800 Subject: [PATCH] Default Resource should have service.name attribute (#1744) * Moving the previous default (SDK info) into separate method * Add CreateDefault and add to API * Converting tests and adding actual default servicename * Fixing Tracer Test with SDKResoruce * removing api reference * Fixing tests * Changelog and API * Re-add test with default ProviderBuilder and privatize method * reverting API changes * Removing AddDfault method altogether, performing addition inside CreateDefault * Resolving comments - main instead of master, shortened changelog.md * Moved to unreleased * markdownlint fix (blank lines) * Closing brace should not be preceded by blank line * Removing colon from unknown_service when no processname * removing comma check from sdk test * link to correct spec section * one other case of the colon missing * Correcting - reversing string.empty and processname in ternary --- src/OpenTelemetry/CHANGELOG.md | 3 ++ .../Resources/ResourceBuilder.cs | 16 +++++++--- .../Resources/ResourceTest.cs | 31 +++++++++++++++---- .../Trace/TracerProviderSdkTest.cs | 15 +++++++++ 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 72f935b74c4..5930d1418f6 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Default `Resource` will now contain service.name instead of Telemetry SDK. + [#1744](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1744) + ## 1.0.0-rc2 Released 2021-Jan-29 diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index ac89b4470c1..4bc8cc500cd 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -30,15 +30,23 @@ private ResourceBuilder() { } + private static Resource DefaultResource { get; } = new Resource(new Dictionary + { + [ResourceSemanticConventions.AttributeServiceName] = "unknown_service" + + (string.IsNullOrWhiteSpace(System.Diagnostics.Process.GetCurrentProcess().ProcessName) + ? string.Empty : + ":" + System.Diagnostics.Process.GetCurrentProcess().ProcessName), + }); + /// - /// Creates a instance with SDK defaults - /// added. See resource + /// Creates a instance with Default + /// service.name added. See resource /// semantic conventions for details. /// /// Created . public static ResourceBuilder CreateDefault() - => new ResourceBuilder().AddTelemetrySdk(); // TODO: Seek spec clarify on whether or not OtelEnvResourceDetector should be added by default. + => new ResourceBuilder().AddResource(DefaultResource); /// /// Creates an empty instance. diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 3abd7845d3f..09ac62edbac 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -330,6 +330,18 @@ public void MergeResource_UpdatingResourceOverridesCurrentResource() Assert.Contains(new KeyValuePair("value", "updatedValue"), newResource.Attributes); } + [Fact] + public void GetResourceWithTelemetrySDKAttributes() + { + // Arrange + var resource = ResourceBuilder.CreateDefault().AddTelemetrySdk().AddEnvironmentVariableDetector().Build(); + + // Assert + var attributes = resource.Attributes; + Assert.Equal(4, attributes.Count()); + ValidateTelemetrySdkAttributes(attributes); + } + [Fact] public void GetResourceWithDefaultAttributes_EmptyResource() { @@ -338,8 +350,8 @@ public void GetResourceWithDefaultAttributes_EmptyResource() // Assert var attributes = resource.Attributes; - Assert.Equal(3, attributes.Count()); - ValidateTelemetrySdkAttributes(attributes); + Assert.Single(attributes); + ValidateDefaultAttributes(attributes); } [Fact] @@ -350,9 +362,9 @@ public void GetResourceWithDefaultAttributes_ResourceWithAttrs() // Assert var attributes = resource.Attributes; - Assert.Equal(5, attributes.Count()); + Assert.Equal(3, attributes.Count()); ValidateAttributes(attributes, 0, 1); - ValidateTelemetrySdkAttributes(attributes); + ValidateDefaultAttributes(attributes); } [Fact] @@ -364,9 +376,9 @@ public void GetResourceWithDefaultAttributes_WithEnvVar() // Assert var attributes = resource.Attributes; - Assert.Equal(7, attributes.Count()); + Assert.Equal(5, attributes.Count()); ValidateAttributes(attributes, 0, 1); - ValidateTelemetrySdkAttributes(attributes); + ValidateDefaultAttributes(attributes); Assert.Contains(new KeyValuePair("EVKey1", "EVVal1"), attributes); Assert.Contains(new KeyValuePair("EVKey2", "EVVal2"), attributes); } @@ -412,6 +424,13 @@ private static void ValidateTelemetrySdkAttributes(IEnumerable> attributes) + { + var serviceName = attributes.Where(pair => pair.Key.Equals("service.name")); + Assert.Single(serviceName); + Assert.Contains("unknown_service", serviceName.FirstOrDefault().Value as string); + } + private Dictionary CreateAttributes(int attributeCount, int startIndex = 0) { var attributes = new Dictionary(); diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index de7e0be8376..e193cb9b5b6 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -375,6 +375,21 @@ public void TracerProviderSdkBuildsWithDefaultResource() var resource = tracerProvider.GetResource(); var attributes = resource.Attributes; + Assert.NotNull(resource); + Assert.NotEqual(Resource.Empty, resource); + Assert.Single(resource.Attributes); + Assert.Equal(resource.Attributes.FirstOrDefault().Key, ResourceSemanticConventions.AttributeServiceName); + Assert.Contains("unknown_service", (string)resource.Attributes.FirstOrDefault().Value); + } + + [Fact] + public void TracerProviderSdkBuildsWithSDKResource() + { + var tracerProvider = Sdk.CreateTracerProviderBuilder().SetResourceBuilder( + ResourceBuilder.CreateDefault().AddTelemetrySdk()).Build(); + var resource = tracerProvider.GetResource(); + var attributes = resource.Attributes; + Assert.NotNull(resource); Assert.NotEqual(Resource.Empty, resource); Assert.Contains(new KeyValuePair("telemetry.sdk.name", "opentelemetry"), attributes);