Skip to content

Commit

Permalink
deal with muliple tags with the same key (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
vthacker authored Mar 26, 2024
1 parent cc194a2 commit c95ed27
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ public static Trace.Span fromIngestDocument(
sourceAndMetadata.remove(IngestDocument.Metadata.ID.getFieldName());
sourceAndMetadata.remove(IngestDocument.Metadata.INDEX.getFieldName());

sourceAndMetadata.forEach(
(key, value) -> spanBuilder.addTags(SpanFormatter.convertKVtoProto(key, value, schema)));
spanBuilder.addTags(
Trace.KeyValue.newBuilder()
.setKey(SERVICE_NAME_KEY)
.setVType(Trace.ValueType.STRING)
.setVStr(index)
.build());
boolean tagsContainServiceName = false;
for (Map.Entry<String, Object> kv : sourceAndMetadata.entrySet()) {
if (!tagsContainServiceName && kv.getKey().equals(SERVICE_NAME_KEY)) {
tagsContainServiceName = true;
}
spanBuilder.addTags(SpanFormatter.convertKVtoProto(kv.getKey(), kv.getValue(), schema));
}
if (!tagsContainServiceName) {
spanBuilder.addTags(
Trace.KeyValue.newBuilder()
.setKey(SERVICE_NAME_KEY)
.setVType(Trace.ValueType.STRING)
.setFieldType(Schema.SchemaFieldType.KEYWORD)
.setVStr(index)
.build());
}

return spanBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ public Document fromMessage(Trace.Span message) throws JsonProcessingException {
Map<String, Trace.KeyValue> tags =
message.getTagsList().stream()
.map(keyValue -> Map.entry(keyValue.getKey(), keyValue))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(
Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (firstKV, dupKV) -> firstKV));

// This should just be top level Trace.Span fields. This is error prone - what if type is
// not a string?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void testRaiseErrorOnConflictingReservedField() {
1, "Test message", Instant.now(), List.of(hostField, tagField))))
.isInstanceOf(FieldDefMismatchException.class);

assertThat(docBuilder.getSchema().size()).isEqualTo(19);
assertThat(docBuilder.getSchema().size()).isEqualTo(18);
assertThat(docBuilder.getSchema().keySet()).contains(hostNameField);
assertThat(docBuilder.getSchema().get(hostNameField).fieldType).isEqualTo(FieldType.STRING);
assertThat(MetricsUtil.getCount(DROP_FIELDS_COUNTER, meterRegistry)).isZero();
Expand Down Expand Up @@ -206,7 +206,7 @@ public void testRaiseErrorOnConflictingReservedFieldWithoutFullTextSearch() {
1, "Test message", Instant.now(), List.of(hostField, tagField))))
.isInstanceOf(FieldDefMismatchException.class);

assertThat(docBuilder.getSchema().size()).isEqualTo(18);
assertThat(docBuilder.getSchema().size()).isEqualTo(17);
assertThat(docBuilder.getSchema().keySet()).contains(hostNameField);
assertThat(docBuilder.getSchema().get(hostNameField).fieldType).isEqualTo(FieldType.STRING);
assertThat(MetricsUtil.getCount(DROP_FIELDS_COUNTER, meterRegistry)).isZero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,27 @@ public void testDuplicateFieldAsTag() {

logStore.addMessage(span);
assertThat(getCount(MESSAGES_FAILED_COUNTER, meterRegistry)).isEqualTo(0);

// duplicate tags
span =
Trace.Span.newBuilder()
.setName("service1")
.setId(ByteString.copyFrom("123".getBytes()))
.addTags(
Trace.KeyValue.newBuilder()
.setKey("tag1")
.setVStr("value1")
.setVType(Trace.ValueType.STRING)
.setFieldType(Schema.SchemaFieldType.KEYWORD))
.addTags(
Trace.KeyValue.newBuilder()
.setKey("tag1")
.setVStr("value1")
.setVType(Trace.ValueType.STRING)
.setFieldType(Schema.SchemaFieldType.KEYWORD))
.build();

logStore.addMessage(span);
assertThat(getCount(MESSAGES_FAILED_COUNTER, meterRegistry)).isEqualTo(0);
}
}

0 comments on commit c95ed27

Please sign in to comment.