Skip to content

Commit

Permalink
Default Resource should have service.name attribute (#1744)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Austin-Tan committed Feb 1, 2021
1 parent 85967ba commit 18db60d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ private ResourceBuilder()
{
}

private static Resource DefaultResource { get; } = new Resource(new Dictionary<string, object>
{
[ResourceSemanticConventions.AttributeServiceName] = "unknown_service"
+ (string.IsNullOrWhiteSpace(System.Diagnostics.Process.GetCurrentProcess().ProcessName)
? string.Empty :
":" + System.Diagnostics.Process.GetCurrentProcess().ProcessName),
});

/// <summary>
/// Creates a <see cref="ResourceBuilder"/> instance with SDK defaults
/// added. See <a
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/">resource
/// Creates a <see cref="ResourceBuilder"/> instance with Default
/// service.name 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.
/// </summary>
/// <returns>Created <see cref="ResourceBuilder"/>.</returns>
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);

/// <summary>
/// Creates an empty <see cref="ResourceBuilder"/> instance.
Expand Down
31 changes: 25 additions & 6 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ public void MergeResource_UpdatingResourceOverridesCurrentResource()
Assert.Contains(new KeyValuePair<string, object>("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()
{
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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<string, object>("EVKey1", "EVVal1"), attributes);
Assert.Contains(new KeyValuePair<string, object>("EVKey2", "EVVal2"), attributes);
}
Expand Down Expand Up @@ -412,6 +424,13 @@ private static void ValidateTelemetrySdkAttributes(IEnumerable<KeyValuePair<stri
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 Dictionary<string, object> CreateAttributes(int attributeCount, int startIndex = 0)
{
var attributes = new Dictionary<string, object>();
Expand Down
15 changes: 15 additions & 0 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>("telemetry.sdk.name", "opentelemetry"), attributes);
Expand Down

0 comments on commit 18db60d

Please sign in to comment.