Skip to content

Commit

Permalink
add Spring boot distro version (#10276)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored Jan 23, 2024
1 parent 35ec443 commit 9c8644c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.resources;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;

public class DistroVersionResourceProvider implements ResourceProvider {

public static final String VERSION =
EmbeddedInstrumentationProperties.findVersion("io.opentelemetry.spring-boot-autoconfigure");

private static final AttributeKey<String> TELEMETRY_DISTRO_NAME =
AttributeKey.stringKey("telemetry.distro.name");
private static final AttributeKey<String> TELEMETRY_DISTRO_VERSION =
AttributeKey.stringKey("telemetry.distro.version");

@Override
public Resource createResource(ConfigProperties config) {
return Resource.create(
Attributes.of(
TELEMETRY_DISTRO_NAME,
"opentelemetry-spring-boot-starter",
TELEMETRY_DISTRO_VERSION,
VERSION));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public ResourceProvider otelResourceProvider(
return new SpringResourceProvider(otelSpringResourceProperties, otelResourceProperties);
}

@Bean
public ResourceProvider otelDistroVersionResourceProvider() {
return new DistroVersionResourceProvider();
}

@Bean
@ConditionalOnClass(OsResource.class)
public ResourceProvider otelOsResourceProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.resources;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class DistroVersionResourceProviderTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OtelResourceAutoConfiguration.class));

@Test
@DisplayName("distro version should be set")
void hasAttributes() {

this.contextRunner.run(
context -> {
ResourceProvider resource =
context.getBean("otelDistroVersionResourceProvider", ResourceProvider.class);

assertThat(
resource
.createResource(DefaultConfigProperties.createFromMap(ImmutableMap.of()))
.getAttributes()
.asMap())
.containsEntry(
AttributeKey.stringKey("telemetry.distro.name"),
"opentelemetry-spring-boot-starter")
.anySatisfy(
(key, val) -> {
assertThat(key.getKey()).isEqualTo("telemetry.distro.version");
assertThat(val).asString().isNotBlank();
});
});
}
}

0 comments on commit 9c8644c

Please sign in to comment.