diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilder.java index 76bb35357d2..e20c6b04e1f 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilder.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilder.java @@ -104,7 +104,7 @@ public SdkLoggerProvider build() { */ public SdkLoggerProviderBuilder addResource(Resource resource) { Objects.requireNonNull(resource, "resource"); - this.resource.merge(resource); + this.resource = this.resource.merge(resource); return this; } } diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilderTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilderTest.java new file mode 100644 index 00000000000..5c49d35e5ca --- /dev/null +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilderTest.java @@ -0,0 +1,31 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.logs; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.resources.Resource; +import org.junit.jupiter.api.Test; + +public class SdkLoggerProviderBuilderTest { + + @Test + void addResource() { + Resource customResource = + Resource.create( + Attributes.of( + AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value")); + + SdkLoggerProvider sdkLoggerProvider = + SdkLoggerProvider.builder().addResource(customResource).build(); + + assertThat(sdkLoggerProvider) + .extracting("sharedState") + .hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource)); + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilder.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilder.java index d4c1fe49f89..1cad7d5b2e6 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilder.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilder.java @@ -142,7 +142,7 @@ public SdkMeterProvider build() { */ public SdkMeterProviderBuilder addResource(Resource resource) { Objects.requireNonNull(resource, "resource"); - this.resource.merge(resource); + this.resource = this.resource.merge(resource); return this; } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilderTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilderTest.java index 74e5e5dcc12..bb15dc070d1 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilderTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilderTest.java @@ -7,6 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import org.junit.jupiter.api.Test; @@ -23,4 +25,22 @@ void defaultResource() { .extracting("sharedState") .hasFieldOrPropertyWithValue("resource", Resource.getDefault()); } + + @Test + void addResource() { + Resource customResource = + Resource.create( + Attributes.of( + AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value")); + + SdkMeterProvider sdkMeterProvider = + SdkMeterProvider.builder() + .registerMetricReader(InMemoryMetricReader.create()) + .addResource(customResource) + .build(); + + assertThat(sdkMeterProvider) + .extracting("sharedState") + .hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource)); + } } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java index 98f8d25cb8e..963f3b16160 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java @@ -154,7 +154,7 @@ public SdkTracerProvider build() { */ public SdkTracerProviderBuilder addResource(Resource resource) { Objects.requireNonNull(resource, "resource"); - this.resource.merge(resource); + this.resource = this.resource.merge(resource); return this; } } diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilderTest.java new file mode 100644 index 00000000000..2f7b4c8ef75 --- /dev/null +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilderTest.java @@ -0,0 +1,31 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.trace; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.resources.Resource; +import org.junit.jupiter.api.Test; + +public class SdkTracerProviderBuilderTest { + + @Test + void addResource() { + Resource customResource = + Resource.create( + Attributes.of( + AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value")); + + SdkTracerProvider sdkTracerProvider = + SdkTracerProvider.builder().addResource(customResource).build(); + + assertThat(sdkTracerProvider) + .extracting("sharedState") + .hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource)); + } +}