From 3980a86c1e2c08f41719f96728f68df82704eea2 Mon Sep 17 00:00:00 2001
From: Alan West <3676547+alanwest@users.noreply.github.com>
Date: Wed, 5 Apr 2023 15:04:22 -0700
Subject: [PATCH 1/5] Add telemetry.sdk.* attributes to default resource
---
.../Resources/ResourceBuilder.cs | 5 ++++-
.../Resources/ResourceTest.cs | 22 +++++++++++++------
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs
index c9de4fa77e7..ed35470dd90 100644
--- a/src/OpenTelemetry/Resources/ResourceBuilder.cs
+++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs
@@ -70,7 +70,10 @@ private ResourceBuilder()
///
/// Created .
public static ResourceBuilder CreateDefault()
- => new ResourceBuilder().AddResource(DefaultResource).AddEnvironmentVariableDetector();
+ => new ResourceBuilder()
+ .AddResource(DefaultResource)
+ .AddTelemetrySdk()
+ .AddEnvironmentVariableDetector();
///
/// Creates an empty instance.
diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
index c1ab60fe0c6..3acbaf215af 100644
--- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
+++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
@@ -395,6 +395,7 @@ public void GetResourceWithTelemetrySDKAttributes()
// Assert
var attributes = resource.Attributes;
Assert.Equal(4, attributes.Count());
+ ValidateDefaultAttributes(attributes);
ValidateTelemetrySdkAttributes(attributes);
}
@@ -406,8 +407,9 @@ public void GetResourceWithDefaultAttributes_EmptyResource()
// Assert
var attributes = resource.Attributes;
- Assert.Single(attributes);
+ Assert.Equal(4, attributes.Count());
ValidateDefaultAttributes(attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -418,9 +420,10 @@ public void GetResourceWithDefaultAttributes_ResourceWithAttrs()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(3, attributes.Count());
+ Assert.Equal(6, attributes.Count());
ValidateAttributes(attributes, 0, 1);
ValidateDefaultAttributes(attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -432,11 +435,12 @@ public void GetResourceWithDefaultAttributes_WithResourceEnvVar()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(5, attributes.Count());
+ Assert.Equal(8, attributes.Count());
ValidateAttributes(attributes, 0, 1);
ValidateDefaultAttributes(attributes);
Assert.Contains(new KeyValuePair("EVKey1", "EVVal1"), attributes);
Assert.Contains(new KeyValuePair("EVKey2", "EVVal2"), attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -448,9 +452,10 @@ public void EnvironmentVariableDetectors_DoNotDuplicateAttributes()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(3, attributes.Count());
+ Assert.Equal(6, attributes.Count());
Assert.Contains(new KeyValuePair("EVKey1", "EVVal1"), attributes);
Assert.Contains(new KeyValuePair("EVKey2", "EVVal2"), attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -462,9 +467,10 @@ public void GetResource_WithServiceEnvVar()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(3, attributes.Count());
+ Assert.Equal(6, attributes.Count());
ValidateAttributes(attributes, 0, 1);
Assert.Contains(new KeyValuePair("service.name", "some-service"), attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -477,9 +483,10 @@ public void GetResource_WithServiceNameSetWithTwoEnvVars()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(3, attributes.Count());
+ Assert.Equal(6, attributes.Count());
ValidateAttributes(attributes, 0, 1);
Assert.Contains(new KeyValuePair("service.name", "from-service-name"), attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
@@ -492,9 +499,10 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode()
// Assert
var attributes = resource.Attributes;
- Assert.Equal(4, attributes.Count());
+ Assert.Equal(7, attributes.Count());
ValidateAttributes(attributes, 0, 1);
Assert.Contains(new KeyValuePair("service.name", "from-code"), attributes);
+ ValidateTelemetrySdkAttributes(attributes);
}
[Fact]
From 15558e9e2497174acd42546095909de50c87c0e8 Mon Sep 17 00:00:00 2001
From: Alan West <3676547+alanwest@users.noreply.github.com>
Date: Wed, 5 Apr 2023 15:56:00 -0700
Subject: [PATCH 2/5] Fix test
---
test/OpenTelemetry.Tests/Resources/ResourceTest.cs | 4 ++--
test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs | 9 ++++++---
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
index 3acbaf215af..418f497fcd9 100644
--- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
+++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
@@ -602,7 +602,7 @@ private static void ValidateResource(Resource resource, int attributeCount)
ValidateAttributes(resource.Attributes);
}
- private static void ValidateTelemetrySdkAttributes(IEnumerable> attributes)
+ internal static void ValidateTelemetrySdkAttributes(IEnumerable> attributes)
{
Assert.Contains(new KeyValuePair("telemetry.sdk.name", "opentelemetry"), attributes);
Assert.Contains(new KeyValuePair("telemetry.sdk.language", "dotnet"), attributes);
@@ -610,7 +610,7 @@ private static void ValidateTelemetrySdkAttributes(IEnumerable> attributes)
+ internal static void ValidateDefaultAttributes(IEnumerable> attributes)
{
var serviceName = attributes.Where(pair => pair.Key.Equals("service.name"));
Assert.Single(serviceName);
diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
index 878d951e70a..abdee03731f 100644
--- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
+++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
@@ -17,6 +17,7 @@
using System.Diagnostics;
using OpenTelemetry.Instrumentation;
using OpenTelemetry.Resources;
+using OpenTelemetry.Resources.Tests;
using OpenTelemetry.Tests;
using Xunit;
@@ -1077,9 +1078,11 @@ public void TracerProviderSdkBuildsWithDefaultResource()
Assert.NotNull(resource);
Assert.NotEqual(Resource.Empty, resource);
- Assert.Single(resource.Attributes);
- Assert.Equal(ResourceSemanticConventions.AttributeServiceName, resource.Attributes.FirstOrDefault().Key);
- Assert.Contains("unknown_service", (string)resource.Attributes.FirstOrDefault().Value);
+
+ var attributes = resource.Attributes;
+ Assert.Equal(4, attributes.Count());
+ ResourceTest.ValidateDefaultAttributes(attributes);
+ ResourceTest.ValidateTelemetrySdkAttributes(attributes);
}
[Theory]
From e276858c4ab90dc8d0157cb97362174a834ccacd Mon Sep 17 00:00:00 2001
From: Alan West <3676547+alanwest@users.noreply.github.com>
Date: Wed, 5 Apr 2023 16:02:46 -0700
Subject: [PATCH 3/5] Internals before privates
---
.../Resources/ResourceTest.cs | 30 +++++++++----------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
index 418f497fcd9..31d0fbdf4f3 100644
--- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
+++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs
@@ -570,6 +570,21 @@ public void ResourceBuilder_AddDetectorInternal_Test()
Assert.True(validTestRun);
}
+ internal static void ValidateTelemetrySdkAttributes(IEnumerable> attributes)
+ {
+ Assert.Contains(new KeyValuePair("telemetry.sdk.name", "opentelemetry"), attributes);
+ Assert.Contains(new KeyValuePair("telemetry.sdk.language", "dotnet"), attributes);
+ var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
+ Assert.Single(versionAttribute);
+ }
+
+ internal static void ValidateDefaultAttributes(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 static void ClearEnvVars()
{
Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, null);
@@ -602,21 +617,6 @@ private static void ValidateResource(Resource resource, int attributeCount)
ValidateAttributes(resource.Attributes);
}
- internal static void ValidateTelemetrySdkAttributes(IEnumerable> attributes)
- {
- Assert.Contains(new KeyValuePair("telemetry.sdk.name", "opentelemetry"), attributes);
- Assert.Contains(new KeyValuePair("telemetry.sdk.language", "dotnet"), attributes);
- var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
- Assert.Single(versionAttribute);
- }
-
- internal static void ValidateDefaultAttributes(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 static Dictionary CreateAttributes(int attributeCount, int startIndex = 0)
{
var attributes = new Dictionary();
From d30dbd7f74d4c677684ffd4686b16c779affe3d9 Mon Sep 17 00:00:00 2001
From: Alan West <3676547+alanwest@users.noreply.github.com>
Date: Wed, 5 Apr 2023 16:11:04 -0700
Subject: [PATCH 4/5] Update method description
---
src/OpenTelemetry/Resources/ResourceBuilder.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs
index ed35470dd90..9b70509190b 100644
--- a/src/OpenTelemetry/Resources/ResourceBuilder.cs
+++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs
@@ -59,8 +59,8 @@ private ResourceBuilder()
internal IServiceProvider? ServiceProvider { get; set; }
///
- /// Creates a instance with Default
- /// service.name added. See instance with default attributes
+ /// added. See resource
/// semantic conventions for details.
/// Additionally it adds resource attributes parsed from OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME environment variables
From 65825a029425939ce74a3deeef3e47c82d7be438 Mon Sep 17 00:00:00 2001
From: Alan West <3676547+alanwest@users.noreply.github.com>
Date: Wed, 5 Apr 2023 16:16:17 -0700
Subject: [PATCH 5/5] Update changelog
---
src/OpenTelemetry/CHANGELOG.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md
index 1f9f22a70eb..66e89aa8292 100644
--- a/src/OpenTelemetry/CHANGELOG.md
+++ b/src/OpenTelemetry/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+* The default resource provided by `ResourceBuilder.CreateDefault()` now
+ adds the `telemetry.sdk.*` attributes defined in the
+ [specification](https://github.com/open-telemetry/opentelemetry-specification/tree/12fcec1ff255b1535db75708e52a3a21f86f0fae/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value).
+ ([#4369](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4369))
* Fixed an issue with `HashCode` computations throwing exceptions on .NET
Standard 2.1 targets.
([#4362](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4362))