Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closes #966 - Resolve 'rt.bmr' data send by Boomerang plugin RT #970

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
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;

import java.util.Collections;

/**
* 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.
* <p>
* Example: The attribute <code>t_other=t_domloaded|437</code> will result in <code>t_other.t_domloaded: 437</code>
* Example: The attribute <code>rt.bmr=37,5</code> will result in <code>rt.bmr.0: 37</code>, <code>rt.bmr.1: 5</code>
* and <code>rt.bmr.sum: 42</code>
*/
@Slf4j
@Component
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 (StringUtils.isNoneBlank(targetAttribute)) {
int sum = 0;
boolean isSum = false;
String[] attributes = targetAttribute.split(GROUP_SEPARATOR);

for (String attribute : attributes) {
String[] splitAttributes = attribute.split(KEY_VALUE_SEPARATOR);
for (int i = 0; i < attributes.length; i++) {
String resultKey = ATTRIBUTE_KEY + "." + i;

if (splitAttributes.length == 2) {
String resultKey = ATTRIBUTE_KEY + "." + splitAttributes[0];
String resultValue = splitAttributes[1];

beacon = beacon.merge(Collections.singletonMap(resultKey, resultValue));
try {
sum += Integer.parseInt(attributes[i]);
isSum = true;
beacon = beacon.merge(Collections.singletonMap(resultKey, attributes[i]));
} 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;
}
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Example: The attribute <code>t_other=t_domloaded|437</code> will result in <code>t_other.t_domloaded: 437</code>
*/
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,84 @@ 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 rtBmrWithoutValues() {
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 rtBmrWithValue() {
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"));
public void rtBmrWithInvalidValue() {
Beacon beacon = Beacon.of(Collections.singletonMap("rt.bmr", "foo"));

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", "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"));

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"));
}
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}