Skip to content

Commit

Permalink
Add autoconfigure console alias for logging exporter (#6027)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Mar 12, 2024
1 parent 97609a9 commit 7655192
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;

/**
* {@link LogRecordExporter} SPI implementation for {@link SystemOutLogRecordExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleLogRecordExporterProvider
implements ConfigurableLogRecordExporterProvider {
@Override
public LogRecordExporter createExporter(ConfigProperties config) {
return SystemOutLogRecordExporter.create();
}

@Override
public String getName() {
return "console";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;

/**
* {@link MetricExporter} SPI implementation for {@link LoggingMetricExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleMetricExporterProvider implements ConfigurableMetricExporterProvider {
@Override
public MetricExporter createExporter(ConfigProperties config) {
return LoggingMetricExporter.create();
}

@Override
public String getName() {
return "console";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import io.opentelemetry.sdk.trace.export.SpanExporter;

/**
* {@link SpanExporter} SPI implementation for {@link LoggingSpanExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleSpanExporterProvider implements ConfigurableSpanExporterProvider {
@Override
public SpanExporter createExporter(ConfigProperties config) {
return LoggingSpanExporter.create();
}

@Override
public String getName() {
return "console";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*
* @deprecated The name {@code logging} is a deprecated alias for {@code console}, which is provided
* via {@link ConsoleLogRecordExporterProvider}.
*/
public class LoggingLogRecordExporterProvider implements ConfigurableLogRecordExporterProvider {
@Deprecated
public final class LoggingLogRecordExporterProvider
implements ConfigurableLogRecordExporterProvider {
@Override
public LogRecordExporter createExporter(ConfigProperties config) {
return SystemOutLogRecordExporter.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*
* @deprecated The name {@code logging} is a deprecated alias for {@code console}, which is provided
* via {@link ConsoleMetricExporterProvider}.
*/
public class LoggingMetricExporterProvider implements ConfigurableMetricExporterProvider {
@Deprecated
public final class LoggingMetricExporterProvider implements ConfigurableMetricExporterProvider {
@Override
public MetricExporter createExporter(ConfigProperties config) {
return LoggingMetricExporter.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*
* @deprecated The name {@code logging} is a deprecated alias for {@code console}, which is provided
* via {@link ConsoleSpanExporterProvider}.
*/
public class LoggingSpanExporterProvider implements ConfigurableSpanExporterProvider {
@Deprecated
public final class LoggingSpanExporterProvider implements ConfigurableSpanExporterProvider {
@Override
public SpanExporter createExporter(ConfigProperties config) {
return LoggingSpanExporter.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.opentelemetry.exporter.logging.internal.LoggingLogRecordExporterProvider
io.opentelemetry.exporter.logging.internal.ConsoleLogRecordExporterProvider
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.opentelemetry.exporter.logging.internal.LoggingMetricExporterProvider
io.opentelemetry.exporter.logging.internal.ConsoleMetricExporterProvider
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.opentelemetry.exporter.logging.internal.LoggingSpanExporterProvider
io.opentelemetry.exporter.logging.internal.ConsoleSpanExporterProvider
11 changes: 8 additions & 3 deletions sdk-extensions/autoconfigure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,14 @@ The logging exporter prints the name of the span along with its attributes to st

| System property | Environment variable | Description |
|-------------------------------|-------------------------------|----------------------------------------------------------------------|
| otel.traces.exporter=logging | OTEL_TRACES_EXPORTER=logging | Select the logging exporter for tracing |
| otel.metrics.exporter=logging | OTEL_METRICS_EXPORTER=logging | Select the logging exporter for metrics |
| otel.logs.exporter=logging | OTEL_LOGS_EXPORTER=logging | Select the logging exporter for logs |
| otel.traces.exporter=console | OTEL_TRACES_EXPORTER=console | Select the logging exporter for tracing |
| otel.metrics.exporter=console | OTEL_METRICS_EXPORTER=console | Select the logging exporter for metrics |
| otel.logs.exporter=console | OTEL_LOGS_EXPORTER=console | Select the logging exporter for logs |

The logging exporter is also set when `otel.traces.exporter`, `otel.metrics.exporter`,
or `otel.logs.exporter` is set to `logging`. `logging` is a deprecated alias for `console`, the
preferred value
as [defined in the specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#exporter-selection).

#### Logging OTLP JSON exporter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class LogRecordExporterConfiguration {

static {
EXPORTER_ARTIFACT_ID_BY_NAME = new HashMap<>();
EXPORTER_ARTIFACT_ID_BY_NAME.put("console", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging-otlp", "opentelemetry-exporter-logging-otlp");
EXPORTER_ARTIFACT_ID_BY_NAME.put("otlp", "opentelemetry-exporter-otlp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import java.io.Closeable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

final class LoggerProviderConfiguration {

private static final List<String> simpleProcessorExporterNames =
Arrays.asList("console", "logging");

static void configureLoggerProvider(
SdkLoggerProviderBuilder loggerProviderBuilder,
ConfigProperties config,
Expand Down Expand Up @@ -64,11 +68,13 @@ static List<LogRecordProcessor> configureLogRecordProcessors(
Map<String, LogRecordExporter> exportersByNameCopy = new HashMap<>(exportersByName);
List<LogRecordProcessor> logRecordProcessors = new ArrayList<>();

LogRecordExporter exporter = exportersByNameCopy.remove("logging");
if (exporter != null) {
LogRecordProcessor logRecordProcessor = SimpleLogRecordProcessor.create(exporter);
closeables.add(logRecordProcessor);
logRecordProcessors.add(logRecordProcessor);
for (String simpleProcessorExporterName : simpleProcessorExporterNames) {
LogRecordExporter exporter = exportersByNameCopy.remove(simpleProcessorExporterName);
if (exporter != null) {
LogRecordProcessor logRecordProcessor = SimpleLogRecordProcessor.create(exporter);
closeables.add(logRecordProcessor);
logRecordProcessors.add(logRecordProcessor);
}
}

if (!exportersByNameCopy.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class MetricExporterConfiguration {

static {
EXPORTER_ARTIFACT_ID_BY_NAME = new HashMap<>();
EXPORTER_ARTIFACT_ID_BY_NAME.put("console", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging-otlp", "opentelemetry-exporter-logging-otlp");
EXPORTER_ARTIFACT_ID_BY_NAME.put("otlp", "opentelemetry-exporter-otlp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class SpanExporterConfiguration {

static {
EXPORTER_ARTIFACT_ID_BY_NAME = new HashMap<>();
EXPORTER_ARTIFACT_ID_BY_NAME.put("console", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("jaeger", "opentelemetry-exporter-jaeger");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging", "opentelemetry-exporter-logging");
EXPORTER_ARTIFACT_ID_BY_NAME.put("logging-otlp", "opentelemetry-exporter-logging-otlp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.Closeable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -32,6 +33,8 @@ final class TracerProviderConfiguration {

private static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d;
private static final String PARENTBASED_ALWAYS_ON = "parentbased_always_on";
private static final List<String> simpleProcessorExporterNames =
Arrays.asList("console", "logging");

static void configureTracerProvider(
SdkTracerProviderBuilder tracerProviderBuilder,
Expand Down Expand Up @@ -74,11 +77,13 @@ static List<SpanProcessor> configureSpanProcessors(
Map<String, SpanExporter> exportersByNameCopy = new HashMap<>(exportersByName);
List<SpanProcessor> spanProcessors = new ArrayList<>();

SpanExporter exporter = exportersByNameCopy.remove("logging");
if (exporter != null) {
SpanProcessor spanProcessor = SimpleSpanProcessor.create(exporter);
closeables.add(spanProcessor);
spanProcessors.add(spanProcessor);
for (String simpleProcessorExporterNames : simpleProcessorExporterNames) {
SpanExporter exporter = exportersByNameCopy.remove(simpleProcessorExporterNames);
if (exporter != null) {
SpanProcessor spanProcessor = SimpleSpanProcessor.create(exporter);
closeables.add(spanProcessor);
spanProcessors.add(spanProcessor);
}
}

if (!exportersByNameCopy.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.assertj.core.api.InstanceOfAssertFactories;
Expand Down Expand Up @@ -137,20 +138,25 @@ void configureExporter_NotFound() {

@Test
void configureSpanProcessors_simpleSpanProcessor() {
String exporterName = "logging";
List<Closeable> closeables = new ArrayList<>();

Map<String, SpanExporter> exportersByName = new LinkedHashMap<>();
exportersByName.put("console", LoggingSpanExporter.create());
exportersByName.put("logging", LoggingSpanExporter.create());

List<SpanProcessor> spanProcessors =
TracerProviderConfiguration.configureSpanProcessors(
DefaultConfigProperties.createFromMap(
Collections.singletonMap("otel.traces.exporter", exporterName)),
ImmutableMap.of(exporterName, LoggingSpanExporter.create()),
Collections.singletonMap("otel.traces.exporter", "console,logging")),
exportersByName,
MeterProvider.noop(),
closeables);
cleanup.addCloseables(closeables);

assertThat(spanProcessors).hasExactlyElementsOfTypes(SimpleSpanProcessor.class);
assertThat(closeables).hasExactlyElementsOfTypes(SimpleSpanProcessor.class);
assertThat(spanProcessors)
.hasExactlyElementsOfTypes(SimpleSpanProcessor.class, SimpleSpanProcessor.class);
assertThat(closeables)
.hasExactlyElementsOfTypes(SimpleSpanProcessor.class, SimpleSpanProcessor.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void configureExporter_KnownSpiExportersOnClasspath() {
LogRecordExporterConfiguration.logRecordExporterSpiManager(
DefaultConfigProperties.createFromMap(Collections.emptyMap()), spiHelper);

assertThat(LogRecordExporterConfiguration.configureExporter("console", spiExportersManager))
.isInstanceOf(SystemOutLogRecordExporter.class);
assertThat(LogRecordExporterConfiguration.configureExporter("logging", spiExportersManager))
.isInstanceOf(SystemOutLogRecordExporter.class);
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
Expand All @@ -18,12 +17,14 @@
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.trace.internal.JcTools;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
Expand Down Expand Up @@ -85,26 +86,31 @@ void configureLoggerProvider() {
void configureLogRecordProcessors_multipleExportersWithLogging() {
List<Closeable> closeables = new ArrayList<>();

Map<String, LogRecordExporter> exportersByName = new LinkedHashMap<>();
exportersByName.put("console", SystemOutLogRecordExporter.create());
exportersByName.put("logging", SystemOutLogRecordExporter.create());
exportersByName.put("otlp", OtlpGrpcLogRecordExporter.builder().build());

List<LogRecordProcessor> logRecordProcessors =
LoggerProviderConfiguration.configureLogRecordProcessors(
DefaultConfigProperties.createFromMap(Collections.emptyMap()),
ImmutableMap.of(
"logging",
SystemOutLogRecordExporter.create(),
"otlp",
OtlpGrpcLogRecordExporter.builder().build()),
exportersByName,
MeterProvider.noop(),
closeables);
cleanup.addCloseables(closeables);

assertThat(logRecordProcessors)
.hasSize(2)
.hasAtLeastOneElementOfType(SimpleLogRecordProcessor.class)
.hasAtLeastOneElementOfType(BatchLogRecordProcessor.class);
.hasSize(3)
.hasExactlyElementsOfTypes(
SimpleLogRecordProcessor.class,
SimpleLogRecordProcessor.class,
BatchLogRecordProcessor.class);
assertThat(closeables)
.hasSize(2)
.hasAtLeastOneElementOfType(SimpleLogRecordProcessor.class)
.hasAtLeastOneElementOfType(BatchLogRecordProcessor.class);
.hasSize(3)
.hasExactlyElementsOfTypes(
SimpleLogRecordProcessor.class,
SimpleLogRecordProcessor.class,
BatchLogRecordProcessor.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void configureExporter_KnownSpiExportersOnClasspath() {
SpanExporterConfiguration.spanExporterSpiManager(
DefaultConfigProperties.createFromMap(Collections.emptyMap()), spiHelper);

assertThat(SpanExporterConfiguration.configureExporter("console", spiExportersManager))
.isInstanceOf(LoggingSpanExporter.class);
assertThat(SpanExporterConfiguration.configureExporter("logging", spiExportersManager))
.isInstanceOf(LoggingSpanExporter.class);
assertThat(SpanExporterConfiguration.configureExporter("logging-otlp", spiExportersManager))
Expand Down

0 comments on commit 7655192

Please sign in to comment.