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

[3.x] - Simple to Batch Span exporter #7419

Merged
merged 7 commits into from
Aug 29, 2023
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 @@ -47,6 +47,8 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
Expand Down Expand Up @@ -99,7 +101,7 @@
* <td>Path to be used.</td>
* </tr>
* <tr>
* <td>{@code exporter-timeout-millis}</td>
* <td>{@code exporter-timeout}</td>
* <td>10 seconds</td>
* <td>Timeout of exporter</td>
* </tr>
Expand Down Expand Up @@ -154,6 +156,9 @@ public class JaegerTracerBuilder implements TracerBuilder<JaegerTracerBuilder> {
static final String DEFAULT_HTTP_HOST = "localhost";
static final int DEFAULT_HTTP_PORT = 14250;

static final long DEFAULT_SCHEDULE_DELAY = 30_000;
static final int DEFAULT_MAX_QUEUE_SIZE = 2048;
static final int DEFAULT_MAX_EXPORT_BATCH_SIZE = 512;
private final Map<String, String> tags = new HashMap<>();
// this is a backward incompatible change, but the correct choice is Jaeger, not B3
private final Set<PropagationFormat> propagationFormats = EnumSet.noneOf(PropagationFormat.class);
Expand All @@ -165,12 +170,17 @@ public class JaegerTracerBuilder implements TracerBuilder<JaegerTracerBuilder> {
private Number samplerParam = 1;
private boolean enabled = DEFAULT_ENABLED;
private boolean global = true;
private Duration exporterTimeout = Duration.ofSeconds(10);
private byte[] privateKey;
private byte[] certificate;
private byte[] trustedCertificates;
private String path;
private Config config;
private Duration exporterTimeout = Duration.ofSeconds(10);
private Duration scheduleDelay = Duration.ofMillis(DEFAULT_SCHEDULE_DELAY);
private int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
private int maxExportBatchSize = DEFAULT_MAX_EXPORT_BATCH_SIZE;

private SpanProcessorType spanProcessorType = SpanProcessorType.BATCH;

/**
* Default constructor, does not modify any state.
Expand Down Expand Up @@ -264,7 +274,6 @@ public JaegerTracerBuilder config(Config config) {
config.get("path").asString().ifPresent(this::collectorPath);
config.get("sampler-type").asString().as(SamplerType::create).ifPresent(this::samplerType);
config.get("sampler-param").asDouble().ifPresent(this::samplerParam);
config.get("exporter-timeout-millis").asLong().ifPresent(it -> exporterTimeout(Duration.ofMillis(it)));
config.get("private-key-pem").as(io.helidon.common.configurable.Resource::create).ifPresent(this::privateKey);
config.get("client-cert-pem").as(io.helidon.common.configurable.Resource::create).ifPresent(this::clientCertificate);
config.get("trusted-cert-pem").as(io.helidon.common.configurable.Resource::create).ifPresent(this::trustedCertificates);
Expand Down Expand Up @@ -299,6 +308,13 @@ public JaegerTracerBuilder config(Config config) {

config.get("global").asBoolean().ifPresent(this::registerGlobal);

config.get("span-processor-type").asString()
.ifPresent(it -> spanProcessorType(SpanProcessorType.valueOf(it.toUpperCase())));
config.get("exporter-timeout").as(Duration.class).ifPresent(this::exporterTimeout);
config.get("schedule-delay").as(Duration.class).ifPresent(this::scheduleDelay);
config.get("max-queue-size").asInt().ifPresent(this::maxQueueSize);
config.get("max-export-batch-size").asInt().ifPresent(this::maxExportBatchSize);

return this;
}

Expand Down Expand Up @@ -338,18 +354,67 @@ public JaegerTracerBuilder trustedCertificates(io.helidon.common.configurable.Re
return this;
}

/**
* Span Processor type used.
*
* @param spanProcessorType to use
* @return updated builder
*/
@ConfiguredOption("batch")
public JaegerTracerBuilder spanProcessorType(SpanProcessorType spanProcessorType) {
this.spanProcessorType = spanProcessorType;
return this;
}


/**
* Timeout of exporter requests.
*
* @param exporterTimeout timeout to use
* @return updated builder
*/
@ConfiguredOption(key = "exporter-timeout-millis", value = "10000")
@ConfiguredOption("PT10S")
public JaegerTracerBuilder exporterTimeout(Duration exporterTimeout) {
this.exporterTimeout = exporterTimeout;
return this;
}

/**
* Schedule Delay of exporter requests.
*
* @param scheduleDelay timeout to use
* @return updated builder
*/
@ConfiguredOption("PT5S")
public JaegerTracerBuilder scheduleDelay(Duration scheduleDelay) {
this.scheduleDelay = scheduleDelay;
return this;
}

/**
* Maximum Queue Size of exporter requests.
*
* @param maxQueueSize to use
* @return updated builder
*/
@ConfiguredOption("2048")
public JaegerTracerBuilder maxQueueSize(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
return this;
}

/**
* Maximum Export Batch Size of exporter requests.
*
* @param maxExportBatchSize to use
* @return updated builder
*/
@ConfiguredOption("512")
public JaegerTracerBuilder maxExportBatchSize(int maxExportBatchSize) {
this.maxExportBatchSize = maxExportBatchSize;
return this;
}

@Override
public JaegerTracerBuilder enabled(boolean enabled) {
this.enabled = enabled;
Expand Down Expand Up @@ -462,7 +527,7 @@ public Tracer build() {
Resource serviceName = Resource.create(attributesBuilder.build());
OpenTelemetry ot = OpenTelemetrySdk.builder()
.setTracerProvider(SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(exporter))
.addSpanProcessor(spanProcessor(exporter))
.setSampler(sampler)
.setResource(serviceName)
.build())
Expand Down Expand Up @@ -525,6 +590,26 @@ boolean isEnabled() {
return enabled;
}

SpanProcessorType spanProcessorType() {
return spanProcessorType;
}

Duration exporterTimeout() {
return exporterTimeout;
}

Duration scheduleDelay() {
return scheduleDelay;
}

int maxQueueSize() {
return maxQueueSize;
}

int maxExportBatchSize() {
return maxExportBatchSize;
}

List<TextMapPropagator> createPropagators() {
if (propagationFormats.isEmpty()) {
// for backward compatibility, we add B3 if nothing is defined
Expand All @@ -537,6 +622,18 @@ List<TextMapPropagator> createPropagators() {
.toList();
}

private SpanProcessor spanProcessor(SpanExporter exporter) {
return switch (spanProcessorType) {
case BATCH -> BatchSpanProcessor.builder(exporter)
.setScheduleDelay(scheduleDelay)
.setMaxQueueSize(maxQueueSize)
.setMaxExportBatchSize(maxExportBatchSize)
.setExporterTimeout(exporterTimeout)
.build();
case SIMPLE -> SimpleSpanProcessor.create(exporter);
};
}

private static TextMapPropagator mapFormatToPropagator(PropagationFormat propagationFormat) {
return switch (propagationFormat) {
case B3 -> B3Propagator.injectingMultiHeaders();
Expand Down Expand Up @@ -602,4 +699,19 @@ public enum PropagationFormat {
*/
W3C
}


/**
* Span Processor type. Batch is default for production.
*/
public enum SpanProcessorType {
/**
* Simple Span Processor.
*/
SIMPLE,
/**
* Batch Span Processor.
*/
BATCH
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.tracing.jaeger;

import java.time.Duration;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -73,7 +74,12 @@ void testConfigDefaults() {
assertThat("Path", jBuilder.path(), nullValue());
assertThat("Enabled", jBuilder.isEnabled(), is(true));
assertThat("Sampler type", jBuilder.samplerType(), is(JaegerTracerBuilder.SamplerType.CONSTANT));
assertThat("Span Processor type", jBuilder.spanProcessorType(), is(JaegerTracerBuilder.SpanProcessorType.BATCH));
assertThat("Sampler param", jBuilder.samplerParam(), is(Integer.valueOf(1)));
assertThat("Exporter timeout", jBuilder.exporterTimeout(), is(Duration.ofSeconds(10)));
assertThat("Schedule delay", jBuilder.scheduleDelay(), is(Duration.ofSeconds(30)));
assertThat("Max Queue Size", jBuilder.maxQueueSize(), is(2048));
assertThat("Max Export Batch Size", jBuilder.maxExportBatchSize(), is(512));
}

@Test
Expand Down Expand Up @@ -102,6 +108,7 @@ void testFullHttp() {
assertThat("Port", jBuilder.port(), is(14240));
assertThat("Path", jBuilder.path(), is("/api/traces/mine"));
assertThat("Sampler type", jBuilder.samplerType(), is(JaegerTracerBuilder.SamplerType.RATIO));
assertThat("Span Processor type", jBuilder.spanProcessorType(), is(JaegerTracerBuilder.SpanProcessorType.SIMPLE));
assertThat("Sampler param", jBuilder.samplerParam(), is(0.5));
assertThat("Tags", jBuilder.tags(), is(Map.of(
"tag1", "tag1-value",
Expand Down
1 change: 1 addition & 0 deletions tracing/jaeger/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tracing:
sampler-type: "ratio"
sampler-param: 0.5
propagation: ["jaeger", "b3_single", "w3c"]
span-processor-type: simple
tags:
tag1: "tag1-value" # JAEGER_TAGS
tag2: "tag2-value" # JAEGER_TAGS
Expand Down