-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add service.instance.id provider #9062
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
Closed
zeitlinger
wants to merge
1
commit into
open-telemetry:main
from
zeitlinger:service_instance_id_provider
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
...y/src/main/java/io/opentelemetry/instrumentation/resources/ServiceInstanceIdResource.java
This file contains hidden or 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,41 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.resources; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * In the spirit of <a | ||
| * href="https://docs.google.com/document/d/1BenPf9vsZHCf4JpHWGQBydKZAA4XdH38wuMD7JQnz9A/edit?pli=1#heading=h.lyyq7xqgpsj9">this | ||
| * proposal</a> | ||
| */ | ||
| public class ServiceInstanceIdResource { | ||
|
|
||
| public static final String RESOURCE_ATTRIBUTES = "otel.resource.attributes"; | ||
| public static final String RANDOM_INSTANCE_ID = UUID.randomUUID().toString(); | ||
|
|
||
| private ServiceInstanceIdResource() {} | ||
|
|
||
| public static Resource getResource(ConfigProperties config) { | ||
| Map<String, String> resourceAttributes = config.getMap(RESOURCE_ATTRIBUTES); | ||
| String k8s = k8sServiceInstanceId(resourceAttributes); | ||
| String value = k8s != null ? k8s : RANDOM_INSTANCE_ID; | ||
| return Resource.create( | ||
| Attributes.of(ResourceAttributes.SERVICE_INSTANCE_ID, value), | ||
| ResourceAttributes.SCHEMA_URL); | ||
| } | ||
|
|
||
| private static String k8sServiceInstanceId(Map<String, String> resource) { | ||
| String podName = resource.get(ResourceAttributes.K8S_POD_NAME.getKey()); | ||
| String containerName = resource.get(ResourceAttributes.K8S_CONTAINER_NAME.getKey()); | ||
| return podName != null && containerName != null ? podName + "/" + containerName : null; | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
...in/java/io/opentelemetry/instrumentation/resources/ServiceInstanceIdResourceProvider.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.resources; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| 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 io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
|
|
||
| /** {@link ResourceProvider} for automatically configuring {@link ServiceInstanceIdResource}. */ | ||
| @AutoService(ResourceProvider.class) | ||
| public final class ServiceInstanceIdResourceProvider implements ConditionalResourceProvider { | ||
|
|
||
| @Override | ||
| public Resource createResource(ConfigProperties config) { | ||
| return ServiceInstanceIdResource.getResource(config); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldApply(ConfigProperties config, Resource existing) { | ||
| return !config | ||
| .getMap(ServiceInstanceIdResource.RESOURCE_ATTRIBUTES) | ||
| .containsKey(ResourceAttributes.SERVICE_INSTANCE_ID.getKey()); | ||
| } | ||
|
Comment on lines
+23
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't really be needed, the |
||
| } | ||
67 changes: 67 additions & 0 deletions
67
...c/test/java/io/opentelemetry/instrumentation/resources/ServiceInstanceIdResourceTest.java
This file contains hidden or 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,67 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.resources; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Named.named; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| class ServiceInstanceIdResourceTest { | ||
|
|
||
| private static class Parameter { | ||
| public final Map<String, String> resource; | ||
| public final String want; | ||
|
|
||
| public Parameter(Map<String, String> resource, String want) { | ||
| this.resource = resource; | ||
| this.want = want; | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{index}: {0}") | ||
| @MethodSource | ||
| void serviceInstanceId(Parameter parameter) { | ||
| Resource r = | ||
| ServiceInstanceIdResource.getResource( | ||
| DefaultConfigProperties.createForTest( | ||
| ImmutableMap.of( | ||
| ServiceInstanceIdResource.RESOURCE_ATTRIBUTES, | ||
| parameter.resource.entrySet().stream() | ||
| .map(e -> e.getKey() + "=" + e.getValue()) | ||
| .collect(Collectors.joining(","))))); | ||
| assertThat(r.getAttribute(ResourceAttributes.SERVICE_INSTANCE_ID)).matches(parameter.want); | ||
| } | ||
|
|
||
| public static Stream<Arguments> serviceInstanceId() { | ||
| return Stream.of( | ||
| Arguments.of( | ||
| named( | ||
| "service instance id is not set, but pod name and container name are", | ||
| new Parameter( | ||
| ImmutableMap.of( | ||
| ResourceAttributes.K8S_POD_NAME.getKey(), | ||
| "pod-12345", | ||
| ResourceAttributes.K8S_CONTAINER_NAME.getKey(), | ||
| "container-42"), | ||
| "pod-12345/container-42"))), | ||
| Arguments.of( | ||
| named( | ||
| "fall back to random service instance id", | ||
| new Parameter( | ||
| ImmutableMap.of(ResourceAttributes.K8S_POD_NAME.getKey(), "pod-12345"), | ||
| "........-....-....-....-............")))); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec doesn't really say anything about using k8s attributes to construct the serviceId (also k8s attributes are usually added by the collector processors, these might simply not be accessible here)