Skip to content

Commit 1e38937

Browse files
Added batchspanprocessor support; fixes gh-51
1 parent f7ca82e commit 1e38937

File tree

4 files changed

+187
-3
lines changed

4 files changed

+187
-3
lines changed

docs/src/main/asciidoc/_configprops.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
|spring.sleuth.otel.log.exporter.enabled | `false` | Enable log exporter for OTel.
1919
|spring.sleuth.otel.log.slf4j.enabled | `false` | Enable slf4j support for OTel.
2020
|spring.sleuth.otel.multiple-tracer-configs-detector-enabled | `true` | Enable detection of autoconfiguration for multiple tracers (i.e.: Brave and OTel); only one of them should be used.
21+
|spring.sleuth.otel.processor.batch.exporter-timeout | | Exporter timeout in millis.
22+
|spring.sleuth.otel.processor.batch.max-export-batch-size | | Max export batch size.
23+
|spring.sleuth.otel.processor.batch.max-queue-size | | Max queue size.
24+
|spring.sleuth.otel.processor.batch.schedule-delay | | Schedule delay in millis.
2125
|spring.sleuth.otel.propagation.composite-text-map-propagator.enabled | `true` | Enable a composite text map propagator that can combine multiple propagation types into a single text map propagator.
2226
|spring.sleuth.otel.propagation.sleuth-baggage.enabled | `true` | Enable propagating baggage in a Sleuth compatible way (baggage key & value pair means e.g. a key & value HTTP pair).
2327
|spring.sleuth.otel.resource.enabled | `true` | Enables default {@link Resource} implementations.

spring-cloud-sleuth-otel-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/otel/OtelAutoConfiguration.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.concurrent.TimeUnit;
2122
import java.util.function.Supplier;
2223
import java.util.stream.Collectors;
2324

