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

Removing protobuf dependency #1008

Merged
merged 17 commits into from
Aug 24, 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
30 changes: 28 additions & 2 deletions disk-buffering/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id("otel.publish-conventions")
id("me.champeau.jmh") version "0.7.1"
id("ru.vyarus.animalsniffer") version "1.7.1"
id("com.squareup.wire") version "4.8.1"
}

description = "Exporter implementations that store signals on disk"
Expand All @@ -17,8 +18,6 @@ java {

dependencies {
api("io.opentelemetry:opentelemetry-sdk")
implementation("io.opentelemetry:opentelemetry-exporter-otlp-common")
implementation("io.opentelemetry.proto:opentelemetry-proto:0.20.0-alpha")
compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
signature("com.toasttab.android:gummy-bears-api-24:0.5.1@signature")
Expand Down Expand Up @@ -47,3 +46,30 @@ jmh {
timeOnIteration.set("5s")
timeUnit.set("ms")
}

wire {
java {}

sourcePath {
srcJar("io.opentelemetry.proto:opentelemetry-proto:0.20.0-alpha")
}

root(
"opentelemetry.proto.trace.v1.TracesData",
"opentelemetry.proto.metrics.v1.MetricsData",
"opentelemetry.proto.logs.v1.LogsData",
)
}

// The javadoc from wire's generated classes has errors that make the task that generates the "javadoc" artifact to fail. This
// makes the javadoc task to ignore those generated classes.
tasks.withType(Javadoc::class.java) {
exclude("io/opentelemetry/proto/*")
}

// The task that generates the "sources" artifact fails due to a "duplicated io/opentelemetry/proto/metrics/v1/Exemplar.java" file
// Which is strange since there's only one file like that which is generated by wire and the main "jar" task doesn't raise the same issue.
// This allows to ignore any subsequent files with the same path when creating the "sources" artifact.
tasks.named("sourcesJar", Jar::class.java) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public List<KeyValue> attributesToProto(Attributes attributes) {
public Attributes protoToAttributes(List<KeyValue> values) {
AttributesBuilder builder = Attributes.builder();
for (KeyValue keyValue : values) {
addValue(builder, keyValue.getKey(), keyValue.getValue());
addValue(builder, keyValue.key, keyValue.value);
}
return builder.build();
}

private static KeyValue attributeEntryToProto(AttributeKey<?> key, Object value) {
KeyValue.Builder builder = KeyValue.newBuilder();
builder.setKey(key.getKey());
builder.setValue(attributeValueToProto(key.getType(), value));
KeyValue.Builder builder = new KeyValue.Builder();
builder.key(key.getKey());
builder.value(attributeValueToProto(key.getType(), value));
return builder.build();
}

Expand Down Expand Up @@ -68,76 +68,76 @@ private static AnyValue attributeValueToProto(AttributeType type, Object value)
}

private static AnyValue arrayToAnyValue(List<AnyValue> value) {
return AnyValue.newBuilder()
.setArrayValue(ArrayValue.newBuilder().addAllValues(value).build())
return new AnyValue.Builder()
.array_value(new ArrayValue.Builder().values(value).build())
.build();
}

private static void addValue(AttributesBuilder builder, String key, AnyValue value) {
if (value.hasStringValue()) {
builder.put(AttributeKey.stringKey(key), value.getStringValue());
} else if (value.hasBoolValue()) {
builder.put(AttributeKey.booleanKey(key), value.getBoolValue());
} else if (value.hasIntValue()) {
builder.put(AttributeKey.longKey(key), value.getIntValue());
} else if (value.hasDoubleValue()) {
builder.put(AttributeKey.doubleKey(key), value.getDoubleValue());
} else if (value.hasArrayValue()) {
addArray(builder, key, value.getArrayValue());
if (value.string_value != null) {
builder.put(AttributeKey.stringKey(key), value.string_value);
} else if (value.bool_value != null) {
builder.put(AttributeKey.booleanKey(key), value.bool_value);
} else if (value.int_value != null) {
builder.put(AttributeKey.longKey(key), value.int_value);
} else if (value.double_value != null) {
builder.put(AttributeKey.doubleKey(key), value.double_value);
} else if (value.array_value != null) {
addArray(builder, key, value.array_value);
} else {
throw new UnsupportedOperationException();
}
}

private static void addArray(AttributesBuilder builder, String key, ArrayValue arrayValue) {
List<AnyValue> values = arrayValue.getValuesList();
List<AnyValue> values = arrayValue.values;
AnyValue anyValue = values.get(0);
if (anyValue.hasStringValue()) {
if (anyValue.string_value != null) {
builder.put(AttributeKey.stringArrayKey(key), anyValuesToStrings(values));
} else if (anyValue.hasBoolValue()) {
} else if (anyValue.bool_value != null) {
builder.put(AttributeKey.booleanArrayKey(key), anyValuesToBooleans(values));
} else if (anyValue.hasIntValue()) {
} else if (anyValue.int_value != null) {
builder.put(AttributeKey.longArrayKey(key), anyValuesToLongs(values));
} else if (anyValue.hasDoubleValue()) {
} else if (anyValue.double_value != null) {
builder.put(AttributeKey.doubleArrayKey(key), anyValuesToDoubles(values));
} else {
throw new UnsupportedOperationException();
}
}

private static AnyValue stringToAnyValue(String value) {
AnyValue.Builder anyValue = AnyValue.newBuilder();
AnyValue.Builder anyValue = new AnyValue.Builder();

anyValue.setStringValue(value);
anyValue.string_value(value);

return anyValue.build();
}

private static AnyValue booleanToAnyValue(Boolean value) {
AnyValue.Builder anyValue = AnyValue.newBuilder();
AnyValue.Builder anyValue = new AnyValue.Builder();

if (value != null) {
anyValue.setBoolValue(value);
anyValue.bool_value(value);
}

return anyValue.build();
}

private static AnyValue longToAnyValue(Long value) {
AnyValue.Builder anyValue = AnyValue.newBuilder();
AnyValue.Builder anyValue = new AnyValue.Builder();

if (value != null) {
anyValue.setIntValue(value);
anyValue.int_value(value);
}

return anyValue.build();
}

private static AnyValue doubleToAnyValue(Double value) {
AnyValue.Builder anyValue = AnyValue.newBuilder();
AnyValue.Builder anyValue = new AnyValue.Builder();

if (value != null) {
anyValue.setDoubleValue(value);
anyValue.double_value(value);
}

return anyValue.build();
Expand Down Expand Up @@ -216,18 +216,18 @@ private static List<Double> anyValuesToDoubles(List<AnyValue> values) {
}

private static String anyValueToString(AnyValue value) {
return value.getStringValue();
return value.string_value;
}

private static Boolean anyValueToBoolean(AnyValue value) {
return value.getBoolValue();
return value.bool_value;
}

private static Long anyValueToLong(AnyValue value) {
return value.getIntValue();
return value.int_value;
}

private static Double anyValueToDouble(AnyValue value) {
return value.getDoubleValue();
return value.double_value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ protected Resource protoToResource(

protected InstrumentationScopeInfo protoToInstrumentationScopeInfo(
InstrumentationScope scope, @Nullable String schemaUrl) {
InstrumentationScopeInfoBuilder builder = InstrumentationScopeInfo.builder(scope.getName());
builder.setAttributes(protoToAttributes(scope.getAttributesList()));
if (!scope.getVersion().isEmpty()) {
builder.setVersion(scope.getVersion());
InstrumentationScopeInfoBuilder builder = InstrumentationScopeInfo.builder(scope.name);
builder.setAttributes(protoToAttributes(scope.attributes));
if (!scope.version.isEmpty()) {
builder.setVersion(scope.version);
}
if (schemaUrl != null) {
builder.setSchemaUrl(schemaUrl);
Expand All @@ -89,11 +89,11 @@ protected InstrumentationScopeInfo protoToInstrumentationScopeInfo(

protected InstrumentationScope instrumentationScopeToProto(InstrumentationScopeInfo source) {
InstrumentationScope.Builder builder =
InstrumentationScope.newBuilder().setName(source.getName());
new InstrumentationScope.Builder().name(source.getName());
if (source.getVersion() != null) {
builder.setVersion(source.getVersion());
builder.version(source.getVersion());
}
builder.addAllAttributes(attributesToProto(source.getAttributes()));
builder.attributes.addAll(attributesToProto(source.getAttributes()));
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.contrib.disk.buffering.internal.serialization.mapping.common;

import com.google.protobuf.ByteString;
import okio.ByteString;

public final class ByteStringMapper {

Expand All @@ -16,10 +16,10 @@ public static ByteStringMapper getInstance() {
}

public ByteString stringToProto(String source) {
return ByteString.copyFromUtf8(source);
return ByteString.encodeUtf8(source);
}

public String protoToString(ByteString source) {
return source.toStringUtf8();
return source.utf8();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public static ResourceMapper getInstance() {
}

public Resource mapToProto(io.opentelemetry.sdk.resources.Resource sdkResource) {
return Resource.newBuilder()
.addAllAttributes(
AttributesMapper.getInstance().attributesToProto(sdkResource.getAttributes()))
return new Resource.Builder()
.attributes(AttributesMapper.getInstance().attributesToProto(sdkResource.getAttributes()))
.build();
}

Expand All @@ -31,8 +30,7 @@ public io.opentelemetry.sdk.resources.Resource mapToSdk(
if (schemaUrl != null) {
resource.setSchemaUrl(schemaUrl);
}
resource.putAll(
AttributesMapper.getInstance().protoToAttributes(protoResource.getAttributesList()));
resource.putAll(AttributesMapper.getInstance().protoToAttributes(protoResource.attributes));
return resource.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,47 @@ public static LogRecordDataMapper getInstance() {
}

public LogRecord mapToProto(LogRecordData source) {
LogRecord.Builder logRecord = LogRecord.newBuilder();
LogRecord.Builder logRecord = new LogRecord.Builder();

logRecord.setTimeUnixNano(source.getTimestampEpochNanos());
logRecord.setObservedTimeUnixNano(source.getObservedTimestampEpochNanos());
logRecord.time_unix_nano(source.getTimestampEpochNanos());
logRecord.observed_time_unix_nano(source.getObservedTimestampEpochNanos());
if (source.getSeverity() != null) {
logRecord.setSeverityNumber(severityToProto(source.getSeverity()));
logRecord.severity_number(severityToProto(source.getSeverity()));
}
if (source.getSeverityText() != null) {
logRecord.setSeverityText(source.getSeverityText());
logRecord.severity_text(source.getSeverityText());
}
if (source.getBody() != null) {
logRecord.setBody(bodyToAnyValue(source.getBody()));
logRecord.body(bodyToAnyValue(source.getBody()));
}

logRecord.setFlags(source.getSpanContext().getTraceFlags().asByte());
logRecord.flags(source.getSpanContext().getTraceFlags().asByte());

addExtrasToProtoBuilder(source, logRecord);

return logRecord.build();
}

private static void addExtrasToProtoBuilder(LogRecordData source, LogRecord.Builder target) {
target.addAllAttributes(
target.attributes.addAll(
AttributesMapper.getInstance().attributesToProto(source.getAttributes()));
SpanContext spanContext = source.getSpanContext();
target.setSpanId(ByteStringMapper.getInstance().stringToProto(spanContext.getSpanId()));
target.setTraceId(ByteStringMapper.getInstance().stringToProto(spanContext.getTraceId()));
target.setDroppedAttributesCount(
target.span_id(ByteStringMapper.getInstance().stringToProto(spanContext.getSpanId()));
target.trace_id(ByteStringMapper.getInstance().stringToProto(spanContext.getTraceId()));
target.dropped_attributes_count(
source.getTotalAttributeCount() - source.getAttributes().size());
}

public LogRecordData mapToSdk(
LogRecord source, Resource resource, InstrumentationScopeInfo scopeInfo) {
LogRecordDataImpl.Builder logRecordData = LogRecordDataImpl.builder();

logRecordData.setTimestampEpochNanos(source.getTimeUnixNano());
logRecordData.setObservedTimestampEpochNanos(source.getObservedTimeUnixNano());
logRecordData.setSeverity(severityNumberToSdk(source.getSeverityNumber()));
logRecordData.setSeverityText(source.getSeverityText());
if (source.hasBody()) {
logRecordData.setBody(anyValueToBody(source.getBody()));
logRecordData.setTimestampEpochNanos(source.time_unix_nano);
logRecordData.setObservedTimestampEpochNanos(source.observed_time_unix_nano);
logRecordData.setSeverity(severityNumberToSdk(source.severity_number));
logRecordData.setSeverityText(source.severity_text);
if (source.body != null) {
logRecordData.setBody(anyValueToBody(source.body));
}

addExtrasToSdkItemBuilder(source, logRecordData, resource, scopeInfo);
Expand All @@ -83,39 +83,38 @@ private static void addExtrasToSdkItemBuilder(
LogRecordDataImpl.Builder target,
Resource resource,
InstrumentationScopeInfo scopeInfo) {
Attributes attributes =
AttributesMapper.getInstance().protoToAttributes(source.getAttributesList());
Attributes attributes = AttributesMapper.getInstance().protoToAttributes(source.attributes);
target.setAttributes(attributes);
target.setSpanContext(
SpanContext.create(
ByteStringMapper.getInstance().protoToString(source.getTraceId()),
ByteStringMapper.getInstance().protoToString(source.getSpanId()),
ByteStringMapper.getInstance().protoToString(source.trace_id),
ByteStringMapper.getInstance().protoToString(source.span_id),
TraceFlags.getSampled(),
TraceState.getDefault()));
target.setTotalAttributeCount(source.getDroppedAttributesCount() + attributes.size());
target.setTotalAttributeCount(source.dropped_attributes_count + attributes.size());
target.setResource(resource);
target.setInstrumentationScopeInfo(scopeInfo);
}

private static AnyValue bodyToAnyValue(Body body) {
return AnyValue.newBuilder().setStringValue(body.asString()).build();
return new AnyValue.Builder().string_value(body.asString()).build();
}

private static SeverityNumber severityToProto(Severity severity) {
return SeverityNumber.forNumber(severity.getSeverityNumber());
return SeverityNumber.fromValue(severity.getSeverityNumber());
}

private static Body anyValueToBody(AnyValue source) {
if (source.hasStringValue()) {
return Body.string(source.getStringValue());
if (source.string_value != null) {
return Body.string(source.string_value);
} else {
return Body.empty();
}
}

private static Severity severityNumberToSdk(SeverityNumber source) {
for (Severity value : Severity.values()) {
if (value.getSeverityNumber() == source.getNumber()) {
if (value.getSeverityNumber() == source.getValue()) {
return value;
}
}
Expand Down
Loading
Loading