Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry.sdk.* attributes to default resource #4369

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ private ResourceBuilder()
internal IServiceProvider? ServiceProvider { get; set; }

/// <summary>
/// Creates a <see cref="ResourceBuilder"/> instance with Default
/// service.name added. See <a
/// Creates a <see cref="ResourceBuilder"/> instance with default attributes
/// added. See <a
/// href="https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value">resource
/// semantic conventions</a> for details.
/// Additionally it adds resource attributes parsed from OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME environment variables
Expand All @@ -70,7 +70,10 @@ private ResourceBuilder()
/// </summary>
/// <returns>Created <see cref="ResourceBuilder"/>.</returns>
public static ResourceBuilder CreateDefault()
alanwest marked this conversation as resolved.
Show resolved Hide resolved
=> new ResourceBuilder().AddResource(DefaultResource).AddEnvironmentVariableDetector();
=> new ResourceBuilder()
.AddResource(DefaultResource)
.AddTelemetrySdk()
.AddEnvironmentVariableDetector();

/// <summary>
/// Creates an empty <see cref="ResourceBuilder"/> instance.
Expand Down
52 changes: 30 additions & 22 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public void GetResourceWithTelemetrySDKAttributes()
// Assert
var attributes = resource.Attributes;
Assert.Equal(4, attributes.Count());
ValidateDefaultAttributes(attributes);
ValidateTelemetrySdkAttributes(attributes);
}

Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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<string, object>("EVKey1", "EVVal1"), attributes);
Assert.Contains(new KeyValuePair<string, object>("EVKey2", "EVVal2"), attributes);
ValidateTelemetrySdkAttributes(attributes);
}

[Fact]
Expand All @@ -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<string, object>("EVKey1", "EVVal1"), attributes);
Assert.Contains(new KeyValuePair<string, object>("EVKey2", "EVVal2"), attributes);
ValidateTelemetrySdkAttributes(attributes);
}

[Fact]
Expand All @@ -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<string, object>("service.name", "some-service"), attributes);
ValidateTelemetrySdkAttributes(attributes);
}

[Fact]
Expand All @@ -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<string, object>("service.name", "from-service-name"), attributes);
ValidateTelemetrySdkAttributes(attributes);
}

[Fact]
Expand All @@ -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<string, object>("service.name", "from-code"), attributes);
ValidateTelemetrySdkAttributes(attributes);
}

[Fact]
Expand Down Expand Up @@ -562,6 +570,21 @@ public void ResourceBuilder_AddDetectorInternal_Test()
Assert.True(validTestRun);
}

internal static void ValidateTelemetrySdkAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
{
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.name", "opentelemetry"), attributes);
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.language", "dotnet"), attributes);
var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
Assert.Single(versionAttribute);
}

internal static void ValidateDefaultAttributes(IEnumerable<KeyValuePair<string, object>> 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);
Expand Down Expand Up @@ -594,21 +617,6 @@ private static void ValidateResource(Resource resource, int attributeCount)
ValidateAttributes(resource.Attributes);
}

private static void ValidateTelemetrySdkAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
{
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.name", "opentelemetry"), attributes);
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.language", "dotnet"), attributes);
var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
Assert.Single(versionAttribute);
}

private static void ValidateDefaultAttributes(IEnumerable<KeyValuePair<string, object>> 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<string, object> CreateAttributes(int attributeCount, int startIndex = 0)
{
var attributes = new Dictionary<string, object>();
Expand Down
9 changes: 6 additions & 3 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using OpenTelemetry.Instrumentation;
using OpenTelemetry.Resources;
using OpenTelemetry.Resources.Tests;
using OpenTelemetry.Tests;
using Xunit;

Expand Down Expand Up @@ -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]
Expand Down