Skip to content

Commit

Permalink
Use ConditionalResourceProvider in the Spring Boot service name provi…
Browse files Browse the repository at this point in the history
…der (open-telemetry#6587)

* Use ConditionalResourceProvider in the Spring Boot service name provider

* make it a bit simpler

* add order=100

* add comment

* spellcheck
  • Loading branch information
Mateusz Rzeszutek authored and LironKS committed Dec 4, 2022
1 parent 890f2cd commit 7557169
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
import java.io.IOException;
Expand Down Expand Up @@ -47,7 +48,7 @@
* </ul>
*/
@AutoService(ResourceProvider.class)
public class SpringBootServiceNameGuesser implements ResourceProvider {
public class SpringBootServiceNameGuesser implements ConditionalResourceProvider {

private static final Logger logger =
Logger.getLogger(SpringBootServiceNameGuesser.class.getName());
Expand Down Expand Up @@ -95,6 +96,23 @@ public Resource createResource(ConfigProperties config) {
.orElseGet(Resource::empty);
}

@Override
public boolean shouldApply(ConfigProperties config, Resource resource) {
// we're skipping this provider if the service name was manually set by the user -- no need to
// waste time trying to compute the service name if it's going to be overridden anyway
String serviceName = config.getString("otel.service.name");
Map<String, String> resourceAttributes = config.getMap("otel.resource.attributes");
return serviceName == null
&& !resourceAttributes.containsKey(ResourceAttributes.SERVICE_NAME.getKey())
&& "unknown_service:java".equals(resource.getAttribute(ResourceAttributes.SERVICE_NAME));
}

@Override
public int order() {
// make it run later than the default set of providers
return 100;
}

@Nullable
private String findByEnvironmentVariable() {
String result = system.getenv("SPRING_APPLICATION_NAME");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import java.io.OutputStream;
Expand Down Expand Up @@ -111,6 +113,35 @@ void getFromCommandlineArgsWithSystemProperty() throws Exception {
expectServiceName(result, "bullpen");
}

@Test
void shouldApply() {
SpringBootServiceNameGuesser guesser = new SpringBootServiceNameGuesser(system);
assertThat(guesser.shouldApply(config, Resource.getDefault())).isTrue();
}

@Test
void shouldNotApplyWhenResourceHasServiceName() {
SpringBootServiceNameGuesser guesser = new SpringBootServiceNameGuesser(system);
Resource resource =
Resource.getDefault().merge(Resource.create(Attributes.of(SERVICE_NAME, "test-service")));
assertThat(guesser.shouldApply(config, resource)).isFalse();
}

@Test
void shouldNotApplyIfConfigHasServiceName() {
SpringBootServiceNameGuesser guesser = new SpringBootServiceNameGuesser(system);
when(config.getString("otel.service.name")).thenReturn("test-service");
assertThat(guesser.shouldApply(config, Resource.getDefault())).isFalse();
}

@Test
void shouldNotApplyIfConfigHasServiceNameResourceAttribute() {
SpringBootServiceNameGuesser guesser = new SpringBootServiceNameGuesser(system);
when(config.getMap("otel.resource.attributes"))
.thenReturn(singletonMap(SERVICE_NAME.getKey(), "test-service"));
assertThat(guesser.shouldApply(config, Resource.getDefault())).isFalse();
}

private static void expectServiceName(Resource result, String expected) {
assertThat(result.getAttribute(SERVICE_NAME)).isEqualTo(expected);
}
Expand Down

0 comments on commit 7557169

Please sign in to comment.