Skip to content

Commit

Permalink
Implement Configuration for BatchSpanProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
thisthat committed Apr 14, 2020
1 parent 877de57 commit 957ecc2
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 121 deletions.
52 changes: 0 additions & 52 deletions sdk/src/main/java/io/opentelemetry/sdk/common/ConfigBuilder.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@

package io.opentelemetry.sdk.trace.export;

import com.google.auto.value.AutoValue;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.MoreExecutors;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.metrics.LongCounter;
import io.opentelemetry.metrics.LongCounter.BoundLongCounter;
import io.opentelemetry.metrics.Meter;
import io.opentelemetry.sdk.common.ConfigBuilder;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
Expand All @@ -39,6 +41,7 @@
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -106,11 +109,11 @@ public static BatchSpansProcessor create(SpanExporter spanExporter) {
public static BatchSpansProcessor create(SpanExporter spanExporter, Config config) {
return new BatchSpansProcessor(
spanExporter,
config.sampled,
config.scheduleDelayMillis,
config.maxQueueSize,
config.maxExportBatchSize,
config.exporterTimeoutMillis);
config.isReportOnlySampled(),
config.getScheduleDelayMillis(),
config.getMaxQueueSize(),
config.getMaxExportBatchSize(),
config.getExporterTimeoutMillis());
}

@Override
Expand Down Expand Up @@ -323,101 +326,165 @@ public void run() {
}

