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 environment variable detectors to the default ResourceBuilder #2247

Merged
merged 5 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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

* `ResourceBuilder.CreateDefault` has detectors for
`OTEL_RESOURCE_ATTRIBUTES`, `OTEL_SERVICE_NAME` environment variables
so that explicit `AddEnvironmentVariableDetector` call is not needed. ([#2247](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2247))
cijothomas marked this conversation as resolved.
Show resolved Hide resolved

* `ResourceBuilder.AddEnvironmentVariableDetector` handles `OTEL_SERVICE_NAME`
environmental variable. ([#2209](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2209))

Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.Build();
```

It is also possible to configure the `Resource` by using `AddEnvironmentVariableDetector`
together with following environmental variables:
It is also possible to configure the `Resource` by using following
environmental variables:

<!-- markdownlint-disable MD013 -->
| Environment variable | Description |
Expand Down
6 changes: 5 additions & 1 deletion src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ private ResourceBuilder()
/// 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.
/// Additionally it adds resource attributes parsed from OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME environment variables
/// to a <see cref="ResourceBuilder"/> following the <a
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable">Resource
/// SDK</a>.
/// </summary>
/// <returns>Created <see cref="ResourceBuilder"/>.</returns>
public static ResourceBuilder CreateDefault()
=> new ResourceBuilder().AddResource(DefaultResource);
=> new ResourceBuilder().AddResource(DefaultResource).AddEnvironmentVariableDetector();

/// <summary>
/// Creates an empty <see cref="ResourceBuilder"/> instance.
Expand Down
14 changes: 7 additions & 7 deletions test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void MergeResource_UpdatingResourceOverridesCurrentResource()
public void GetResourceWithTelemetrySDKAttributes()
{
// Arrange
var resource = ResourceBuilder.CreateDefault().AddTelemetrySdk().AddEnvironmentVariableDetector().Build();
var resource = ResourceBuilder.CreateDefault().AddTelemetrySdk().Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -403,7 +403,7 @@ public void GetResourceWithTelemetrySDKAttributes()
public void GetResourceWithDefaultAttributes_EmptyResource()
{
// Arrange
var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().Build();
var resource = ResourceBuilder.CreateDefault().Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -415,7 +415,7 @@ public void GetResourceWithDefaultAttributes_EmptyResource()
public void GetResourceWithDefaultAttributes_ResourceWithAttrs()
{
// Arrange
var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build();
var resource = ResourceBuilder.CreateDefault().AddAttributes(this.CreateAttributes(2)).Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -429,7 +429,7 @@ public void GetResourceWithDefaultAttributes_WithResourceEnvVar()
{
// Arrange
Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2");
var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build();
var resource = ResourceBuilder.CreateDefault().AddAttributes(this.CreateAttributes(2)).Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -445,7 +445,7 @@ public void GetResource_WithServiceEnvVar()
{
// Arrange
Environment.SetEnvironmentVariable(OtelServiceNameEnvVarDetector.EnvVarKey, "some-service");
var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build();
var resource = ResourceBuilder.CreateDefault().AddAttributes(this.CreateAttributes(2)).Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -460,7 +460,7 @@ public void GetResource_WithServiceNameSetWithTwoEnvVars()
// Arrange
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();
var resource = ResourceBuilder.CreateDefault().AddAttributes(this.CreateAttributes(2)).Build();

// Assert
var attributes = resource.Attributes;
Expand All @@ -475,7 +475,7 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode()
// Arrange
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();
var resource = ResourceBuilder.CreateDefault().AddService("from-code").AddAttributes(this.CreateAttributes(2)).Build();

// Assert
var attributes = resource.Attributes;
Expand Down