Skip to content

Commit

Permalink
Make ecs@mappings work with OTel attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Aug 5, 2024
1 parent f2952a8 commit 1a1a018
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doc_values": false
},
"path_match": [
"event.original"
"*event.original"
]
}
},
Expand Down Expand Up @@ -128,9 +128,9 @@
"*file.path",
"*file.target_path",
"*os.full",
"email.subject",
"vulnerability.description",
"user_agent.original"
"*email.subject",
"*vulnerability.description",
"*user_agent.original"
],
"unmatch_mapping_type": "object"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ public void testFlattenedFields() throws IOException {
verifyEcsMappings(indexName);
}

public void testFlattenedFieldsWithinAttributes() throws IOException {
String indexName = "test-flattened-attributes";
createTestIndex(indexName);
Map<String, Object> flattenedFieldsMap = createTestDocument(true);
indexDocument(indexName, Map.of("attributes", flattenedFieldsMap));
verifyEcsMappings(indexName, "attributes.");
}

public void testFlattenedFieldsWithinResourceAttributes() throws IOException {
String indexName = "test-flattened-attributes";
createTestIndex(indexName);
Map<String, Object> flattenedFieldsMap = createTestDocument(true);
indexDocument(indexName, Map.of("resource.attributes", flattenedFieldsMap));
verifyEcsMappings(indexName, "resource.attributes.");
}

public void testFlattenedFieldsWithoutSubobjects() throws IOException {
String indexName = "test_flattened_fields_subobjects_false";
createTestIndex(indexName, Map.of("subobjects", false));
Expand Down Expand Up @@ -394,12 +410,19 @@ private void processRawMappingsSubtree(
}

private void verifyEcsMappings(String indexName) throws IOException {
verifyEcsMappings(indexName, "");
}

private void verifyEcsMappings(String indexName, String fieldPrefix) throws IOException {
final Map<String, Object> rawMappings = getMappings(indexName);
final Map<String, Map<String, Object>> flatFieldMappings = new HashMap<>();
final Map<String, Map<String, Object>> flatMultiFieldsMappings = new HashMap<>();
processRawMappingsSubtree(rawMappings, flatFieldMappings, flatMultiFieldsMappings, "");

Map<String, Map<String, Object>> shallowFieldMapCopy = new HashMap<>(ecsFlatFieldDefinitions);
Map<String, Map<String, Object>> shallowFieldMapCopy = ecsFlatFieldDefinitions.entrySet()
.stream()
.collect(Collectors.toMap(e -> fieldPrefix + e.getKey(), Map.Entry::getValue));

logger.info("Testing mapping of {} ECS fields", shallowFieldMapCopy.size());
List<String> nonEcsFields = new ArrayList<>();
Map<String, String> fieldToWrongMappingType = new HashMap<>();
Expand All @@ -421,7 +444,9 @@ private void verifyEcsMappings(String indexName) throws IOException {
}
});

Map<String, Map<String, Object>> shallowMultiFieldMapCopy = new HashMap<>(ecsFlatMultiFieldDefinitions);
Map<String, Map<String, Object>> shallowMultiFieldMapCopy = ecsFlatMultiFieldDefinitions.entrySet()
.stream()
.collect(Collectors.toMap(e -> fieldPrefix + e.getKey(), Map.Entry::getValue));
logger.info("Testing mapping of {} ECS multi-fields", shallowMultiFieldMapCopy.size());
flatMultiFieldsMappings.forEach((fieldName, actualMappings) -> {
Map<String, Object> expectedMultiFieldMappings = shallowMultiFieldMapCopy.remove(fieldName);
Expand Down

0 comments on commit 1a1a018

Please sign in to comment.