Skip to content

Commit

Permalink
remove all spring starter features where a standard property can be used
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 13, 2024
1 parent 658c286 commit 15a3ba2
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
library("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
library("org.springframework.boot:spring-boot-starter-webflux:$springBootVersion")

compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
compileOnly("io.opentelemetry:opentelemetry-extension-annotations")
compileOnly("io.opentelemetry:opentelemetry-extension-trace-propagators")
compileOnly("io.opentelemetry.contrib:opentelemetry-aws-xray-propagator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpSpanExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.MapConverter;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringConfigProperties;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand Down Expand Up @@ -57,7 +58,11 @@
* <p>Updates the sampler probability for the configured {@link TracerProvider}.
*/
@Configuration
@EnableConfigurationProperties({SamplerProperties.class, OtlpExporterProperties.class})
@EnableConfigurationProperties({
SamplerProperties.class,
OtlpExporterProperties.class,
OtelResourceProperties.class
})
public class OpenTelemetryAutoConfiguration {

public OpenTelemetryAutoConfiguration() {}
Expand Down Expand Up @@ -96,8 +101,11 @@ static class Metric {}
@Bean
@ConditionalOnMissingBean
ConfigProperties configProperties(
Environment env, OtlpExporterProperties otlpExporterProperties) {
return new SpringConfigProperties(env, new SpelExpressionParser(), otlpExporterProperties);
Environment env,
OtlpExporterProperties otlpExporterProperties,
OtelResourceProperties resourceProperties) {
return new SpringConfigProperties(
env, new SpelExpressionParser(), otlpExporterProperties, resourceProperties);
}

@Bean(destroyMethod = "") // SDK components are shutdown from the OpenTelemetry instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.internal;

import java.util.Arrays;
import javax.annotation.Nullable;
import org.springframework.core.env.Environment;

/**
Expand All @@ -18,28 +17,13 @@ public final class ExporterConfigEvaluator {
private ExporterConfigEvaluator() {}

public static boolean isExporterEnabled(
Environment environment,
@Nullable String oldAllKey,
String oldKey,
String exportersKey,
String wantExporter,
boolean defaultValue) {
Environment environment, String exportersKey, String wantExporter, boolean defaultValue) {

String exporter = environment.getProperty(exportersKey);
if (exporter != null) {
return Arrays.asList(exporter.split(",")).contains(wantExporter);
}

String old = environment.getProperty(oldKey);
if (old != null) {
return "true".equals(old);
}
if (oldAllKey != null) {
String oldAll = environment.getProperty(oldAllKey);
if (oldAll != null) {
return "true".equals(oldAll);
}
}
return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.logging.enabled",
"otel.exporter.logging.metrics.enabled",
"otel.metrics.exporter",
"logging",
false);
context.getEnvironment(), "otel.metrics.exporter", "logging", false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.logging.enabled",
"otel.exporter.logging.traces.enabled",
"otel.traces.exporter",
"logging",
false);
context.getEnvironment(), "otel.traces.exporter", "logging", false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.logging.enabled",
"otel.exporter.logging.logs.enabled",
"otel.logs.exporter",
"logging",
false);
context.getEnvironment(), "otel.logs.exporter", "logging", false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
"otel.exporter.otlp.logs.enabled",
"otel.logs.exporter",
"otlp",
true);
context.getEnvironment(), "otel.logs.exporter", "otlp", true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
"otel.exporter.otlp.metrics.enabled",
"otel.metrics.exporter",
"otlp",
true);
context.getEnvironment(), "otel.metrics.exporter", "otlp", true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
"otel.exporter.otlp.traces.enabled",
"otel.traces.exporter",
"otlp",
true);
context.getEnvironment(), "otel.traces.exporter", "otlp", true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ static final class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
null,
"otel.exporter.zipkin.enabled",
"otel.traces.exporter",
"zipkin",
true);
context.getEnvironment(), "otel.traces.exporter", "zipkin", true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public final class CompositeTextMapPropagatorFactory {
private static final Logger logger =
Logger.getLogger(CompositeTextMapPropagatorFactory.class.getName());

@SuppressWarnings("deprecation") // deprecated class to be updated once published in new location
static TextMapPropagator getCompositeTextMapPropagator(
BeanFactory beanFactory, List<String> types) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** Configures {@link ContextPropagators} bean for propagation. */
@Configuration
@EnableConfigurationProperties(PropagationProperties.class)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnProperty(prefix = "otel.propagation", name = "enabled", matchIfMissing = true)
@SuppressWarnings("deprecation")
public class PropagationAutoConfiguration {

private static final List<String> DEFAULT_PROPAGATORS = Arrays.asList("tracecontext", "baggage");

@Bean
@ConditionalOnMissingBean
ContextPropagators contextPropagators(ObjectProvider<List<TextMapPropagator>> propagators) {
Expand All @@ -43,11 +43,9 @@ static class PropagatorsConfiguration {

@Bean
TextMapPropagator compositeTextMapPropagator(
BeanFactory beanFactory,
PropagationProperties properties,
ConfigProperties configProperties) {
BeanFactory beanFactory, ConfigProperties configProperties) {
return CompositeTextMapPropagatorFactory.getCompositeTextMapPropagator(
beanFactory, configProperties.getList("otel.propagators", properties.getType()));
beanFactory, configProperties.getList("otel.propagators", DEFAULT_PROPAGATORS));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.instrumentation.resources.ProcessRuntimeResource;
import io.opentelemetry.instrumentation.resources.ProcessRuntimeResourceProvider;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -24,18 +25,20 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SuppressWarnings("deprecation") // old otel.springboot.resource support
@Configuration
@EnableConfigurationProperties({OtelSpringResourceProperties.class, OtelResourceProperties.class})
@EnableConfigurationProperties({OtelResourceProperties.class})
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnProperty(prefix = "otel.springboot.resource", name = "enabled", matchIfMissing = true)
public class OtelResourceAutoConfiguration {

@Bean
public ResourceProvider otelResourceProvider(
OtelSpringResourceProperties otelSpringResourceProperties,
OtelResourceProperties otelResourceProperties) {
return new SpringResourceProvider(otelSpringResourceProperties, otelResourceProperties);
public ResourceProvider otelEnvironmentResourceProvider() {
return new EnvironmentResourceProvider();
}

@Bean
public ResourceProvider otelSpringResourceProvider() {
return new SpringResourceProvider();
}

@Bean
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ public class SpringConfigProperties implements ConfigProperties {

private final ExpressionParser parser;
private final OtlpExporterProperties otlpExporterProperties;
private final OtelResourceProperties resourceProperties;

public SpringConfigProperties(
Environment environment,
ExpressionParser parser,
OtlpExporterProperties otlpExporterProperties) {
OtlpExporterProperties otlpExporterProperties,
OtelResourceProperties resourceProperties) {
this.environment = environment;
this.parser = parser;
this.otlpExporterProperties = otlpExporterProperties;
this.resourceProperties = resourceProperties;
}

@Nullable
Expand Down Expand Up @@ -91,6 +94,8 @@ public Duration getDuration(String name) {
public Map<String, String> getMap(String name) {
// maps from config properties are not supported by Environment, so we have to fake it
switch (name) {
case "otel.resource.attributes":
return resourceProperties.getAttributes();
case "otel.exporter.otlp.headers":
return otlpExporterProperties.getHeaders();
case "otel.exporter.otlp.logs.headers":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,15 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ResourceAttributes;

@SuppressWarnings("deprecation") // old otel.springboot.resource support
public class SpringResourceProvider implements ResourceProvider {

private final OtelSpringResourceProperties otelSpringResourceProperties;
private final OtelResourceProperties otelResourceProperties;

public SpringResourceProvider(
OtelSpringResourceProperties otelSpringResourceProperties,
OtelResourceProperties otelResourceProperties) {
this.otelSpringResourceProperties = otelSpringResourceProperties;
this.otelResourceProperties = otelResourceProperties;
}

@Override
public Resource createResource(ConfigProperties configProperties) {
AttributesBuilder attributesBuilder = Attributes.builder();
String springApplicationName = configProperties.getString("spring.application.name");
if (springApplicationName != null) {
attributesBuilder.put(ResourceAttributes.SERVICE_NAME, springApplicationName);
}
otelSpringResourceProperties.getAttributes().forEach(attributesBuilder::put);
otelResourceProperties.getAttributes().forEach(attributesBuilder::put);
String applicationName = configProperties.getString("otel.service.name");
if (applicationName != null) {
attributesBuilder.put(ResourceAttributes.SERVICE_NAME, applicationName);
}
return Resource.create(attributesBuilder.build());
}
}
Loading

0 comments on commit 15a3ba2

Please sign in to comment.