Skip to content

Commit

Permalink
service.instance.id implementation (#6226)
Browse files Browse the repository at this point in the history
Co-authored-by: Jack Berg <jberg@newrelic.com>
  • Loading branch information
zeitlinger and jack-berg committed Apr 2, 2024
1 parent 98eded9 commit ec46407
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public Resource createResource(ConfigProperties config) {

@Override
public int order() {
// Environment resource takes precedent over all other ResourceProviders
return Integer.MAX_VALUE;
// Environment resource takes precedent over all other ResourceProviders except
// ServiceInstanceIdResourceProvider.
return Integer.MAX_VALUE - 1;
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.sdk.extension.incubator.resources.ServiceInstanceIdResourceProvider
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);
}
}));
}
}

0 comments on commit ec46407

Please sign in to comment.