-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serialization of entities to resource.
- Loading branch information
Showing
4 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...mon/src/main/java/io/opentelemetry/exporter/internal/otlp/ResourceEntityRefMarshaler.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,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; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...a/io/opentelemetry/exporter/internal/otlp/experimental/ResourceEntityRefExperimental.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,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() {} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../main/java/io/opentelemetry/exporter/internal/otlp/experimental/ResourceExperimental.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,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() {} | ||
} |