Skip to content

Commit

Permalink
Introduce string list attribute value getter method (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
suresh-prakash authored Dec 15, 2022
1 parent e583ecd commit 4c4ca17
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.hypertrace.core.datamodel.shared;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.hypertrace.core.datamodel.shared.AvroBuilderCache.fastNewBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.avro.reflect.Nullable;
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Attributes;
Expand All @@ -20,6 +28,7 @@
* Span.
*/
public class SpanAttributeUtils {
private static final ObjectMapper MAPPER = new ObjectMapper();

public static boolean isLeafSpan(StructuredTraceGraph structuredTraceGraph, Event event) {
List<Event> childEvents = structuredTraceGraph.getChildrenEvents(event);
Expand Down Expand Up @@ -106,6 +115,33 @@ public static boolean getBooleanAttribute(Event event, String attributeKey) {
return Boolean.parseBoolean(value.getValue());
}

public static List<String> getStringListAttribute(final Event event, final String attributeKey) {
final AttributeValue value = getAttributeValue(event, attributeKey);
if (value == null) {
return emptyList();
}

if (value.getValueList() != null && !value.getValueList().isEmpty()) {
return value.getValueList();
}

final String jsonValue = value.getValue();

try {
final JsonNode node = MAPPER.readTree(jsonValue);
if (node.isArray()) {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(node.elements(), Spliterator.ORDERED), false)
.map(JsonNode::asText)
.collect(toUnmodifiableList());
}

return List.of(node.asText());
} catch (final JsonProcessingException e) {
return emptyList();
}
}

/**
* Utility method to get a single value from one of the given attributes. The first attribute
* given in the list with non-null value will be used.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.hypertrace.core.datamodel.shared;

import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Map;
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Attributes;
import org.hypertrace.core.datamodel.Event;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class SpanAttributeUtilsTest {
@ParameterizedTest
@ValueSource(strings = {"\"Valid String\"", "[\"Valid String\"]"})
void testGetStringListAttribute(final String attributeValue) {
final Event event = mock(Event.class);
when(event.getAttributes())
.thenReturn(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"attributeKey",
AttributeValue.newBuilder().setValue(attributeValue).build()))
.build());
final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey");
assertEquals(List.of("Valid String"), result);
}

@ParameterizedTest
@ValueSource(strings = {"Invalid JSON", "[]"})
void testGetStringListAttribute_emptyList(final String attributeValue) {
final Event event = mock(Event.class);
when(event.getAttributes())
.thenReturn(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"attributeKey",
AttributeValue.newBuilder().setValue(attributeValue).build()))
.build());
final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey");
assertEquals(emptyList(), result);
}

@Test
void testGetStringListAttribute_multiValuedList() {
final Event event = mock(Event.class);
when(event.getAttributes())
.thenReturn(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"attributeKey",
AttributeValue.newBuilder()
.setValue("[\"value1\", \"value2\", 3]")
.build()))
.build());
final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey");
assertEquals(List.of("value1", "value2", "3"), result);
}
}

0 comments on commit 4c4ca17

Please sign in to comment.