Skip to content
Open
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 @@ -14,6 +14,7 @@
import io.opentelemetry.sdk.common.export.MemoryMode;
import java.io.OutputStream;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* Builder for {@link OtlpJsonLoggingLogRecordExporter}.
Expand All @@ -26,13 +27,14 @@ public final class OtlpStdoutLogRecordExporterBuilder {
private static final String TYPE = "log records";

private final Logger logger;
private JsonWriter jsonWriter;
@Nullable private OutputStream outputStream;
@Nullable private Logger outputLogger;
private boolean wrapperJsonObject = true;
private MemoryMode memoryMode = MemoryMode.IMMUTABLE_DATA;
private boolean prettyPrint;

public OtlpStdoutLogRecordExporterBuilder(Logger logger) {
this.logger = logger;
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
}

/**
Expand All @@ -57,6 +59,12 @@ public OtlpStdoutLogRecordExporterBuilder setMemoryMode(MemoryMode memoryMode) {
return this;
}

/** Sets the exporter to use pretty-printed JSON output. */
public OtlpStdoutLogRecordExporterBuilder setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
return this;
}

/**
* Sets the exporter to use the specified output stream.
*
Expand All @@ -67,14 +75,16 @@ public OtlpStdoutLogRecordExporterBuilder setMemoryMode(MemoryMode memoryMode) {
*/
public OtlpStdoutLogRecordExporterBuilder setOutput(OutputStream outputStream) {
requireNonNull(outputStream, "outputStream");
this.jsonWriter = new StreamJsonWriter(outputStream, TYPE);
this.outputStream = outputStream;
this.outputLogger = null;
return this;
}

/** Sets the exporter to use the specified logger. */
public OtlpStdoutLogRecordExporterBuilder setOutput(Logger logger) {
requireNonNull(logger, "logger");
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
this.outputLogger = logger;
this.outputStream = null;
return this;
}