@@ -32,6 +33,8 @@
3233
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
3334
import io.opentelemetry.sdk.trace.SpanLimits;
3435
import io.opentelemetry.sdk.trace.SpanProcessor;
36+
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
37+
import io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder;
3538
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
3639
import io.opentelemetry.sdk.trace.export.SpanExporter;
3740
import io.opentelemetry.sdk.trace.samplers.Sampler;
@@ -41,6 +44,7 @@
4144
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
4245
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4346
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
47+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
4448
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4549
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4650
import org.springframework.cloud.sleuth.autoconfig.SleuthAnnotationConfiguration;
@@ -70,7 +74,7 @@
7074
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
7175
@ConditionalOnMissingBean(org.springframework.cloud.sleuth.Tracer.class)
7276
@EnableConfigurationProperties({ OtelProperties.class, SleuthSpanFilterProperties.class, SleuthBaggageProperties.class,
73-
SleuthTracerProperties.class })
77+
SleuthTracerProperties.class, OtelProcessorProperties.class })
7478
@Import({ OtelBridgeConfiguration.class, OtelPropagationConfiguration.class, TraceConfiguration.class,
7579
SleuthAnnotationConfiguration.class, OtelResourceConfiguration.class })
7680
@AutoConfigureBefore(BraveAutoConfiguration.class)
@@ -86,17 +90,56 @@ OpenTelemetry otel(SdkTracerProvider tracerProvider, ContextPropagators contextP
8690
@ConditionalOnMissingBean
8791
SdkTracerProvider otelTracerProvider(SpanLimits spanLimits, ObjectProvider<List<SpanProcessor>> spanProcessors,
8892
SpanExporterCustomizer spanExporterCustomizer, ObjectProvider<List<SpanExporter>> spanExporters,
89-
Sampler sampler, Resource resource) {
93+
Sampler sampler, Resource resource, SpanProcessorProvider spanProcessorProvider) {
9094
SdkTracerProviderBuilder sdkTracerProviderBuilder = SdkTracerProvider.builder().setResource(resource)
9195
.setSampler(sampler).setSpanLimits(spanLimits);
9296
List<SpanProcessor> processors = spanProcessors.getIfAvailable(ArrayList::new);
9397
processors.addAll(spanExporters.getIfAvailable(ArrayList::new).stream()
94-
.map(e -> SimpleSpanProcessor.create(spanExporterCustomizer.customize(e)))
98+
.map(e -> spanProcessorProvider.toSpanProcessor(spanExporterCustomizer.customize(e)))
9599
.collect(Collectors.toList()));
96100
processors.forEach(sdkTracerProviderBuilder::addSpanProcessor);
97101
return sdkTracerProviderBuilder.build();
98102
}
99103

104+
@Bean
105+
@ConditionalOnClass(name = "io.opentelemetry.api.metrics.GlobalMeterProvider")
106+
@ConditionalOnMissingBean
107+
SpanProcessorProvider otelBatchSpanProcessorProvider(OtelProcessorProperties otelProcessorProperties) {
108+
return new SpanProcessorProvider() {
109+
@Override
110+
public SpanProcessor toSpanProcessor(SpanExporter spanExporter) {
111+
BatchSpanProcessorBuilder builder = BatchSpanProcessor.builder(spanExporter);
112+
setBuilderProperties(otelProcessorProperties, builder);
113+
return builder.build();
114+
}
115+
116+
private void setBuilderProperties(OtelProcessorProperties otelProcessorProperties,
117+
BatchSpanProcessorBuilder builder) {
118+
if (otelProcessorProperties.getBatch().getExporterTimeout() != null) {
119+
builder.setExporterTimeout(otelProcessorProperties.getBatch().getExporterTimeout(),
120+
TimeUnit.MILLISECONDS);
121+
}
122+
if (otelProcessorProperties.getBatch().getMaxExportBatchSize() != null) {
123+
builder.setMaxExportBatchSize(otelProcessorProperties.getBatch().getMaxExportBatchSize());
124+
}
125+
if (otelProcessorProperties.getBatch().getMaxQueueSize() != null) {
126+
builder.setMaxQueueSize(otelProcessorProperties.getBatch().getMaxQueueSize());
127+
}
128+
if (otelProcessorProperties.getBatch().getScheduleDelay() != null) {
129+
builder.setScheduleDelay(otelProcessorProperties.getBatch().getScheduleDelay(),
130+
TimeUnit.MILLISECONDS);
131+
}
132+
}
133+
};
134+
}
135+
136+
@Bean
137+
@ConditionalOnMissingClass("io.opentelemetry.api.metrics.GlobalMeterProvider")
138+
@ConditionalOnMissingBean
139+
SpanProcessorProvider otelSimpleSpanProcessorProvider() {
140+
return SimpleSpanProcessor::create;
141+
}
142+
100143
@Bean
101144
@ConditionalOnMissingBean
102145
Resource otelResource(Environment env, ObjectProvider<List<Supplier<Resource>>> resourceProviders) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.autoconfig.otel;
18+
19+
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
20+
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
23+
/**
24+
* OpenTelemetry span process properties.
25+
*
26+
* @author Marcin Grzejszczak
27+
* @since 1.0.0
28+
*/
29+
@ConfigurationProperties("spring.sleuth.otel.processor")
30+
public class OtelProcessorProperties {
31+
32+
private Batch batch = new Batch();
33+
34+
public Batch getBatch() {
35+
return this.batch;
36+
}
37+
38+
public void setBatch(Batch batch) {
39+
this.batch = batch;
40+
}
41+
42+
/**
43+
* Configuration of the {@link BatchSpanProcessor}.
44+
*/
45+
public static class Batch {
46+
47+
/**
48+
* Schedule delay in millis.
49+
*/
50+
private Long scheduleDelay;
51+
52+
/**
53+
* Max queue size.
54+
*/
55+
private Integer maxQueueSize;
56+
57+
/**
58+
* Max export batch size.
59+
*/
60+
private Integer maxExportBatchSize;
61+
62+
/**
63+
* Exporter timeout in millis.
64+
*/
65+
private Long exporterTimeout;
66+
67+
public Long getScheduleDelay() {
68+
return scheduleDelay;
69+
}
70+
71+
public void setScheduleDelay(long scheduleDelay) {
72+
this.scheduleDelay = scheduleDelay;
73+
}
74+
75+
public Integer getMaxQueueSize() {
76+
return maxQueueSize;
77+
}
78+
79+
public void setMaxQueueSize(int maxQueueSize) {
80+
this.maxQueueSize = maxQueueSize;
81+
}
82+
83+
public Integer getMaxExportBatchSize() {
84+
return maxExportBatchSize;
85+
}
86+
87+
public void setMaxExportBatchSize(int maxExportBatchSize) {
88+
this.maxExportBatchSize = maxExportBatchSize;
89+
}
90+
91+
public Long getExporterTimeout() {
92+
return exporterTimeout;
93+
}
94+
95+
public void setExporterTimeout(long exporterTimeout) {
96+
this.exporterTimeout = exporterTimeout;
97+
}
98+
99+
}
100+
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.autoconfig.otel;
18+
19+
import io.opentelemetry.sdk.trace.SpanProcessor;
20+
import io.opentelemetry.sdk.trace.export.SpanExporter;
21+
22+
/**
23+
* Converts {@link SpanExporter} to {@link SpanProcessor}.
24+
*
25+
* @author Marcin Grzejszczak
26+
* @since 1.0.0
27+
*/
28+
public interface SpanProcessorProvider {
29+
30+
/**
31+
* @param spanExporter span exporter
32+
* @return converted span processor
33+
*/
34+
SpanProcessor toSpanProcessor(SpanExporter spanExporter);
35+
36+
}

0 commit comments

Comments
 (0)