-
Notifications
You must be signed in to change notification settings - Fork 337
Truncate CI Visibility meta tag values #11485
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import static datadog.communication.http.OkHttpUtils.gzippedMsgpackRequestBodyOf; | ||
| import static datadog.communication.http.OkHttpUtils.msgpackRequestBodyOf; | ||
| import static datadog.json.JsonMapper.toJson; | ||
| import static datadog.trace.common.writer.CiVisibilityMetaTruncation.truncate; | ||
|
|
||
| import datadog.communication.serialization.GrowableBuffer; | ||
| import datadog.communication.serialization.Writable; | ||
|
|
@@ -240,34 +241,34 @@ private void writeHeader() { | |
| headerWriter.startMap(10); | ||
| /* 2,1,1 */ | ||
| headerWriter.writeUTF8(ENV); | ||
| headerWriter.writeUTF8(wellKnownTags.getEnv()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getEnv())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For well-known tags that will never change I'd recommend creating your own CI-Vis holder class containing truncated well-known tags. That would be more efficient and could also help address the comment above, because you could ensure that any truncated well-known tag values stay as the more efficient I'd also suggest looking at the existing |
||
| /* 2,1,2 */ | ||
| headerWriter.writeUTF8(RUNTIME_ID); | ||
| headerWriter.writeUTF8(wellKnownTags.getRuntimeId()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getRuntimeId())); | ||
| /* 2,1,3 */ | ||
| headerWriter.writeUTF8(LANGUAGE); | ||
| headerWriter.writeUTF8(wellKnownTags.getLanguage()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getLanguage())); | ||
| /* 2,1,4 */ | ||
| headerWriter.writeUTF8(RUNTIME_NAME); | ||
| headerWriter.writeUTF8(wellKnownTags.getRuntimeName()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getRuntimeName())); | ||
| /* 2,1,5 */ | ||
| headerWriter.writeUTF8(RUNTIME_VENDOR); | ||
| headerWriter.writeUTF8(wellKnownTags.getRuntimeVendor()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getRuntimeVendor())); | ||
| /* 2,1,6 */ | ||
| headerWriter.writeUTF8(RUNTIME_VERSION); | ||
| headerWriter.writeUTF8(wellKnownTags.getRuntimeVersion()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getRuntimeVersion())); | ||
| /* 2,1,7 */ | ||
| headerWriter.writeUTF8(OS_ARCHITECTURE); | ||
| headerWriter.writeUTF8(wellKnownTags.getOsArch()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getOsArch())); | ||
| /* 2,1,8 */ | ||
| headerWriter.writeUTF8(OS_PLATFORM); | ||
| headerWriter.writeUTF8(wellKnownTags.getOsPlatform()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getOsPlatform())); | ||
| /* 2,1,9 */ | ||
| headerWriter.writeUTF8(OS_VERSION); | ||
| headerWriter.writeUTF8(wellKnownTags.getOsVersion()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getOsVersion())); | ||
| /* 2,1,10 */ | ||
| headerWriter.writeUTF8(TEST_IS_USER_PROVIDED_SERVICE); | ||
| headerWriter.writeUTF8(wellKnownTags.getIsUserProvidedService()); | ||
| headerWriter.writeUTF8(truncate(wellKnownTags.getIsUserProvidedService())); | ||
| /* 3 */ | ||
| headerWriter.writeUTF8(EVENTS); | ||
| headerWriter.startArray(eventCount); | ||
|
|
@@ -350,21 +351,21 @@ public void accept(Metadata metadata) { | |
| // we just need to be sure that the size is the same as the number of elements | ||
| for (Map.Entry<String, String> entry : metadata.getBaggage().entrySet()) { | ||
| writable.writeString(entry.getKey(), null); | ||
| writable.writeString(entry.getValue(), null); | ||
| writable.writeString(truncate(entry.getValue()), null); | ||
| } | ||
| if (null != metadata.getHttpStatusCode()) { | ||
| writable.writeUTF8(HTTP_STATUS); | ||
| writable.writeUTF8(metadata.getHttpStatusCode()); | ||
| writable.writeUTF8(truncate(metadata.getHttpStatusCode().toString())); | ||
| } | ||
| for (Map.Entry<String, Object> entry : tags.entrySet()) { | ||
| Object value = entry.getValue(); | ||
| if (!(value instanceof Number)) { | ||
| writable.writeString(entry.getKey(), null); | ||
| if (!(value instanceof Iterable)) { | ||
| writable.writeObjectString(value, null); | ||
| writable.writeString(truncate(String.valueOf(value)), null); | ||
| } else { | ||
| String serializedValue = toJson((Collection<String>) value); | ||
| writable.writeString(serializedValue, null); | ||
| writable.writeString(truncate(serializedValue), null); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package datadog.trace.common.writer; | ||
|
|
||
| public final class CiVisibilityMetaTruncation { | ||
| public static final int MAX_META_STRING_VALUE_LENGTH = 5000; | ||
|
|
||
| private CiVisibilityMetaTruncation() {} | ||
|
|
||
| public static String truncate(String value) { | ||
| if (value == null || value.length() <= MAX_META_STRING_VALUE_LENGTH) { | ||
| return value; | ||
| } | ||
| return value.substring(0, MAX_META_STRING_VALUE_LENGTH); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ import java.nio.ByteBuffer | |
| import java.nio.channels.WritableByteChannel | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.DD_MEASURED | ||
| import static datadog.trace.common.writer.CiVisibilityMetaTruncation.MAX_META_STRING_VALUE_LENGTH | ||
| import static datadog.trace.common.writer.CiVisibilityMetaTruncation.truncate | ||
| import static datadog.trace.common.writer.TraceGenerator.generateRandomSpan | ||
| import static datadog.trace.common.writer.TraceGenerator.generateRandomTraces | ||
| import static org.junit.jupiter.api.Assertions.assertEquals | ||
|
|
@@ -110,6 +112,56 @@ class CiTestCycleMapperV1PayloadTest extends DDSpecification { | |
| assert spanContent.containsKey("parent_id") | ||
| } | ||
|
|
||
| def "truncates meta string values and preserves metrics and top level ids"() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This adds new Spock/Groovy coverage in a file under Useful? React with 👍 / 👎. |
||
| setup: | ||
| String longValue = "a" * (MAX_META_STRING_VALUE_LENGTH + 1) | ||
| String exactValue = "b" * MAX_META_STRING_VALUE_LENGTH | ||
| def span = generateRandomSpan(InternalSpanTypes.TEST, [ | ||
| (Tags.TEST_SESSION_ID): DDTraceId.from(123), | ||
| (Tags.TEST_MODULE_ID) : 456, | ||
| (Tags.TEST_SUITE_ID) : 789, | ||
| "custom.tag" : longValue, | ||
| "exact.tag" : exactValue, | ||
| "custom.metric" : 42, | ||
| ]) | ||
|
|
||
| when: | ||
| Map<String, Object> deserializedSpan = whenASpanIsWritten(span) | ||
|
|
||
| then: | ||
| verifyTopLevelTags(deserializedSpan, DDTraceId.from(123), 456, 789) | ||
|
|
||
| def spanContent = (Map<String, Object>) deserializedSpan.get("content") | ||
| def deserializedMetrics = (Map<String, Object>) spanContent.get("metrics") | ||
| def deserializedMeta = (Map<String, Object>) spanContent.get("meta") | ||
|
|
||
| assert deserializedMeta.get("custom.tag") == longValue.substring(0, MAX_META_STRING_VALUE_LENGTH) | ||
| assert deserializedMeta.get("custom.tag").length() == MAX_META_STRING_VALUE_LENGTH | ||
| assert deserializedMeta.get("exact.tag") == exactValue | ||
| assert deserializedMetrics.get("custom.metric") == 42 | ||
| } | ||
|
|
||
| def "truncates payload metadata values"() { | ||
| setup: | ||
| String longValue = "m" * (MAX_META_STRING_VALUE_LENGTH + 1) | ||
| CiVisibilityWellKnownTags wellKnownTags = new CiVisibilityWellKnownTags( | ||
| longValue, longValue, longValue, | ||
| longValue, longValue, longValue, | ||
| longValue, longValue, longValue, longValue) | ||
| CiTestCycleMapperV1 mapper = new CiTestCycleMapperV1(wellKnownTags, false) | ||
| List<List<TraceGenerator.PojoSpan>> traces = Collections.singletonList( | ||
| Collections.singletonList(generateRandomSpan(InternalSpanTypes.TEST, Collections.emptyMap()))) | ||
| PayloadVerifier verifier = new PayloadVerifier(wellKnownTags, traces, mapper) | ||
| MsgPackWriter packer = new MsgPackWriter(new FlushingBuffer(100 << 10, verifier)) | ||
|
|
||
| when: | ||
| packer.format(traces.get(0), mapper) | ||
| packer.flush() | ||
|
|
||
| then: | ||
| verifier.verifyTracesConsumed() | ||
| } | ||
|
|
||
| def "verify test_suite_end event is written correctly"() { | ||
| setup: | ||
| def span = generateRandomSpan(InternalSpanTypes.TEST_SUITE_END, [ | ||
|
|
@@ -275,25 +327,25 @@ class CiTestCycleMapperV1PayloadTest extends DDSpecification { | |
|
|
||
| assertEquals(10, unpacker.unpackMapHeader()) | ||
| assertEquals("env", unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.env as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.env as String), unpacker.unpackString()) | ||
| assertEquals("runtime-id", unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.runtimeId as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.runtimeId as String), unpacker.unpackString()) | ||
| assertEquals("language", unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.language as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.language as String), unpacker.unpackString()) | ||
| assertEquals(Tags.RUNTIME_NAME, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.runtimeName as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.runtimeName as String), unpacker.unpackString()) | ||
| assertEquals(Tags.RUNTIME_VENDOR, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.runtimeVendor as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.runtimeVendor as String), unpacker.unpackString()) | ||
| assertEquals(Tags.RUNTIME_VERSION, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.runtimeVersion as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.runtimeVersion as String), unpacker.unpackString()) | ||
| assertEquals(Tags.OS_ARCHITECTURE, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.osArch as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.osArch as String), unpacker.unpackString()) | ||
| assertEquals(Tags.OS_PLATFORM, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.osPlatform as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.osPlatform as String), unpacker.unpackString()) | ||
| assertEquals(Tags.OS_VERSION, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.osVersion as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.osVersion as String), unpacker.unpackString()) | ||
| assertEquals(DDTags.TEST_IS_USER_PROVIDED_SERVICE, unpacker.unpackString()) | ||
| assertEquals(wellKnownTags.isUserProvidedService as String, unpacker.unpackString()) | ||
| assertEquals(truncate(wellKnownTags.isUserProvidedService as String), unpacker.unpackString()) | ||
|
|
||
| assertEquals("events", unpacker.unpackString()) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this header path,
CiVisibilityWellKnownTags#getEnv()and the following well-known tag getters returnUTF8BytesString, but the newtruncatehelper only acceptsString. Java has to resolvetruncate(...)before it can callwriteUTF8, so this makesdd-trace-corefail to compile for the CI test-cycle mapper; convert these values to strings first or make the helper acceptCharSequence.Useful? React with 👍 / 👎.