Skip to content

Commit

Permalink
Merge the current Resource object with custom Resource Object. (#5619)
Browse files Browse the repository at this point in the history
  • Loading branch information
parth1601 authored Aug 9, 2023
1 parent 88c80b1 commit f52050b
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** 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)
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** 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)
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** 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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}. */
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit f52050b

Please sign in to comment.