Skip to content

Commit

Permalink
added util method (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarthak Singhal <[email protected]>
  • Loading branch information
sarthak77 and Sarthak Singhal authored Nov 11, 2022
1 parent 63e135c commit e583ecd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hypertrace.core.datamodel.shared;

import java.util.List;
import java.util.Optional;
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Event;
Expand Down Expand Up @@ -31,6 +32,23 @@ public static String getStringAttribute(StructuredTrace trace, String attribute)
return value.getValue();
}

public static List<String> getListAttribute(StructuredTrace trace, String attribute) {
if (trace.getAttributes() == null) {
return null;
}

if (trace.getAttributes().getAttributeMap() == null) {
return null;
}

AttributeValue value = trace.getAttributes().getAttributeMap().get(attribute);
if (value == null) {
return null;
}

return value.getValueList();
}

public static Optional<String> getAttributeValueIncludeSearchInSpans(
StructuredTrace trace, String key) {
if (trace == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.hypertrace.core.datamodel.shared;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
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.StructuredTrace;
import org.junit.jupiter.api.Test;

public class TraceAttributeUtilsTest {

@Test
public void getListAttributeTest() {
StructuredTrace mockTrace = mock(StructuredTrace.class);
assertNull(TraceAttributeUtils.getListAttribute(mockTrace, "someattribute"));

when(mockTrace.getAttributes()).thenReturn(Attributes.newBuilder().build());
assertNull(TraceAttributeUtils.getListAttribute(mockTrace, "someattribute"));

Map<String, AttributeValue> attributeMap = new HashMap<>();
when(mockTrace.getAttributes())
.thenReturn(Attributes.newBuilder().setAttributeMap(attributeMap).build());
assertNull(TraceAttributeUtils.getListAttribute(mockTrace, "someattribute"));

attributeMap.put(
"someattribute",
AttributeValue.newBuilder().setValueList(List.of("somevalue1", "somevalue2")).build());
assertEquals(
List.of("somevalue1", "somevalue2"),
TraceAttributeUtils.getListAttribute(mockTrace, "someattribute"));
}
}

0 comments on commit e583ecd

Please sign in to comment.