Skip to content

Commit

Permalink
Add serialization of entities to resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Nov 15, 2024
1 parent a24d691 commit 1211b37
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.exporter.internal.otlp.experimental.ResourceEntityRefExperimental;
import io.opentelemetry.sdk.resources.Entity;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;

/**
* A Marshaler of {@link io.opentelemetry.sdk.resources.Entity}.
*
* <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 ResourceEntityRefMarshaler extends MarshalerWithSize {
@Nullable private final byte[] schemaUrlUtf8;
private final byte[] typeUtf8;
private final byte[][] identityAttributeKeysUtf8;
private final byte[][] descriptiveAttributeKeysUtf8;

@Override
protected void writeTo(Serializer output) throws IOException {
if (schemaUrlUtf8 != null) {
output.writeString(ResourceEntityRefExperimental.SCHEMA_URL, schemaUrlUtf8);
}
output.writeString(ResourceEntityRefExperimental.TYPE, typeUtf8);
for (byte[] keyUtf8 : identityAttributeKeysUtf8) {
output.serializeString(ResourceEntityRefExperimental.IDENTITY_ATTRIBUTES, keyUtf8);
}
for (byte[] keyUtf8 : descriptiveAttributeKeysUtf8) {
output.serializeString(ResourceEntityRefExperimental.DESCRIPTION_ATTRIBUTES, keyUtf8);
}
}

public static ResourceEntityRefMarshaler createForEntity(Entity e) {
byte[] schemaUrlUtf8 = null;
if (!StringUtils.isNullOrEmpty(e.getSchemaUrl())) {
schemaUrlUtf8 = e.getSchemaUrl().getBytes(StandardCharsets.UTF_8);
}
return new ResourceEntityRefMarshaler(
schemaUrlUtf8,
e.getType().getBytes(StandardCharsets.UTF_8),
e.getIdentifyingAttributes().asMap().keySet().stream()
.map(key -> key.getKey().getBytes(StandardCharsets.UTF_8))
.toArray(byte[][]::new),
e.getAttributes().asMap().keySet().stream()
.map(key -> key.getKey().getBytes(StandardCharsets.UTF_8))
.toArray(byte[][]::new));
}

private ResourceEntityRefMarshaler(
@Nullable byte[] schemaUrlUtf8,
byte[] typeUtf8,
byte[][] identityAttributeKeysUtf8,
byte[][] descriptiveAttributeKeysUtf8) {
super(
calculateSize(
schemaUrlUtf8, typeUtf8, identityAttributeKeysUtf8, descriptiveAttributeKeysUtf8));
this.schemaUrlUtf8 = schemaUrlUtf8;
this.typeUtf8 = typeUtf8;
this.identityAttributeKeysUtf8 = identityAttributeKeysUtf8;
this.descriptiveAttributeKeysUtf8 = descriptiveAttributeKeysUtf8;
}

private static int calculateSize(
@Nullable byte[] schemaUrlUtf8,
byte[] typeUtf8,
byte[][] identityAttributeKeysUtf8,
byte[][] descriptiveAttributeKeysUtf8) {
int size = 0;
if (schemaUrlUtf8 != null) {
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.SCHEMA_URL, schemaUrlUtf8);
}
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.TYPE, typeUtf8);
for (byte[] keyUtf8 : identityAttributeKeysUtf8) {
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.IDENTITY_ATTRIBUTES, keyUtf8);
}
for (byte[] keyUtf8 : descriptiveAttributeKeysUtf8) {
size +=
MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.DESCRIPTION_ATTRIBUTES, keyUtf8);
}
return size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.exporter.internal.otlp.experimental.ResourceExperimental;
import io.opentelemetry.proto.resource.v1.internal.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -37,7 +38,10 @@ public static ResourceMarshaler create(io.opentelemetry.sdk.resources.Resource r

RealResourceMarshaler realMarshaler =
new RealResourceMarshaler(
KeyValueMarshaler.createForAttributes(resource.getAttributes()));
KeyValueMarshaler.createForAttributes(resource.getAttributes()),
resource.getEntities().stream()
.map(ResourceEntityRefMarshaler::createForEntity)
.toArray(MarshalerWithSize[]::new));

ByteArrayOutputStream binaryBos =
new ByteArrayOutputStream(realMarshaler.getBinarySerializedSize());
Expand Down Expand Up @@ -70,19 +74,30 @@ public void writeTo(Serializer output) throws IOException {

private static final class RealResourceMarshaler extends MarshalerWithSize {
private final KeyValueMarshaler[] attributes;
private final MarshalerWithSize[] entityRefs;

private RealResourceMarshaler(KeyValueMarshaler[] attributes) {
super(calculateSize(attributes));
private RealResourceMarshaler(KeyValueMarshaler[] attributes, MarshalerWithSize[] entityRefs) {
super(calculateSize(attributes, entityRefs));
this.attributes = attributes;
this.entityRefs = entityRefs;
}

@Override
protected void writeTo(Serializer output) throws IOException {
output.serializeRepeatedMessage(Resource.ATTRIBUTES, attributes);
if (entityRefs.length > 0) {
output.serializeRepeatedMessage(ResourceExperimental.ENTITY_REFS, entityRefs);
}
}

private static int calculateSize(KeyValueMarshaler[] attributeMarshalers) {
return MarshalerUtil.sizeRepeatedMessage(Resource.ATTRIBUTES, attributeMarshalers);
private static int calculateSize(
KeyValueMarshaler[] attributeMarshalers, MarshalerWithSize[] entityRefs) {
int size = 0;
size += MarshalerUtil.sizeRepeatedMessage(Resource.ATTRIBUTES, attributeMarshalers);
if (entityRefs.length > 0) {
size += MarshalerUtil.sizeRepeatedMessage(ResourceExperimental.ENTITY_REFS, entityRefs);
}
return size;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp.experimental;

import io.opentelemetry.exporter.internal.marshal.ProtoFieldInfo;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class ResourceEntityRefExperimental {
public static final ProtoFieldInfo SCHEMA_URL = ProtoFieldInfo.create(1, 10, "schemaUrl");
public static final ProtoFieldInfo TYPE = ProtoFieldInfo.create(2, 18, "type");
public static final ProtoFieldInfo IDENTITY_ATTRIBUTES =
ProtoFieldInfo.create(3, 26, "idAttrKeys");
public static final ProtoFieldInfo DESCRIPTION_ATTRIBUTES =
ProtoFieldInfo.create(4, 34, "descrAttrKeys");

private ResourceEntityRefExperimental() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp.experimental;

import io.opentelemetry.exporter.internal.marshal.ProtoFieldInfo;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class ResourceExperimental {
public static final ProtoFieldInfo ENTITY_REFS = ProtoFieldInfo.create(3, 26, "entityRefs");

private ResourceExperimental() {}
}

0 comments on commit 1211b37

Please sign in to comment.