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

Add a OTLP JSON span exporter to logs. #2295

Merged
merged 7 commits into from
Dec 23, 2020
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
9 changes: 9 additions & 0 deletions exporters/logging-otlp/README.md
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
22 changes: 22 additions & 0 deletions exporters/logging-otlp/build.gradle
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"
Copy link
Contributor

@jkwatson jkwatson Dec 22, 2020

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. Maybe description = 'OpenTelemetry logging exporters with OTLP JSON formatting. Currently supports span exports.' ?


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
}
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);
}
}
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);
}
}
}
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();
}
}
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;
Loading