-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service.instance.id implementation (#6226)
Co-authored-by: Jack Berg <jberg@newrelic.com>
- Loading branch information
1 parent
98eded9
commit ec46407
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...io/opentelemetry/sdk/extension/incubator/resources/ServiceInstanceIdResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.resources; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.UUID; | ||
|
||
/** | ||
* does not implement {@link ResourceProvider}, because it depends on all attributes discovered by | ||
* the other providers. | ||
*/ | ||
public final class ServiceInstanceIdResourceProvider implements ConditionalResourceProvider { | ||
|
||
public static final AttributeKey<String> SERVICE_INSTANCE_ID = | ||
AttributeKey.stringKey("service.instance.id"); | ||
|
||
// multiple calls to this resource provider should return the same value | ||
private static final Resource RANDOM = | ||
Resource.create(Attributes.of(SERVICE_INSTANCE_ID, UUID.randomUUID().toString())); | ||
|
||
static final int ORDER = Integer.MAX_VALUE; | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
return RANDOM; | ||
} | ||
|
||
@Override | ||
public boolean shouldApply(ConfigProperties config, Resource existing) { | ||
return existing.getAttribute(SERVICE_INSTANCE_ID) == null; | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// Run after environment resource provider - only set the service instance ID if it | ||
// hasn't been set by any other provider or the user. | ||
return ORDER; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.opentelemetry.sdk.extension.incubator.resources.ServiceInstanceIdResourceProvider |
71 changes: 71 additions & 0 deletions
71
...pentelemetry/sdk/extension/incubator/resources/ServiceInstanceIdResourceProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.resources; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
class ServiceInstanceIdResourceProviderTest { | ||
|
||
private static class TestCase { | ||
private final String name; | ||
final String expectedValue; | ||
final Map<String, String> attributes; | ||
|
||
TestCase(String name, String expectedValue, Map<String, String> attributes) { | ||
this.name = name; | ||
this.expectedValue = expectedValue; | ||
this.attributes = attributes; | ||
} | ||
} | ||
|
||
@TestFactory | ||
Stream<DynamicTest> createResource() { | ||
return Stream.of( | ||
new TestCase( | ||
"user provided service.instance.id", | ||
null, | ||
ImmutableMap.of("service.instance.id", "custom")), | ||
new TestCase("random value", "random", Collections.emptyMap())) | ||
.map( | ||
testCase -> | ||
DynamicTest.dynamicTest( | ||
testCase.name, | ||
() -> { | ||
ServiceInstanceIdResourceProvider provider = | ||
new ServiceInstanceIdResourceProvider(); | ||
DefaultConfigProperties config = | ||
DefaultConfigProperties.createFromMap(Collections.emptyMap()); | ||
AttributesBuilder builder = Attributes.builder(); | ||
testCase.attributes.forEach(builder::put); | ||
Resource existing = Resource.create(builder.build()); | ||
Resource resource = | ||
provider.shouldApply(config, existing) | ||
? provider.createResource(config) | ||
: Resource.empty(); | ||
|
||
String actual = | ||
resource | ||
.getAttributes() | ||
.get(ServiceInstanceIdResourceProvider.SERVICE_INSTANCE_ID); | ||
if ("random".equals(testCase.expectedValue)) { | ||
assertThat(actual).isNotNull(); | ||
} else { | ||
assertThat(actual).isEqualTo(testCase.expectedValue); | ||
} | ||
})); | ||
} | ||
} |