Skip to content

Commit 522f78c

Browse files
Expose resource string attribute from SpanAttributeUtils (#44)
* Expose resource string attribute from SpanAttributeUtils * Remove javax nullable * Remove javax nullable
1 parent b7bb3e7 commit 522f78c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

data-model/src/main/java/org/hypertrace/core/datamodel/shared/SpanAttributeUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.hypertrace.core.datamodel.AttributeValue;
2020
import org.hypertrace.core.datamodel.Attributes;
2121
import org.hypertrace.core.datamodel.Event;
22+
import org.hypertrace.core.datamodel.Resource;
23+
import org.hypertrace.core.datamodel.StructuredTrace;
2224

2325
/**
2426
* Span being a very generic data structure with different attributes, reading attributes from Span
@@ -82,6 +84,25 @@ public static String getStringAttribute(Event event, String attributeKey) {
8284
return value == null ? null : value.getValue();
8385
}
8486

87+
public static Optional<String> getResourceStringAttribute(
88+
StructuredTrace trace, Event event, String attributeKey) {
89+
if (event.getResourceIndex() < 0
90+
|| event.getResourceIndex() >= trace.getResourceList().size()) {
91+
return Optional.empty();
92+
}
93+
94+
return Optional.of(trace.getResourceList().get(event.getResourceIndex()))
95+
.map(Resource::getAttributes)
96+
.flatMap(attributes -> getAttributeString(attributes, attributeKey));
97+
}
98+
99+
private static Optional<String> getAttributeString(@Nullable Attributes attributes, String key) {
100+
return Optional.ofNullable(attributes)
101+
.map(Attributes::getAttributeMap)
102+
.map(attributeMap -> attributeMap.get(key))
103+
.map(AttributeValue::getValue);
104+
}
105+
85106
public static Optional<String> getStringAttributeIgnoreKeyCase(Event event, String attributeKey) {
86107
if (event == null) {
87108
return Optional.empty();

data-model/src/test/java/org/hypertrace/core/datamodel/shared/SpanAttributeUtilsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
import static java.util.Collections.emptyList;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
57
import static org.mockito.Mockito.mock;
68
import static org.mockito.Mockito.when;
79

10+
import java.util.Collections;
811
import java.util.List;
912
import java.util.Map;
13+
import java.util.Optional;
1014
import org.hypertrace.core.datamodel.AttributeValue;
1115
import org.hypertrace.core.datamodel.Attributes;
1216
import org.hypertrace.core.datamodel.Event;
17+
import org.hypertrace.core.datamodel.Resource;
18+
import org.hypertrace.core.datamodel.StructuredTrace;
1319
import org.junit.jupiter.api.Test;
1420
import org.junit.jupiter.params.ParameterizedTest;
1521
import org.junit.jupiter.params.provider.ValueSource;
@@ -63,4 +69,27 @@ void testGetStringListAttribute_multiValuedList() {
6369
final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey");
6470
assertEquals(List.of("value1", "value2", "3"), result);
6571
}
72+
73+
@Test
74+
void testGetResourceStringAttribute() {
75+
final Event event = mock(Event.class);
76+
final StructuredTrace trace = mock(StructuredTrace.class);
77+
Resource resource =
78+
Resource.newBuilder()
79+
.setAttributes(
80+
Attributes.newBuilder()
81+
.setAttributeMap(
82+
Map.of("key1", AttributeValue.newBuilder().setValue("value1").build()))
83+
.build())
84+
.build();
85+
when(event.getResourceIndex()).thenReturn(0);
86+
when(trace.getResourceList()).thenReturn(Collections.singletonList(resource));
87+
Optional<String> valueAbsent =
88+
SpanAttributeUtils.getResourceStringAttribute(trace, event, "key2");
89+
Optional<String> valuePresent =
90+
SpanAttributeUtils.getResourceStringAttribute(trace, event, "key1");
91+
assertFalse(valueAbsent.isPresent());
92+
assertTrue(valuePresent.isPresent());
93+
assertEquals("value1", valuePresent.get());
94+
}
6695
}

0 commit comments

Comments
 (0)