Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service.instance.id implementation #6226

Merged
merged 14 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
}
}));
}
}