-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Universal profiling integration: Added serialization of stacktrace ID…
…s as profiler_stack_trace_ids otel attributes (#3607)
- Loading branch information
Showing
7 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
43 changes: 43 additions & 0 deletions
43
...nt-core/src/main/java/co/elastic/apm/agent/report/serialize/Base64SerializationUtils.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,43 @@ | ||
package co.elastic.apm.agent.report.serialize; | ||
|
||
import com.dslplatform.json.JsonWriter; | ||
|
||
public class Base64SerializationUtils { | ||
|
||
private static final byte[] BASE64_URL_CHARS = new byte[]{ | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | ||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | ||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | ||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | ||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | ||
'w', 'x', 'y', 'z', '0', '1', '2', '3', | ||
'4', '5', '6', '7', '8', '9', '-', '_', | ||
}; | ||
|
||
|
||
public static void writeBytesAsBase64UrlSafe(byte[] data, JsonWriter jw) { | ||
int i = 0; | ||
for (; i + 2 < data.length; i += 3) { | ||
int b0 = ((int) data[i]) & 0xFF; | ||
int b1 = ((int) data[i + 1]) & 0xFF; | ||
int b2 = ((int) data[i + 2]) & 0xFF; | ||
jw.writeByte(BASE64_URL_CHARS[b0 >> 2]); | ||
jw.writeByte(BASE64_URL_CHARS[((b0 << 4) & 63) | (b1 >> 4)]); | ||
jw.writeByte(BASE64_URL_CHARS[((b1 << 2) & 63) | (b2 >> 6)]); | ||
jw.writeByte(BASE64_URL_CHARS[b2 & 63]); | ||
} | ||
int leftOver = data.length - i; | ||
if (leftOver == 1) { | ||
int b0 = ((int) data[i]) & 0xFF; | ||
jw.writeByte(BASE64_URL_CHARS[b0 >> 2]); | ||
jw.writeByte(BASE64_URL_CHARS[(b0 << 4) & 63]); | ||
} else if (leftOver == 2) { | ||
int b0 = ((int) data[i]) & 0xFF; | ||
int b1 = ((int) data[i + 1]) & 0xFF; | ||
jw.writeByte(BASE64_URL_CHARS[b0 >> 2]); | ||
jw.writeByte(BASE64_URL_CHARS[((b0 << 4) & 63) | (b1 >> 4)]); | ||
jw.writeByte(BASE64_URL_CHARS[(b1 << 2) & 63]); | ||
} | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...core/src/test/java/co/elastic/apm/agent/report/serialize/Base64SerializationUtilTest.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,41 @@ | ||
package co.elastic.apm.agent.report.serialize; | ||
|
||
import com.dslplatform.json.DslJson; | ||
import com.dslplatform.json.JsonWriter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Base64; | ||
import java.util.Random; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class Base64SerializationUtilTest { | ||
|
||
@Test | ||
public void empty() { | ||
JsonWriter jw = new DslJson<>(new DslJson.Settings<>()).newWriter(); | ||
Base64SerializationUtils.writeBytesAsBase64UrlSafe(new byte[0], jw); | ||
assertThat(jw.size()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void randomInputs() { | ||
DslJson<Object> dslJson = new DslJson<>(new DslJson.Settings<>()); | ||
|
||
Base64.Encoder reference = Base64.getUrlEncoder().withoutPadding(); | ||
|
||
Random rnd = new Random(42); | ||
for (int i = 0; i < 100_000; i++) { | ||
int len = rnd.nextInt(31) + 1; | ||
byte[] data = new byte[len]; | ||
rnd.nextBytes(data); | ||
|
||
String expectedResult = reference.encodeToString(data); | ||
|
||
JsonWriter jw = dslJson.newWriter(); | ||
Base64SerializationUtils.writeBytesAsBase64UrlSafe(data, jw); | ||
|
||
assertThat(jw.toString()).isEqualTo(expectedResult); | ||
} | ||
} | ||
} |
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