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

Serialize log body any value #5938

Merged
merged 1 commit into from
Dec 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void writeString(ProtoFieldInfo field, byte[] utf8Bytes) throws IOExcepti
}

@Override
protected void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
public void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
generator.writeBinaryField(field.getJsonName(), value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void writeString(ProtoFieldInfo field, byte[] utf8Bytes) throws IOExcepti
}

@Override
protected void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
public void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeByteArrayNoTag(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void serializeBytes(ProtoFieldInfo field, byte[] value) throws IOExceptio
writeBytes(field, value);
}

protected abstract void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException;
public abstract void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException;

protected abstract void writeStartMessage(ProtoFieldInfo field, int protoMessageSize)
throws IOException;
Expand Down
1 change: 1 addition & 0 deletions exporters/otlp/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
protoSource("io.opentelemetry.proto:opentelemetry-proto:${versions["io.opentelemetry.proto"]}")

api(project(":exporters:common"))
implementation(project(":extensions:incubator"))

compileOnly(project(":sdk:metrics"))
compileOnly(project(":sdk:trace"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.extension.incubator.logs.AnyValue;
import io.opentelemetry.extension.incubator.logs.KeyAnyValue;
import java.nio.ByteBuffer;
import java.util.List;

/**
* Utility methods for obtaining AnyValue marshaler.
*
* <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 AnyValueMarshaler {

private AnyValueMarshaler() {}

@SuppressWarnings("unchecked")
public static MarshalerWithSize create(AnyValue<?> anyValue) {
switch (anyValue.getType()) {
case STRING:
return StringAnyValueMarshaler.create((String) anyValue.getValue());
case BOOLEAN:
return BoolAnyValueMarshaler.create((boolean) anyValue.getValue());
case LONG:
return IntAnyValueMarshaler.create((long) anyValue.getValue());
case DOUBLE:
return DoubleAnyValueMarshaler.create((double) anyValue.getValue());
case ARRAY:
return ArrayAnyValueMarshaler.createAnyValue((List<AnyValue<?>>) anyValue.getValue());
case KEY_VALUE_LIST:
return KeyValueListAnyValueMarshaler.create((List<KeyAnyValue>) anyValue.getValue());
case BYTES:
return BytesAnyValueMarshaler.create((ByteBuffer) anyValue.getValue());
}
throw new IllegalArgumentException("Unsupported AnyValue type: " + anyValue.getType());

Check warning on line 42 in exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AnyValueMarshaler.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AnyValueMarshaler.java#L42

Added line #L42 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import io.opentelemetry.proto.common.v1.internal.ArrayValue;
import java.io.IOException;
import java.util.List;
import java.util.function.Function;

final class ArrayAnyValueMarshaler extends MarshalerWithSize {
private final Marshaler value;

private ArrayAnyValueMarshaler(ArrayValueMarshaler value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize createAnyValue(
List<io.opentelemetry.extension.incubator.logs.AnyValue<?>> values) {
return createInternal(values, AnyValueMarshaler::create);
}

static MarshalerWithSize createString(List<String> values) {
return createInternal(values, StringAnyValueMarshaler::create);
}

static MarshalerWithSize createBool(List<Boolean> values) {
return createInternal(values, BoolAnyValueMarshaler::create);
}

static MarshalerWithSize createInt(List<Long> values) {
return createInternal(values, IntAnyValueMarshaler::create);
}

static MarshalerWithSize createDouble(List<Double> values) {
return createInternal(values, DoubleAnyValueMarshaler::create);
}

private static <T, M extends MarshalerWithSize> MarshalerWithSize createInternal(
List<T> values, Function<T, M> initializer) {
int len = values.size();
Marshaler[] marshalers = new Marshaler[len];
for (int i = 0; i < len; i++) {
marshalers[i] = initializer.apply(values.get(i));
}
return new ArrayAnyValueMarshaler(new ArrayValueMarshaler(marshalers));
}

@Override
public void writeTo(Serializer output) throws IOException {
output.serializeMessage(AnyValue.ARRAY_VALUE, value);
}

private static int calculateSize(Marshaler value) {
return MarshalerUtil.sizeMessage(AnyValue.ARRAY_VALUE, value);
}

private static class ArrayValueMarshaler extends MarshalerWithSize {

private final Marshaler[] values;

private ArrayValueMarshaler(Marshaler[] values) {
super(calculateSize(values));
this.values = values;
}

@Override
public void writeTo(Serializer output) throws IOException {
output.serializeRepeatedMessage(ArrayValue.VALUES, values);
}

private static int calculateSize(Marshaler[] values) {
return MarshalerUtil.sizeRepeatedMessage(ArrayValue.VALUES, values);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import java.io.IOException;

final class BoolAnyValueMarshaler extends MarshalerWithSize {

private final boolean value;

private BoolAnyValueMarshaler(boolean value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize create(boolean value) {
return new BoolAnyValueMarshaler(value);
}

@Override
public void writeTo(Serializer output) throws IOException {
// Do not call serialize* method because we always have to write the message tag even if the
// value is empty since it's a oneof.
output.writeBool(AnyValue.BOOL_VALUE, value);
}

private static int calculateSize(boolean value) {
return AnyValue.BOOL_VALUE.getTagSize() + CodedOutputStream.computeBoolSizeNoTag(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import java.io.IOException;
import java.nio.ByteBuffer;

final class BytesAnyValueMarshaler extends MarshalerWithSize {

private final byte[] value;

private BytesAnyValueMarshaler(byte[] value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize create(ByteBuffer value) {
byte[] bytes = new byte[value.remaining()];
value.get(bytes);
return new BytesAnyValueMarshaler(bytes);
}

@Override
public void writeTo(Serializer output) throws IOException {
// Do not call serialize* method because we always have to write the message tag even if the
// value is empty since it's a oneof.
output.writeBytes(AnyValue.BYTES_VALUE, value);
}

private static int calculateSize(byte[] value) {
return AnyValue.BYTES_VALUE.getTagSize() + CodedOutputStream.computeByteArraySizeNoTag(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import java.io.IOException;

final class DoubleAnyValueMarshaler extends MarshalerWithSize {

private final double value;

private DoubleAnyValueMarshaler(double value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize create(double value) {
return new DoubleAnyValueMarshaler(value);
}

@Override
public void writeTo(Serializer output) throws IOException {
// Do not call serialize* method because we always have to write the message tag even if the
// value is empty since it's a oneof.
output.writeDouble(AnyValue.DOUBLE_VALUE, value);
}

private static int calculateSize(double value) {
return AnyValue.DOUBLE_VALUE.getTagSize() + CodedOutputStream.computeDoubleSizeNoTag(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static InstrumentationScopeMarshaler create(InstrumentationScopeInfo scop
// a few times until the cache gets filled which is fine.
byte[] name = MarshalerUtil.toBytes(scopeInfo.getName());
byte[] version = MarshalerUtil.toBytes(scopeInfo.getVersion());
KeyValueMarshaler[] attributes = KeyValueMarshaler.createRepeated(scopeInfo.getAttributes());
KeyValueMarshaler[] attributes =
KeyValueMarshaler.createForAttributes(scopeInfo.getAttributes());

RealInstrumentationScopeMarshaler realMarshaler =
new RealInstrumentationScopeMarshaler(name, version, attributes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import java.io.IOException;

final class IntAnyValueMarshaler extends MarshalerWithSize {

private final long value;

private IntAnyValueMarshaler(long value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize create(long value) {
return new IntAnyValueMarshaler(value);
}

@Override
public void writeTo(Serializer output) throws IOException {
// Do not call serialize* method because we always have to write the message tag even if the
// value is empty since it's a oneof.
output.writeInt64(AnyValue.INT_VALUE, value);
}

private static int calculateSize(long value) {
return AnyValue.INT_VALUE.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.extension.incubator.logs.KeyAnyValue;
import io.opentelemetry.proto.common.v1.internal.AnyValue;
import io.opentelemetry.proto.common.v1.internal.KeyValueList;
import java.io.IOException;
import java.util.List;

final class KeyValueListAnyValueMarshaler extends MarshalerWithSize {

private final Marshaler value;

private KeyValueListAnyValueMarshaler(KeyValueListMarshaler value) {
super(calculateSize(value));
this.value = value;
}

static MarshalerWithSize create(List<KeyAnyValue> values) {
int len = values.size();
KeyValueMarshaler[] marshalers = new KeyValueMarshaler[values.size()];
for (int i = 0; i < len; i++) {
marshalers[i] = KeyValueMarshaler.createForKeyAnyValue(values.get(i));
}
return new KeyValueListAnyValueMarshaler(new KeyValueListMarshaler(marshalers));
}

@Override
public void writeTo(Serializer output) throws IOException {
output.serializeMessage(AnyValue.KVLIST_VALUE, value);
}

private static int calculateSize(Marshaler value) {
return MarshalerUtil.sizeMessage(AnyValue.KVLIST_VALUE, value);
}

private static class KeyValueListMarshaler extends MarshalerWithSize {

private final Marshaler[] values;

private KeyValueListMarshaler(KeyValueMarshaler[] values) {
super(calculateSize(values));
this.values = values;
}

@Override
public void writeTo(Serializer output) throws IOException {
output.serializeRepeatedMessage(KeyValueList.VALUES, values);
}

private static int calculateSize(Marshaler[] values) {
return MarshalerUtil.sizeRepeatedMessage(KeyValueList.VALUES, values);
}
}
}
Loading