-
Notifications
You must be signed in to change notification settings - Fork 858
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
Add a OTLP JSON span exporter to logs. #2295
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
414a00b
Add a OTLP logging span exporter.
89149ce
Add file to git
bb5742e
Merge branch 'master' of github.com:open-telemetry/opentelemetry-java…
a34d0ed
Merge branch 'master' of github.com:open-telemetry/opentelemetry-java…
b701e00
Move
973f541
OTLPJSON
ec65fe4
Cleanups
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# OpenTelemetry - OTLP JSON Logging Exporter | ||
|
||
[![Javadocs][javadoc-image]][javadoc-url] | ||
|
||
Exporters for writing data to logs using OTLP JSON format. They are appropriate for writing spans or | ||
metrics to logs in a way that is both human-readable and structured for machine parsing. | ||
|
||
[javadoc-image]: https://www.javadoc.io/badge/io.opentelemetry/opentelemetry-exporters-otlpjson.svg | ||
[javadoc-url]: https://www.javadoc.io/doc/io.opentelemetry/opentelemetry-exporters-otlpjson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
id "java-library" | ||
id "maven-publish" | ||
|
||
id "ru.vyarus.animalsniffer" | ||
} | ||
|
||
description = 'OpenTelemetry Protocol JSON Logging Exporters' | ||
ext.moduleName = "io.opentelemetry.exporter.logging.otlp" | ||
|
||
dependencies { | ||
compileOnly project(':sdk:trace') | ||
compileOnly project(':sdk:metrics') | ||
|
||
implementation project(':sdk-extensions:otproto'), | ||
'org.curioswitch.curiostack:protobuf-jackson:1.1.0' | ||
|
||
testImplementation project(':sdk:testing'), | ||
'org.skyscreamer:jsonassert:1.5.0' | ||
|
||
signature libraries.android_signature | ||
} |
50 changes: 50 additions & 0 deletions
50
.../src/main/java/io/opentelemetry/exporter/logging/otlp/HexEncodingStringJsonGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.logging.otlp; | ||
|
||
import com.fasterxml.jackson.core.Base64Variant; | ||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.io.SegmentedStringWriter; | ||
import com.fasterxml.jackson.core.util.JsonGeneratorDelegate; | ||
import java.io.IOException; | ||
|
||
final class HexEncodingStringJsonGenerator extends JsonGeneratorDelegate { | ||
|
||
static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
|
||
static JsonGenerator create(SegmentedStringWriter stringWriter) { | ||
final JsonGenerator delegate; | ||
try { | ||
delegate = JSON_FACTORY.createGenerator(stringWriter); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to create in-memory JsonGenerator, can't happen.", e); | ||
} | ||
return new HexEncodingStringJsonGenerator(delegate); | ||
} | ||
|
||
private HexEncodingStringJsonGenerator(JsonGenerator delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) | ||
throws IOException { | ||
writeString(bytesToHex(data, offset, len)); | ||
} | ||
|
||
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); | ||
|
||
private static String bytesToHex(byte[] bytes, int offset, int len) { | ||
char[] hexChars = new char[len * 2]; | ||
for (int i = 0; i < len; i++) { | ||
int v = bytes[offset + i] & 0xFF; | ||
hexChars[i * 2] = HEX_ARRAY[v >>> 4]; | ||
hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
} | ||
return new String(hexChars); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...p/src/main/java/io/opentelemetry/exporter/logging/otlp/OtlpJsonLoggingMetricExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.logging.otlp; | ||
|
||
import static io.opentelemetry.exporter.logging.otlp.HexEncodingStringJsonGenerator.JSON_FACTORY; | ||
|
||
import com.fasterxml.jackson.core.Base64Variant; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.io.SegmentedStringWriter; | ||
import com.fasterxml.jackson.core.util.JsonGeneratorDelegate; | ||
import io.opentelemetry.proto.metrics.v1.ResourceMetrics; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.extension.otproto.MetricAdapter; | ||
import io.opentelemetry.sdk.metrics.data.MetricData; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.curioswitch.common.protobuf.json.MessageMarshaller; | ||
|
||
/** | ||
* A {@link MetricExporter} which writes {@linkplain MetricData spans} to a {@link Logger} in OTLP | ||
* JSON format. Each log line will include a single {@link ResourceMetrics}. | ||
*/ | ||
public class OtlpJsonLoggingMetricExporter implements MetricExporter { | ||
|
||
private static final MessageMarshaller marshaller = | ||
MessageMarshaller.builder() | ||
.register(ResourceMetrics.class) | ||
.omittingInsignificantWhitespace(true) | ||
.build(); | ||
|
||
// Visible for testing | ||
static final Logger logger = Logger.getLogger(OtlpJsonLoggingMetricExporter.class.getName()); | ||
|
||
/** Returns a new {@link OtlpJsonLoggingMetricExporter}. */ | ||
public static MetricExporter create() { | ||
return new OtlpJsonLoggingMetricExporter(); | ||
} | ||
|
||
private OtlpJsonLoggingMetricExporter() {} | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<MetricData> metrics) { | ||
List<ResourceMetrics> allResourceMetrics = MetricAdapter.toProtoResourceMetrics(metrics); | ||
for (ResourceMetrics resourceMetrics : allResourceMetrics) { | ||
SegmentedStringWriter sw = new SegmentedStringWriter(JSON_FACTORY._getBufferRecycler()); | ||
try (JsonGenerator gen = HexEncodingStringJsonGenerator.create(sw)) { | ||
marshaller.writeValue(resourceMetrics, gen); | ||
} catch (IOException e) { | ||
// Shouldn't happen in practice, just skip it. | ||
continue; | ||
} | ||
logger.log(Level.INFO, sw.getAndClear()); | ||
} | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
private static final class HexEncodingStringJsonGenerator extends JsonGeneratorDelegate { | ||
|
||
static JsonGenerator create(SegmentedStringWriter stringWriter) { | ||
final JsonGenerator delegate; | ||
try { | ||
delegate = JSON_FACTORY.createGenerator(stringWriter); | ||
} catch (IOException e) { | ||
throw new IllegalStateException( | ||
"Unable to create in-memory JsonGenerator, can't happen.", e); | ||
} | ||
return new HexEncodingStringJsonGenerator(delegate); | ||
} | ||
|
||
private HexEncodingStringJsonGenerator(JsonGenerator delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) | ||
throws IOException { | ||
writeString(bytesToHex(data, offset, len)); | ||
} | ||
|
||
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); | ||
|
||
private static String bytesToHex(byte[] bytes, int offset, int len) { | ||
char[] hexChars = new char[len * 2]; | ||
for (int i = 0; i < len; i++) { | ||
int v = bytes[offset + i] & 0xFF; | ||
hexChars[i * 2] = HEX_ARRAY[v >>> 4]; | ||
hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
} | ||
return new String(hexChars); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...tlp/src/main/java/io/opentelemetry/exporter/logging/otlp/OtlpJsonLoggingSpanExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.logging.otlp; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.io.SegmentedStringWriter; | ||
import io.opentelemetry.proto.trace.v1.ResourceSpans; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.extension.otproto.SpanAdapter; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.curioswitch.common.protobuf.json.MessageMarshaller; | ||
|
||
/** | ||
* A {@link SpanExporter} which writes {@linkplain SpanData spans} to a {@link Logger} in OTLP JSON | ||
* format. Each log line will include a single {@link ResourceSpans}. | ||
*/ | ||
public class OtlpJsonLoggingSpanExporter implements SpanExporter { | ||
|
||
private static final MessageMarshaller marshaller = | ||
MessageMarshaller.builder() | ||
.register(ResourceSpans.class) | ||
.omittingInsignificantWhitespace(true) | ||
.build(); | ||
|
||
// Visible for testing | ||
static final Logger logger = Logger.getLogger(OtlpJsonLoggingSpanExporter.class.getName()); | ||
|
||
/** Returns a new {@link OtlpJsonLoggingSpanExporter}. */ | ||
public static SpanExporter create() { | ||
return new OtlpJsonLoggingSpanExporter(); | ||
} | ||
|
||
private OtlpJsonLoggingSpanExporter() {} | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<SpanData> spans) { | ||
List<ResourceSpans> allResourceSpans = SpanAdapter.toProtoResourceSpans(spans); | ||
for (ResourceSpans resourceSpans : allResourceSpans) { | ||
SegmentedStringWriter sw = | ||
new SegmentedStringWriter( | ||
HexEncodingStringJsonGenerator.JSON_FACTORY._getBufferRecycler()); | ||
try (JsonGenerator gen = HexEncodingStringJsonGenerator.create(sw)) { | ||
marshaller.writeValue(resourceSpans, gen); | ||
} catch (IOException e) { | ||
// Shouldn't happen in practice, just skip it. | ||
continue; | ||
} | ||
logger.log(Level.INFO, sw.getAndClear()); | ||
} | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...rters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** OpenTelemetry exporters which writes spans or metrics to log using OTLP JSON format. */ | ||
@ParametersAreNonnullByDefault | ||
package io.opentelemetry.exporter.logging.otlp; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my only possible quibble is that maybe this should be
"io.opentelemetry.exporter.logging-otlp"
but I'm not quite sure which is going to be more discoverable by end-users. Maybe the description could be more .. descriptive about exactly what's going on here. Maybedescription = 'OpenTelemetry logging exporters with OTLP JSON formatting. Currently supports span exports.'
?