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

fix otel.propagators in spring boot starter #10559

Merged
merged 1 commit into from
Feb 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpMetricExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpSpanExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.MapConverter;
import io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringConfigProperties;
import io.opentelemetry.sdk.OpenTelemetrySdk;
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,
PropagationProperties.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,
PropagationProperties propagationProperties) {
return new SpringConfigProperties(
env, new SpelExpressionParser(), otlpExporterProperties, propagationProperties);
}

@Bean(destroyMethod = "") // SDK components are shutdown from the OpenTelemetry instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import java.util.Arrays;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;

/** Configuration for propagators. */
@ConfigurationProperties(prefix = "otel.propagation")
@Deprecated // use otel.propagators instead
public final class DeprecatedPropagationProperties {

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

public List<String> getType() {
return type;
}

public void setType(List<String> type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/** Configures {@link ContextPropagators} bean for propagation. */
@Configuration
@EnableConfigurationProperties(PropagationProperties.class)
@EnableConfigurationProperties(DeprecatedPropagationProperties.class)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnProperty(prefix = "otel.propagation", name = "enabled", matchIfMissing = true)
@SuppressWarnings("deprecation")
Expand All @@ -44,7 +44,7 @@ static class PropagatorsConfiguration {
@Bean
TextMapPropagator compositeTextMapPropagator(
BeanFactory beanFactory,
PropagationProperties properties,
DeprecatedPropagationProperties properties,
ConfigProperties configProperties) {
return CompositeTextMapPropagatorFactory.getCompositeTextMapPropagator(
beanFactory, configProperties.getList("otel.propagators", properties.getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@

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

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;

/** Configuration for propagators. */
@ConfigurationProperties(prefix = "otel.propagation")
@Deprecated // use otel.propagators instead
@ConfigurationProperties(prefix = "otel")
public final class PropagationProperties {

private List<String> type = Arrays.asList("tracecontext", "baggage");
private List<String> propagators = Collections.emptyList();

public List<String> getType() {
return type;
public List<String> getPropagators() {
return propagators;
}

public void setType(List<String> type) {
this.type = type;
public void setPropagators(List<String> propagators) {
this.propagators = propagators;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpExporterProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import java.time.Duration;
Expand All @@ -22,14 +23,17 @@ public class SpringConfigProperties implements ConfigProperties {

private final ExpressionParser parser;
private final OtlpExporterProperties otlpExporterProperties;
private final PropagationProperties propagationProperties;

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

@Nullable
Expand Down Expand Up @@ -71,6 +75,9 @@ public Double getDouble(String name) {
@SuppressWarnings("unchecked")
@Override
public List<String> getList(String name) {
if (name.equals("otel.propagators")) {
return propagationProperties.getPropagators();
}
List<String> value = environment.getProperty(name, List.class);
return value == null ? Collections.emptyList() : value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.assertj.core.api.Assertions.entry;

import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand Down Expand Up @@ -90,6 +91,7 @@ private static ConfigProperties getConfig(AssertableApplicationContext context)
return new SpringConfigProperties(
context.getBean("environment", Environment.class),
new SpelExpressionParser(),
context.getBean(OtlpExporterProperties.class));
context.getBean(OtlpExporterProperties.class),
new PropagationProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void hasType() {
.withPropertyValues("otel.propagation.type=xray,b3")
.run(
context -> {
PropagationProperties propertiesBean = context.getBean(PropagationProperties.class);
DeprecatedPropagationProperties propertiesBean =
context.getBean(DeprecatedPropagationProperties.class);

assertThat(propertiesBean.getType()).isEqualTo(Arrays.asList("xray", "b3"));
});
Expand All @@ -43,7 +44,7 @@ void hasDefaultTypes() {

this.contextRunner.run(
context ->
assertThat(context.getBean(PropagationProperties.class).getType())
assertThat(context.getBean(DeprecatedPropagationProperties.class).getType())
.containsExactly("tracecontext", "baggage"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.assertj.core.api.Assertions.entry;

import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpExporterProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationProperties;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -29,7 +30,10 @@ void shouldInitializeAttributesByMapInArow() {
Environment env = context.getBean("environment", Environment.class);
SpringConfigProperties config =
new SpringConfigProperties(
env, new SpelExpressionParser(), new OtlpExporterProperties());
env,
new SpelExpressionParser(),
new OtlpExporterProperties(),
new PropagationProperties());

assertThat(config.getMap("otel.springboot.test.map"))
.contains(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
otel:
instrumentation:
logback-appender:
experimental:
capture-code-attributes: true
propagators:
- b3
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
Expand Down Expand Up @@ -54,6 +55,8 @@ class OtelSpringStarterSmokeTest {

@Autowired private TestRestTemplate testRestTemplate;

@Autowired private ConfigProperties configProperties;

@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
Expand All @@ -72,6 +75,14 @@ public LogRecordExporter logRecordExporter() {
}
}

@Test
void propertyConversion() {
assertThat(configProperties.getMap("otel.exporter.otlp.headers"))
.containsEntry("a", "1")
.containsEntry("b", "2");
assertThat(configProperties.getList("otel.propagators")).containsExactly("b3");
}

@Test
void shouldSendTelemetry() throws InterruptedException {

Expand Down
Loading