Expand All @@ -88,6 +98,13 @@ public OtlpStdoutLogRecordExporter build() {
throw new IllegalArgumentException(
"Reusable data mode is not supported without wrapperJsonObject");
}
JsonWriter jsonWriter;
if (outputStream != null) {
jsonWriter = new StreamJsonWriter(outputStream, TYPE, prettyPrint);
} else {
Logger writerLogger = outputLogger != null ? outputLogger : this.logger;
jsonWriter = new LoggerJsonWriter(writerLogger, TYPE, prettyPrint);
}
return new OtlpStdoutLogRecordExporter(logger, jsonWriter, wrapperJsonObject, memoryMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public String getName() {
public LogRecordExporter create(DeclarativeConfigProperties config) {
OtlpStdoutLogRecordExporterBuilder builder = OtlpStdoutLogRecordExporter.builder();
IncubatingExporterBuilderUtil.configureExporterMemoryMode(config, builder::setMemoryMode);
Boolean prettyPrint = config.getBoolean("pretty_print");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're out of spec with this. So let's add some guardrails to make it clear:

Suggested change
Boolean prettyPrint = config.getBoolean("pretty_print");
// TODO: conform to spec / declarative config naming conventions when available: https://github.com/open-telemetry/opentelemetry-specification/issues/4940
Boolean prettyPrint = config.getBoolean("pretty_print/development_java");

In declarative config, we denote experimental / development status of properties with the /development suffix. Here, I've proposed taking that convention further and indicating that this is a java-specific development option. This should help confusion if users try to take this java feature and use it in other languages.

Thoughts @open-telemetry/java-approvers, @open-telemetry/configuration-approvers?

if (prettyPrint != null) {
builder.setPrettyPrint(prettyPrint);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.io.OutputStream;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* Builder for {@link OtlpJsonLoggingMetricExporter}.
Expand All @@ -39,13 +40,14 @@ public final class OtlpStdoutMetricExporterBuilder {
DefaultAggregationSelector.getDefault();

private final Logger logger;
private JsonWriter jsonWriter;
@Nullable private OutputStream outputStream;
@Nullable private Logger outputLogger;
private boolean wrapperJsonObject = true;
private MemoryMode memoryMode = MemoryMode.IMMUTABLE_DATA;
private boolean prettyPrint;

public OtlpStdoutMetricExporterBuilder(Logger logger) {
this.logger = logger;
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
}

/**
Expand All @@ -70,6 +72,12 @@ public OtlpStdoutMetricExporterBuilder setMemoryMode(MemoryMode memoryMode) {
return this;
}

/** Sets the exporter to use pretty-printed JSON output. */
public OtlpStdoutMetricExporterBuilder setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
return this;
}

/**
* Sets the exporter to use the specified output stream.
*
Expand All @@ -80,14 +88,16 @@ public OtlpStdoutMetricExporterBuilder setMemoryMode(MemoryMode memoryMode) {
*/
public OtlpStdoutMetricExporterBuilder setOutput(OutputStream outputStream) {
requireNonNull(outputStream, "outputStream");
this.jsonWriter = new StreamJsonWriter(outputStream, TYPE);
this.outputStream = outputStream;
this.outputLogger = null;
return this;
}

/** Sets the exporter to use the specified logger. */
public OtlpStdoutMetricExporterBuilder setOutput(Logger logger) {
requireNonNull(logger, "logger");
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
this.outputLogger = logger;
this.outputStream = null;
return this;
}

Expand Down Expand Up @@ -131,6 +141,13 @@ public OtlpStdoutMetricExporter build() {
throw new IllegalArgumentException(
"Reusable data mode is not supported without wrapperJsonObject");
}
JsonWriter jsonWriter;
if (outputStream != null) {
jsonWriter = new StreamJsonWriter(outputStream, TYPE, prettyPrint);
} else {
Logger writerLogger = outputLogger != null ? outputLogger : this.logger;
jsonWriter = new LoggerJsonWriter(writerLogger, TYPE, prettyPrint);
}
return new OtlpStdoutMetricExporter(
logger,
jsonWriter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public MetricExporter create(DeclarativeConfigProperties config) {
config, builder::setAggregationTemporalitySelector);
IncubatingExporterBuilderUtil.configureOtlpHistogramDefaultAggregation(
config, builder::setDefaultAggregationSelector);
Boolean prettyPrint = config.getBoolean("pretty_print");
if (prettyPrint != null) {
builder.setPrettyPrint(prettyPrint);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.sdk.common.export.MemoryMode;
import java.io.OutputStream;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* Builder for {@link OtlpJsonLoggingSpanExporter}.
Expand All @@ -26,13 +27,14 @@ public final class OtlpStdoutSpanExporterBuilder {
private static final String TYPE = "spans";

private final Logger logger;
private JsonWriter jsonWriter;
@Nullable private OutputStream outputStream;
@Nullable private Logger outputLogger;
private boolean wrapperJsonObject = true;
private MemoryMode memoryMode = MemoryMode.IMMUTABLE_DATA;
private boolean prettyPrint;

public OtlpStdoutSpanExporterBuilder(Logger logger) {
this.logger = logger;
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
}

/**
Expand All @@ -57,6 +59,12 @@ public OtlpStdoutSpanExporterBuilder setMemoryMode(MemoryMode memoryMode) {
return this;
}

/** Sets the exporter to use pretty-printed JSON output. */
public OtlpStdoutSpanExporterBuilder setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
return this;
}

/**
* Sets the exporter to use the specified output stream.
*
Expand All @@ -67,14 +75,16 @@ public OtlpStdoutSpanExporterBuilder setMemoryMode(MemoryMode memoryMode) {
*/
public OtlpStdoutSpanExporterBuilder setOutput(OutputStream outputStream) {
requireNonNull(outputStream, "outputStream");
this.jsonWriter = new StreamJsonWriter(outputStream, TYPE);
this.outputStream = outputStream;
this.outputLogger = null;
return this;
}

/** Sets the exporter to use the specified logger. */
public OtlpStdoutSpanExporterBuilder setOutput(Logger logger) {
requireNonNull(logger, "logger");
this.jsonWriter = new LoggerJsonWriter(logger, TYPE);
this.outputLogger = logger;
this.outputStream = null;
return this;
}

Expand All @@ -88,6 +98,13 @@ public OtlpStdoutSpanExporter build() {
throw new IllegalArgumentException(
"Reusable data mode is not supported without wrapperJsonObject");
}
JsonWriter jsonWriter;
if (outputStream != null) {
jsonWriter = new StreamJsonWriter(outputStream, TYPE, prettyPrint);
} else {
Logger writerLogger = outputLogger != null ? outputLogger : this.logger;
jsonWriter = new LoggerJsonWriter(writerLogger, TYPE, prettyPrint);
}
return new OtlpStdoutSpanExporter(logger, jsonWriter, wrapperJsonObject, memoryMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public String getName() {
public SpanExporter create(DeclarativeConfigProperties config) {
OtlpStdoutSpanExporterBuilder builder = OtlpStdoutSpanExporter.builder();
IncubatingExporterBuilderUtil.configureExporterMemoryMode(config, builder::setMemoryMode);
Boolean prettyPrint = config.getBoolean("pretty_print");
if (prettyPrint != null) {
builder.setPrettyPrint(prettyPrint);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import java.io.IOException;
import java.io.OutputStream;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
Expand All @@ -26,5 +27,13 @@ public static JsonGenerator create(SegmentedStringWriter stringWriter) {
}
}

public static JsonGenerator create(OutputStream os) {
try {
return JSON_FACTORY.createGenerator(os);
} catch (IOException e) {
throw new IllegalStateException("Unable to create in-memory JsonGenerator, can't happen.", e);
}
}

private JsonUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ public class LoggerJsonWriter implements JsonWriter {

private final Logger logger;
private final String type;
private final boolean prettyPrint;

public LoggerJsonWriter(Logger logger, String type) {
public LoggerJsonWriter(Logger logger, String type, boolean prettyPrint) {
this.logger = logger;
this.type = type;
this.prettyPrint = prettyPrint;
}

@Override
public CompletableResultCode write(Marshaler exportRequest) {
SegmentedStringWriter sw = new SegmentedStringWriter(JSON_FACTORY._getBufferRecycler());
try (JsonGenerator gen = JsonUtil.create(sw)) {
if (prettyPrint) {
gen.useDefaultPrettyPrinter();
}
exportRequest.writeJsonToGenerator(gen);
} catch (IOException e) {
logger.log(Level.WARNING, "Unable to write OTLP JSON " + type, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.exporter.logging.otlp.internal.writer;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
Expand All @@ -21,27 +20,28 @@
*/
public class StreamJsonWriter implements JsonWriter {

public static final JsonFactory JSON_FACTORY = new JsonFactory();

private static final Logger internalLogger = Logger.getLogger(StreamJsonWriter.class.getName());

private final ThrottlingLogger logger = new ThrottlingLogger(internalLogger);

private final String type;
private final OutputStream outputStream;
private final boolean prettyPrint;

public StreamJsonWriter(OutputStream originalStream, String type) {
public StreamJsonWriter(OutputStream originalStream, String type, boolean prettyPrint) {
this.outputStream = originalStream;
this.type = type;
this.prettyPrint = prettyPrint;
}

@Override
public CompletableResultCode write(Marshaler exportRequest) {
try {
exportRequest.writeJsonWithNewline(
JSON_FACTORY
.createGenerator(outputStream)
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
try (JsonGenerator gen = JsonUtil.create(outputStream)) {
gen.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
if (prettyPrint) {
gen.useDefaultPrettyPrinter();
}
exportRequest.writeJsonWithNewline(gen);
return CompletableResultCode.ofSuccess();
} catch (IOException e) {
logger.log(Level.WARNING, "Unable to write OTLP JSON " + type, e);
Expand Down
Loading
Loading