Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Expose resource string attribute from SpanAttributeUtils #44

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -19,6 +19,8 @@
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Attributes;
import org.hypertrace.core.datamodel.Event;
import org.hypertrace.core.datamodel.Resource;
import org.hypertrace.core.datamodel.StructuredTrace;

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

public static Optional<String> getResourceStringAttribute(
StructuredTrace trace, Event event, String attributeKey) {
if (event.getResourceIndex() < 0
|| event.getResourceIndex() >= trace.getResourceList().size()) {
return Optional.empty();
}

return Optional.of(trace.getResourceList().get(event.getResourceIndex()))
.map(Resource::getAttributes)
.flatMap(attributes -> getAttributeString(attributes, attributeKey));
}

private static Optional<String> getAttributeString(
@javax.annotation.Nullable Attributes attributes, String key) {
AnandShivansh marked this conversation as resolved.
Show resolved Hide resolved
return Optional.ofNullable(attributes)
.map(Attributes::getAttributeMap)
.map(attributeMap -> attributeMap.get(key))
.map(AttributeValue::getValue);
}

public static Optional<String> getStringAttributeIgnoreKeyCase(Event event, String attributeKey) {
if (event == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

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

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Attributes;
import org.hypertrace.core.datamodel.Event;
import org.hypertrace.core.datamodel.Resource;
import org.hypertrace.core.datamodel.StructuredTrace;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -63,4 +69,27 @@ void testGetStringListAttribute_multiValuedList() {
final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey");
assertEquals(List.of("value1", "value2", "3"), result);
}

@Test
void testGetResourceStringAttribute() {
final Event event = mock(Event.class);
final StructuredTrace trace = mock(StructuredTrace.class);
Resource resource =
Resource.newBuilder()
.setAttributes(
Attributes.newBuilder()
.setAttributeMap(
Map.of("key1", AttributeValue.newBuilder().setValue("value1").build()))
.build())
.build();
when(event.getResourceIndex()).thenReturn(0);
when(trace.getResourceList()).thenReturn(Collections.singletonList(resource));
Optional<String> valueAbsent =
SpanAttributeUtils.getResourceStringAttribute(trace, event, "key2");
Optional<String> valuePresent =
SpanAttributeUtils.getResourceStringAttribute(trace, event, "key1");
assertFalse(valueAbsent.isPresent());
assertTrue(valuePresent.isPresent());
assertEquals("value1", valuePresent.get());
}
}