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

Base64 encode AnyValue bytes in string representation #6003

Merged
merged 1 commit into from
Nov 21, 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 @@ -5,9 +5,9 @@

package io.opentelemetry.extension.incubator.logs;

import io.opentelemetry.api.internal.OtelEncodingUtils;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;

final class AnyValueBytes implements AnyValue<ByteBuffer> {
Expand Down Expand Up @@ -35,11 +35,7 @@ public ByteBuffer getValue() {

@Override
public String asString() {
// TODO: base64 would be better, but isn't available in android and java. Can we vendor in a
// base64 implementation?
char[] arr = new char[raw.length * 2];
OtelEncodingUtils.bytesToBase16(raw, arr, raw.length);
return new String(arr);
return Base64.getEncoder().encodeToString(raw);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import io.opentelemetry.api.internal.OtelEncodingUtils;
import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -208,16 +208,15 @@ private static Stream<Arguments> asStringArgs() {
AnyValue.of(Collections.singletonMap("grandchild", AnyValue.of("str"))))),
"[child=[grandchild=str]]"),
// bytes
arguments(
AnyValue.of("hello world".getBytes(StandardCharsets.UTF_8)), "68656c6c6f20776f726c64"));
arguments(AnyValue.of("hello world".getBytes(StandardCharsets.UTF_8)), "aGVsbG8gd29ybGQ="));
}

@Test
void anyValueByteAsString() {
// TODO: add more test cases
String str = "hello world";
String base16Encoded = AnyValue.of(str.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedBytes = OtelEncodingUtils.bytesFromBase16(base16Encoded, base16Encoded.length());
String base64Encoded = AnyValue.of(str.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedBytes = Base64.getDecoder().decode(base64Encoded);
assertThat(new String(decodedBytes, StandardCharsets.UTF_8)).isEqualTo(str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void anyValueBody() {
logRecordData -> {
// TODO (jack-berg): add assertion when ANY_VALUE is added to Body.Type
// assertThat(logRecordData.getBody().getType()).isEqualTo(Body.Type.ANY_VALUE);
assertThat(logRecordData.getBody().asString()).isEqualTo("68656c6c6f20776f726c64");
assertThat(logRecordData.getBody().asString()).isEqualTo("aGVsbG8gd29ybGQ=");
assertThat(((AnyValueBody) logRecordData.getBody()).asAnyValue())
.isEqualTo(AnyValue.of("hello world".getBytes(StandardCharsets.UTF_8)));
});
Expand Down Expand Up @@ -104,7 +104,7 @@ void anyValueBody() {
+ "bool_key=true, "
+ "long_key=1, "
+ "double_key=1.1, "
+ "bytes_key=6279746573, "
+ "bytes_key=Ynl0ZXM=, "
+ "arr_key=[entry1, 2, 3.3], "
+ "key_value_list_key=[child_str_key1=child_value1, child_str_key2=child_value2]"
+ "]");
Expand Down