Skip to content

Commit c66663b

Browse files
committed
Apply review comments
1 parent b2dd61d commit c66663b

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

modules/ingest-otel/src/main/java/org/elasticsearch/ingest/otel/NormalizeToOTelProcessor.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ public IngestDocument execute(IngestDocument document) {
114114
if (keepKey.equals("@timestamp")) {
115115
continue;
116116
}
117-
Object value = source.remove(keepKey);
118-
if (value != null) {
119-
newAttributes.put(keepKey, value);
117+
if (source.containsKey(keepKey)) {
118+
newAttributes.put(keepKey, source.remove(keepKey));
120119
}
121120
}
122121

@@ -147,8 +146,8 @@ public IngestDocument execute(IngestDocument document) {
147146
}
148147

149148
// Flatten attributes
150-
source.replace(ATTRIBUTES_KEY, Maps.flatten(newAttributes, false, false));
151-
newResource.replace(ATTRIBUTES_KEY, Maps.flatten(newResourceAttributes, false, false));
149+
source.put(ATTRIBUTES_KEY, Maps.flatten(newAttributes, false, false));
150+
newResource.put(ATTRIBUTES_KEY, Maps.flatten(newResourceAttributes, false, false));
152151

153152
return document;
154153
}
@@ -223,9 +222,12 @@ static boolean isOTelDocument(Map<String, Object> source) {
223222
*/
224223
static void renameSpecialKeys(IngestDocument document) {
225224
RENAME_KEYS.forEach((nonOtelName, otelName) -> {
225+
boolean fieldExists = false;
226+
Object value = null;
226227
// first look assuming dot notation for nested fields
227-
Object value = document.getFieldValue(nonOtelName, Object.class, true);
228-
if (value != null) {
228+
if (document.hasField(nonOtelName)) {
229+
fieldExists = true;
230+
value = document.getFieldValue(nonOtelName, Object.class, true);
229231
document.removeField(nonOtelName);
230232
// recursively remove empty parent fields
231233
int lastDot = nonOtelName.lastIndexOf('.');
@@ -243,9 +245,13 @@ static void renameSpecialKeys(IngestDocument document) {
243245
}
244246
} else if (nonOtelName.contains(".")) {
245247
// look for dotted field names
246-
value = document.getSource().remove(nonOtelName);
248+
Map<String, Object> source = document.getSource();
249+
if (source.containsKey(nonOtelName)) {
250+
fieldExists = true;
251+
value = source.remove(nonOtelName);
252+
}
247253
}
248-
if (value != null) {
254+
if (fieldExists) {
249255
document.setFieldValue(otelName, value);
250256
}
251257
});

modules/ingest-otel/src/test/java/org/elasticsearch/ingest/otel/NormalizeToOTelProcessorTests.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.Map;
1818

1919
import static java.util.Map.entry;
20-
import static org.hamcrest.Matchers.sameInstance;
2120

2221
public class NormalizeToOTelProcessorTests extends ESTestCase {
2322

@@ -121,8 +120,10 @@ public void testExecute_validOTelDocument() {
121120
entry("key1", "value1")
122121
);
123122
IngestDocument document = new IngestDocument("index", "id", 1, null, null, source);
123+
Map<String, Object> shallowCopy = new HashMap<>(source);
124124
processor.execute(document);
125-
assertThat(source, sameInstance(document.getSource()));
125+
// verify that top level keys are not moved when processing a valid OTel document
126+
assertEquals(shallowCopy, document.getSource());
126127
}
127128

128129
public void testExecute_nonOTelDocument() {
@@ -325,6 +326,35 @@ public void testExecute_moveToAttributeMaps() {
325326
assertFalse(source.containsKey("service"));
326327
}
327328

329+
public void testKeepNullValues() {
330+
Map<String, Object> source = new HashMap<>();
331+
Map<String, Object> span = new HashMap<>();
332+
span.put("id", null);
333+
source.put("span", span);
334+
source.put("log.level", null);
335+
source.put("trace_id", null);
336+
source.put("foo", null);
337+
source.put("agent.name", null);
338+
IngestDocument document = new IngestDocument("index", "id", 1, null, null, source);
339+
340+
processor.execute(document);
341+
342+
assertFalse(source.containsKey("span"));
343+
assertTrue(source.containsKey("span_id"));
344+
assertNull(source.get("span_id"));
345+
assertFalse(source.containsKey("log"));
346+
assertTrue(source.containsKey("severity_text"));
347+
assertNull(source.get("severity_text"));
348+
assertFalse(source.containsKey("trace_id"));
349+
Map<String, Object> expectedAttributes = new HashMap<>();
350+
expectedAttributes.put("foo", null);
351+
expectedAttributes.put("trace_id", null);
352+
assertEquals(expectedAttributes, get(source, "attributes"));
353+
Map<String, Object> expectedResourceAttributes = new HashMap<>();
354+
expectedResourceAttributes.put("agent.name", null);
355+
assertEquals(expectedResourceAttributes, get(get(source, "resource"), "attributes"));
356+
}
357+
328358
public void testExecute_deepFlattening() {
329359
Map<String, Object> source = new HashMap<>();
330360
Map<String, Object> nestedAgent = new HashMap<>();

0 commit comments

Comments
 (0)