From b4716c4c61f4835007f55c21b6f426f862189168 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 18 Jan 2021 15:13:45 +0100 Subject: [PATCH 1/4] added beacon-processor for bmr beacon --- .../processor/CsvExpanderBeaconProcessor.java | 28 ++++---- .../CsvKeyValueExpanderBeaconProcessor.java | 53 +++++++++++++++ .../CsvExpanderBeaconProcessorTest.java | 27 ++++---- ...svKeyValueExpanderBeaconProcessorTest.java | 65 +++++++++++++++++++ 4 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessor.java create mode 100644 components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessorTest.java diff --git a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java index 5c561a50f4..5bbf7c8490 100644 --- a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java +++ b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java @@ -7,9 +7,10 @@ /** * Processor to expand comma separated values. The expanded values will be available at a new attribute - * key in the beacon. + * indexed key in the beacon. *

- * Example: The attribute t_other=t_domloaded|437 will result in t_other.t_domloaded: 437 + * Example: The attribute rt.bmr=37,5 will result in rt.bmr.0: 37, rt.bmr.1: 5 + * and rt.bmr.sum: 42 */ @Component public class CsvExpanderBeaconProcessor implements BeaconProcessor { @@ -17,35 +18,28 @@ public class CsvExpanderBeaconProcessor implements BeaconProcessor { /** * The target key of the attribute to expand. */ - private static final String ATTRIBUTE_KEY = "t_other"; + private static final String ATTRIBUTE_KEY = "rt.bmr"; /** * The regex (character) used to separate individual value groups. */ private static final String GROUP_SEPARATOR = ","; - /** - * The regex (character) used to separate key and value. - */ - private static final String KEY_VALUE_SEPARATOR = "\\|"; - @Override public Beacon process(Beacon beacon) { String targetAttribute = beacon.get(ATTRIBUTE_KEY); - if (targetAttribute != null) { + if (targetAttribute != null && !targetAttribute.equals("")) { + int sum = 0; String[] attributes = targetAttribute.split(GROUP_SEPARATOR); - for (String attribute : attributes) { - String[] splitAttributes = attribute.split(KEY_VALUE_SEPARATOR); - - if (splitAttributes.length == 2) { - String resultKey = ATTRIBUTE_KEY + "." + splitAttributes[0]; - String resultValue = splitAttributes[1]; + for (int i = 0; i < attributes.length; i++) { + String resultKey = ATTRIBUTE_KEY + "." + i; + beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i])); - beacon = beacon.merge(Collections.singletonMap(resultKey, resultValue)); - } + sum += Integer.parseInt(attributes[i]); } + beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); } return beacon; diff --git a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessor.java b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessor.java new file mode 100644 index 0000000000..95647fe9b5 --- /dev/null +++ b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessor.java @@ -0,0 +1,53 @@ +package rocks.inspectit.oce.eum.server.beacon.processor; + +import org.springframework.stereotype.Component; +import rocks.inspectit.oce.eum.server.beacon.Beacon; + +import java.util.Collections; + +/** + * Processor to expand comma separated key values pairs. The expanded values will be available at a new attribute + * key in the beacon. + *

+ * Example: The attribute t_other=t_domloaded|437 will result in t_other.t_domloaded: 437 + */ +@Component +public class CsvKeyValueExpanderBeaconProcessor implements BeaconProcessor { + + /** + * The target key of the attribute to expand. + */ + private static final String ATTRIBUTE_KEY = "t_other"; + + /** + * The regex (character) used to separate individual value groups. + */ + private static final String GROUP_SEPARATOR = ","; + + /** + * The regex (character) used to separate key and value. + */ + private static final String KEY_VALUE_SEPARATOR = "\\|"; + + @Override + public Beacon process(Beacon beacon) { + String targetAttribute = beacon.get(ATTRIBUTE_KEY); + + if (targetAttribute != null) { + String[] attributes = targetAttribute.split(GROUP_SEPARATOR); + + for (String attribute : attributes) { + String[] splitAttributes = attribute.split(KEY_VALUE_SEPARATOR); + + if (splitAttributes.length == 2) { + String resultKey = ATTRIBUTE_KEY + "." + splitAttributes[0]; + String resultValue = splitAttributes[1]; + + beacon = beacon.merge(Collections.singletonMap(resultKey, resultValue)); + } + } + } + + return beacon; + } +} diff --git a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java index 7464fb22b4..ba78d7c8b4 100644 --- a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java +++ b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java @@ -22,44 +22,39 @@ class CsvExpanderBeaconProcessorTest { public class Process { @Test - public void noTOtherAttribute() { - Beacon beacon = Beacon.of(Collections.singletonMap("key", "value")); + public void noRtBmrAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("key", "")); Beacon result = processor.process(beacon); - assertThat(result.toMap()).containsOnly(entry("key", "value")); + assertThat(result.toMap()).containsOnly(entry("key", "")); } @Test - public void tOtherWithoutPatternAttribute() { - Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value")); + public void rtBmrWithoutPatternAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "")); Beacon result = processor.process(beacon); - assertThat(result.toMap()).containsOnly(entry("t_other", "value")); + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "")); } @Test - public void tOtherWithPatternAttribute() { - Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value|123")); + public void rtBmrWithPatternAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123")); Beacon result = processor.process(beacon); - assertThat(result.toMap()).containsOnly( - entry("t_other", "value|123"), - entry("t_other.value", "123")); + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "123"), entry("rt.bmr.0", "123"), entry("rt.bmr.sum", "123")); } @Test public void tOtherWithMultiplePatternAttributes() { - Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value|123,another_value|321")); + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123,321")); Beacon result = processor.process(beacon); - assertThat(result.toMap()).containsOnly( - entry("t_other", "value|123,another_value|321"), - entry("t_other.value", "123"), - entry("t_other.another_value", "321")); + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "123,321"), entry("rt.bmr.0", "123"), entry("rt.bmr.1", "321"), entry("rt.bmr.sum", "444")); } } } \ No newline at end of file diff --git a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessorTest.java b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessorTest.java new file mode 100644 index 0000000000..6b5152d9f5 --- /dev/null +++ b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvKeyValueExpanderBeaconProcessorTest.java @@ -0,0 +1,65 @@ +package rocks.inspectit.oce.eum.server.beacon.processor; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +import rocks.inspectit.oce.eum.server.beacon.Beacon; + +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +@ExtendWith(MockitoExtension.class) +class CsvKeyValueExpanderBeaconProcessorTest { + + @InjectMocks + private CsvKeyValueExpanderBeaconProcessor processor; + + @Nested + public class Process { + + @Test + public void noTOtherAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("key", "value")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("key", "value")); + } + + @Test + public void tOtherWithoutPatternAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("t_other", "value")); + } + + @Test + public void tOtherWithPatternAttribute() { + Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value|123")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly( + entry("t_other", "value|123"), + entry("t_other.value", "123")); + } + + @Test + public void tOtherWithMultiplePatternAttributes() { + Beacon beacon = Beacon.of(Collections.singletonMap("t_other", "value|123,another_value|321")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly( + entry("t_other", "value|123,another_value|321"), + entry("t_other.value", "123"), + entry("t_other.another_value", "321")); + } + } +} \ No newline at end of file From 7c7bb075a222b9716c436409df2ef0f910dc1f67 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 19 Jan 2021 09:17:28 +0100 Subject: [PATCH 2/4] code improved according to PR comments and more tests added --- .../processor/CsvExpanderBeaconProcessor.java | 17 +++++++--- .../CsvExpanderBeaconProcessorTest.java | 33 +++++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java index 5bbf7c8490..480f83b1df 100644 --- a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java +++ b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java @@ -1,5 +1,7 @@ package rocks.inspectit.oce.eum.server.beacon.processor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import rocks.inspectit.oce.eum.server.beacon.Beacon; @@ -12,6 +14,7 @@ * Example: The attribute rt.bmr=37,5 will result in rt.bmr.0: 37, rt.bmr.1: 5 * and rt.bmr.sum: 42 */ +@Slf4j @Component public class CsvExpanderBeaconProcessor implements BeaconProcessor { @@ -29,17 +32,23 @@ public class CsvExpanderBeaconProcessor implements BeaconProcessor { public Beacon process(Beacon beacon) { String targetAttribute = beacon.get(ATTRIBUTE_KEY); - if (targetAttribute != null && !targetAttribute.equals("")) { + if (StringUtils.isNoneBlank(targetAttribute)) { int sum = 0; String[] attributes = targetAttribute.split(GROUP_SEPARATOR); for (int i = 0; i < attributes.length; i++) { String resultKey = ATTRIBUTE_KEY + "." + i; - beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i])); - sum += Integer.parseInt(attributes[i]); + try { + sum += Integer.parseInt(attributes[i]); + beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i])); + } catch (Exception e) { + log.error("Error parsing the value <'{}'>: invalid number.", attributes[i]); + } + } + if (sum != 0) { + beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); } - beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); } return beacon; diff --git a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java index ba78d7c8b4..0651b14091 100644 --- a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java +++ b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java @@ -31,7 +31,7 @@ public void noRtBmrAttribute() { } @Test - public void rtBmrWithoutPatternAttribute() { + public void rtBmrWithoutValues() { Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "")); Beacon result = processor.process(beacon); @@ -40,7 +40,7 @@ public void rtBmrWithoutPatternAttribute() { } @Test - public void rtBmrWithPatternAttribute() { + public void rtBmrWithValue() { Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123")); Beacon result = processor.process(beacon); @@ -49,12 +49,39 @@ public void rtBmrWithPatternAttribute() { } @Test - public void tOtherWithMultiplePatternAttributes() { + public void rtBmrWithInvalidValue() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "foo")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "foo")); + } + + @Test + public void rtBmrWithMultipleValues() { Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123,321")); Beacon result = processor.process(beacon); assertThat(result.toMap()).containsOnly(entry("rt.bmr", "123,321"), entry("rt.bmr.0", "123"), entry("rt.bmr.1", "321"), entry("rt.bmr.sum", "444")); } + + @Test + public void rtBmrMultipleValuesWithOneEmptyValue() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123,,321")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "123,,321"), entry("rt.bmr.0", "123"), entry("rt.bmr.2", "321"), entry("rt.bmr.sum", "444")); + } + + @Test + public void rtBmrMultipleValuesWithOneInvalidValue() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123,bar,321")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "123,bar,321"), entry("rt.bmr.0", "123"), entry("rt.bmr.2", "321"), entry("rt.bmr.sum", "444")); + } } } \ No newline at end of file From 8f3ba5b836a9ded9cf9437b794222edfc42696b1 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 20 Jan 2021 08:19:04 +0100 Subject: [PATCH 3/4] code improved according to PR comments --- .../processor/CsvExpanderBeaconProcessor.java | 5 +---- .../CsvExpanderBeaconProcessorTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java index 480f83b1df..2b29fe8493 100644 --- a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java +++ b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java @@ -42,15 +42,12 @@ public Beacon process(Beacon beacon) { try { sum += Integer.parseInt(attributes[i]); beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i])); + beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); } catch (Exception e) { log.error("Error parsing the value <'{}'>: invalid number.", attributes[i]); } } - if (sum != 0) { - beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); - } } - return beacon; } } diff --git a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java index 0651b14091..24cfba5cae 100644 --- a/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java +++ b/components/inspectit-ocelot-eum-server/src/test/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessorTest.java @@ -57,6 +57,24 @@ public void rtBmrWithInvalidValue() { assertThat(result.toMap()).containsOnly(entry("rt.bmr", "foo")); } + @Test + public void rtBmrWithZeroValue() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "0")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "0"), entry("rt.bmr.0", "0"), entry("rt.bmr.sum", "0")); + } + + @Test + public void rtBmrWithZeroSum() { + Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "0,0")); + + Beacon result = processor.process(beacon); + + assertThat(result.toMap()).containsOnly(entry("rt.bmr", "0,0"), entry("rt.bmr.0", "0"), entry("rt.bmr.1", "0"), entry("rt.bmr.sum", "0")); + } + @Test public void rtBmrWithMultipleValues() { Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "123,321")); From 9a148cf7d4b6ff2b370f64ee77fbdcadb107d4f3 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 20 Jan 2021 09:07:40 +0100 Subject: [PATCH 4/4] added isSum Flag for 'abc' case --- .../server/beacon/processor/CsvExpanderBeaconProcessor.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java index 2b29fe8493..655c472b8a 100644 --- a/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java +++ b/components/inspectit-ocelot-eum-server/src/main/java/rocks/inspectit/oce/eum/server/beacon/processor/CsvExpanderBeaconProcessor.java @@ -34,6 +34,7 @@ public Beacon process(Beacon beacon) { if (StringUtils.isNoneBlank(targetAttribute)) { int sum = 0; + boolean isSum = false; String[] attributes = targetAttribute.split(GROUP_SEPARATOR); for (int i = 0; i < attributes.length; i++) { @@ -41,12 +42,15 @@ public Beacon process(Beacon beacon) { try { sum += Integer.parseInt(attributes[i]); + isSum = true; beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i])); - beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); } catch (Exception e) { log.error("Error parsing the value <'{}'>: invalid number.", attributes[i]); } } + if (isSum) { + beacon = beacon.merge(Collections.singletonMap(ATTRIBUTE_KEY + ".sum", String.valueOf(sum))); + } } return beacon; }