@Immutable
public static final class Config {
private final long scheduleDelayMillis;
private final int maxQueueSize;
private final int maxExportBatchSize;
private final int exporterTimeoutMillis;
private final boolean sampled;
@AutoValue
public abstract static class Config {

private Config(
boolean sampled,
long scheduleDelayMillis,
int maxQueueSize,
int maxExportBatchSize,
int exporterTimeoutMillis) {
this.sampled = sampled;
this.scheduleDelayMillis = scheduleDelayMillis;
this.maxQueueSize = maxQueueSize;
this.maxExportBatchSize = maxExportBatchSize;
this.exporterTimeoutMillis = exporterTimeoutMillis;
}
public abstract boolean isReportOnlySampled();

public long getScheduleDelayMillis() {
return scheduleDelayMillis;
}

public int getMaxQueueSize() {
return maxQueueSize;
}
public abstract long getScheduleDelayMillis();

public int getMaxExportBatchSize() {
return maxExportBatchSize;
}
public abstract int getMaxQueueSize();

public int getExporterTimeoutMillis() {
return exporterTimeoutMillis;
}
public abstract int getMaxExportBatchSize();

public boolean isReportOnlySampled() {
return sampled;
}
public abstract int getExporterTimeoutMillis();

public static Config getDefault() {
return new Builder().build();
}

/**
* Creates a {@link Config} object reading the configuration values first from the environment
* and then from system properties. If a configuration value is missing, it uses the default
* value.
*
* @return The {@link Config} object.
*/
public static Config loadFromDefaultSources() {
return new Builder().readEnv().readSystemProperties().build();
}

public static Builder newBuilder() {
return new Builder();
}

public static class Builder extends ConfigBuilder {
public static final class Builder {

private static final String KEY_SCHEDULE_DELAY_MILLIS = "otel.bsp.schedule.delay";
private static final String KEY_MAX_QUEUE_SIZE = "otel.bsp.max.queue";
private static final String KEY_MAX_EXPORT_BATCH_SIZE = "otel.bsp.max.export.batch";
private static final String KEY_EXPORT_TIMEOUT_MILLIS = "otel.bsp.export.timeout";
private static final String KEY_SAMPLED = "otel.bsp.report.sampled";

private static final long DEFAULT_SCHEDULE_DELAY_MILLIS = 5000;
private static final int DEFAULT_MAX_QUEUE_SIZE = 2048;
private static final int DEFAULT_MAX_EXPORT_BATCH_SIZE = 512;
private static final int DEFAULT_EXPORT_TIMEOUT_MILLIS = 30_000;
private static final boolean DEFAULT_REPORT_ONLY_SAMPLED = true;

private long scheduleDelayMillis = DEFAULT_SCHEDULE_DELAY_MILLIS;
private int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
private int maxExportBatchSize = DEFAULT_MAX_EXPORT_BATCH_SIZE;
private int exporterTimeoutMillis = DEFAULT_EXPORT_TIMEOUT_MILLIS;
private boolean sampled = DEFAULT_REPORT_ONLY_SAMPLED;

enum NamingConvention {
DOT {
@Override
protected char getSeparator() {
return '.';
}

@Nonnull
@Override
public String normalize(@Nonnull String key) {
return key.toLowerCase();
}
},
ENV_VAR {
@Override
protected char getSeparator() {
return '_';
}
};

private static final String KEY_SCHEDULE_DELAY_MILLIS = "OTEL_BSP_SCHEDULE_DELAY";
private static final String KEY_MAX_QUEUE_SIZE = "OTEL_BSP_MAX_QUEUE";
private static final String KEY_MAX_EXPORT_BATCH_SIZE = "OTEL_BSP_MAX_EXPORT_BATCH";
private static final String KEY_EXPORT_TIMEOUT_MILLIS = "OTEL_BSP_EXPORT_TIMEOUT";
private static final String KEY_SAMPLED = "OTEL_BSP_REPORT_SAMPLED";
@Nonnull
public String normalize(@Nonnull String key) {
return key.toLowerCase().replace(getSeparator(), DOT.getSeparator());
}

private static final long SCHEDULE_DELAY_MILLIS = 5000;
private static final int MAX_QUEUE_SIZE = 2048;
private static final int MAX_EXPORT_BATCH_SIZE = 512;
private static final int EXPORT_TIMEOUT_MILLIS = 30_000;
private static final boolean REPORT_ONLY_SAMPLED = true;
public Map<String, String> normalize(@Nonnull Map<String, String> map) {
Map<String, String> properties = new HashMap<>();
for (String key : map.keySet()) {
properties.put(normalize(key), map.get(key));
}
return Collections.unmodifiableMap(properties);
}

private long scheduleDelayMillis = SCHEDULE_DELAY_MILLIS;
private int maxQueueSize = MAX_QUEUE_SIZE;
private int maxExportBatchSize = MAX_EXPORT_BATCH_SIZE;
private int exporterTimeoutMillis = EXPORT_TIMEOUT_MILLIS;
private boolean sampled = REPORT_ONLY_SAMPLED;
protected abstract char getSeparator();
}

/**
* Sets the configuration values from the given configuration map.
* Sets the configuration values from the given configuration map. This method looks for the
* following keys:
*
* <ul>
* <li>{@code otel.bsp.schedule.delay}: to set the delay interval between two consecutive
* exports.
* <li>{@code otel.bsp.max.queue}: to set the maximum queue size.
* <li>{@code otel.bsp.max.export.batch}: to set the maximum batch size.
* <li>{@code otel.bsp.export.timeout}: to set the maximum allowed time to export data.
* <li>{@code otel.bsp.report.sampled}: to set whether only sampled spans should be
* reported.
* </ul>
*
* @param configMap {@link Map} holding the configuration values.
* @return this.
*/
@Override
public Builder fromConfigMap(Map<String, String> configMap) {
public Builder fromConfigMap(
Map<String, String> configMap, NamingConvention namingConvention) {
configMap = namingConvention.normalize(configMap);
this.setScheduleDelayMillis(
getProperty(KEY_SCHEDULE_DELAY_MILLIS, configMap, SCHEDULE_DELAY_MILLIS));
this.setMaxQueueSize(getProperty(KEY_MAX_QUEUE_SIZE, configMap, MAX_QUEUE_SIZE));
getProperty(KEY_SCHEDULE_DELAY_MILLIS, configMap, this.scheduleDelayMillis));
this.setMaxQueueSize(
getProperty(
namingConvention.normalize(KEY_MAX_QUEUE_SIZE), configMap, this.maxQueueSize));
this.setMaxExportBatchSize(
getProperty(KEY_MAX_EXPORT_BATCH_SIZE, configMap, MAX_EXPORT_BATCH_SIZE));
getProperty(KEY_MAX_EXPORT_BATCH_SIZE, configMap, this.maxExportBatchSize));
this.setExporterTimeoutMillis(
getProperty(KEY_EXPORT_TIMEOUT_MILLIS, configMap, EXPORT_TIMEOUT_MILLIS));
this.reportOnlySampled(getProperty(KEY_SAMPLED, configMap, REPORT_ONLY_SAMPLED));
getProperty(KEY_EXPORT_TIMEOUT_MILLIS, configMap, this.exporterTimeoutMillis));
this.reportOnlySampled(getProperty(KEY_SAMPLED, configMap, this.sampled));
return this;
}

/**
* Sets the configuration values from environment variables.
* Sets the configuration values from environment variables. This method looks for the
* following keys:
*
* <ul>
* <li>{@code OTEL_BSP_SCHEDULE_DELAY}: to set the delay interval between two consecutive
* exports.
* <li>{@code OTEL_BSP_MAX_QUEUE}: to set the maximum queue size.
* <li>{@code OTEL_BSP_MAX_EXPORT_BATCH}: to set the maximum batch size.
* <li>{@code OTEL_BSP_EXPORT_TIMEOUT}: to set the maximum allowed time to export data.
* <li>{@code OTEL_BSP_REPORT_SAMPLED}: to set whether only sampled spans should be
* reported.
* </ul>
*
* @return this.
*/
public Builder readEnv() {
return fromConfigMap(System.getenv(), NamingConvention.ENV_VAR);
}

/**
* Sets the configuration values from system properties. This method looks for the
* following keys:
*
* <ul>
* <li>{@code otel.bsp.schedule.delay}: to set the delay interval between two consecutive
* exports.
* <li>{@code otel.bsp.max.queue}: to set the maximum queue size.
* <li>{@code otel.bsp.max.export.batch}: to set the maximum batch size.
* <li>{@code otel.bsp.export.timeout}: to set the maximum allowed time to export data.
* <li>{@code otel.bsp.report.sampled}: to set whether only sampled spans should be
* reported.
* </ul>
*
* @return this.
*/
@Override
public Builder fromEnv() {
return fromConfigMap(System.getenv());
public Builder readSystemProperties() {
return fromConfigMap(Maps.fromProperties(System.getProperties()), NamingConvention.DOT);
}

/**
Expand Down Expand Up @@ -495,8 +562,37 @@ public Builder setMaxExportBatchSize(int maxExportBatchSize) {
return this;
}

private static boolean getProperty(
String name, Map<String, String> map, boolean defaultValue) {
if (map.containsKey(name)) {
return Boolean.parseBoolean(map.get(name));
}
return defaultValue;
}

private static int getProperty(String name, Map<String, String> map, int defaultValue) {
try {
return Integer.parseInt(map.get(name));
} catch (NumberFormatException ex) {
return defaultValue;
}
}

private static long getProperty(String name, Map<String, String> map, long defaultValue) {
try {
return Long.parseLong(map.get(name));
} catch (NumberFormatException ex) {
return defaultValue;
}
}

/**
* Builds the {@link Config} object.
*
* @return the {@link Config} object.
*/
public Config build() {
return new Config(
return new AutoValue_BatchSpansProcessor_Config(
sampled, scheduleDelayMillis, maxQueueSize, maxExportBatchSize, exporterTimeoutMillis);
}
}
Expand Down
Loading

0 comments on commit 957ecc2

Please sign in to comment.