From f52050bc2d3867df3e5005b579bddec2ee194723 Mon Sep 17 00:00:00 2001 From: Parth Dadhaniya <70507401+parth1601@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:58:25 +0530 Subject: [PATCH] Merge the current Resource object with custom Resource Object. (#5619) --- .../opentelemetry-sdk-logs.txt | 4 ++- .../opentelemetry-sdk-metrics.txt | 4 ++- .../opentelemetry-sdk-trace.txt | 4 ++- .../sdk/logs/SdkLoggerProviderBuilder.java | 12 +++++++ .../logs/SdkLoggerProviderBuilderTest.java | 31 +++++++++++++++++++ .../sdk/metrics/SdkMeterProviderBuilder.java | 11 +++++++ .../metrics/SdkMeterProviderBuilderTest.java | 20 ++++++++++++ .../sdk/trace/SdkTracerProviderBuilder.java | 12 +++++++ .../trace/SdkTracerProviderBuilderTest.java | 31 +++++++++++++++++++ 9 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderBuilderTest.java create mode 100644 sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilderTest.java diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt index df26146497b..836280b5bed 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt @@ -1,2 +1,4 @@ Comparing source compatibility of against -No changes. \ No newline at end of file +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder addResource(io.opentelemetry.sdk.resources.Resource) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt index df26146497b..f504766328b 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt @@ -1,2 +1,4 @@ Comparing source compatibility of against -No changes. \ No newline at end of file +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder addResource(io.opentelemetry.sdk.resources.Resource) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt index df26146497b..7cc166e26ed 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt @@ -1,2 +1,4 @@ Comparing source compatibility of against -No changes. \ No newline at end of file +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.SdkTracerProviderBuilder (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder addResource(io.opentelemetry.sdk.resources.Resource) 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 485731822f1..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 @@ -15,6 +15,7 @@ import io.opentelemetry.sdk.resources.Resource; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.function.Supplier; /** @@ -95,4 +96,15 @@ public SdkLoggerProviderBuilder setClock(Clock clock) { public SdkLoggerProvider build() { return new SdkLoggerProvider(resource, logLimitsSupplier, logRecordProcessors, clock); } + + /** + * Merge custom resource object with the current resource object. + * + * @param resource {@link Resource} object to merge with current. + */ + public SdkLoggerProviderBuilder addResource(Resource resource) { + Objects.requireNonNull(resource, "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 d4657de438c..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 @@ -134,4 +134,15 @@ SdkMeterProviderBuilder registerMetricReader( public SdkMeterProvider build() { return new SdkMeterProvider(registeredViews, metricReaders, clock, resource, exemplarFilter); } + + /** + * Merge custom resource object with the current resource object. + * + * @param resource {@link Resource} object to merge with current. + */ + public SdkMeterProviderBuilder addResource(Resource resource) { + Objects.requireNonNull(resource, "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 7801a3fa728..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 @@ -12,6 +12,7 @@ import io.opentelemetry.sdk.trace.samplers.Sampler; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.function.Supplier; /** Builder of {@link SdkTracerProvider}. */ @@ -145,4 +146,15 @@ public SdkTracerProvider build() { } SdkTracerProviderBuilder() {} + + /** + * Merge custom resource object with the current resource object. + * + * @param resource {@link Resource} object to merge with current. + */ + public SdkTracerProviderBuilder addResource(Resource resource) { + Objects.requireNonNull(resource, "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)); + } +}