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

chore | Add SpanJoiner based on list of spanId #177

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -3,7 +3,7 @@
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Iterables.concat;
import static org.hypertrace.core.graphql.atttributes.scopes.HypertraceCoreAttributeScopeString.SPAN;
import static org.hypertrace.core.graphql.span.joiner.SpanJoin.SPAN_KEY;
import static org.hypertrace.core.graphql.span.joiner.SpanJoin.SPANS_KEY;

import graphql.schema.DataFetchingFieldSelectionSet;
import graphql.schema.SelectedField;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Single<SpanJoiner> build(

private List<SelectedField> getSelections(
DataFetchingFieldSelectionSet selectionSet, List<String> pathToSpanJoin) {
List<String> fullPath = copyOf(concat(pathToSpanJoin, List.of(SPAN_KEY)));
List<String> fullPath = copyOf(concat(pathToSpanJoin, List.of(SPANS_KEY)));
return selectionFinder
.findSelections(selectionSet, SelectionQuery.builder().selectionPath(fullPath).build())
.collect(Collectors.toUnmodifiableList());
Expand All @@ -91,34 +91,47 @@ private class DefaultSpanJoiner implements SpanJoiner {
private final List<SelectedField> selectedFields;

@Override
public <T> Single<Map<T, Span>> joinSpans(
public <T> Single<Map<T, Collection<Span>>> joinSpans(
Collection<T> joinSources, SpanIdGetter<T> spanIdGetter) {
return this.buildSourceToIdMap(joinSources, spanIdGetter).flatMap(this::joinSpans);
}

private <T> Single<Map<T, Span>> joinSpans(Map<T, String> sourceToSpanIdMap) {
return this.buildSpanRequest(sourceToSpanIdMap)
private <T> Single<Map<T, Collection<Span>>> joinSpans(
Map<T, Collection<String>> sourceToSpanIdsMap) {
return this.buildSpanRequest(sourceToSpanIdsMap)
.flatMap(spanDao::getSpans)
.map(this::buildSpanIdToSpanMap)
.map(spanIdToSpanMap -> buildSourceToSpanMap(sourceToSpanIdMap, spanIdToSpanMap));
.map(spanIdToSpanMap -> buildSourceToSpansMap(sourceToSpanIdsMap, spanIdToSpanMap));
}

private <T> Map<T, Span> buildSourceToSpanMap(
Map<T, String> sourceToSpanIdMap, Map<String, Span> spanIdToSpanMap) {
return sourceToSpanIdMap.entrySet().stream()
.filter(entry -> spanIdToSpanMap.containsKey(entry.getValue()))
private <T> Map<T, Collection<Span>> buildSourceToSpansMap(
Map<T, Collection<String>> sourceToSpanIdsMap, Map<String, Span> spanIdToSpanMap) {
return sourceToSpanIdsMap.entrySet().stream()
.collect(
Collectors.toUnmodifiableMap(
Entry::getKey, entry -> spanIdToSpanMap.get(entry.getValue())));
Entry::getKey,
entry -> buildSpansCollectionForEachSource(entry.getValue(), spanIdToSpanMap)));
}

private Collection<Span> buildSpansCollectionForEachSource(
Collection<String> spanIds, Map<String, Span> spanIdToSpanMap) {
return spanIds.stream()
.filter(spanIdToSpanMap::containsKey)
.distinct()
AnandShivansh marked this conversation as resolved.
Show resolved Hide resolved
.map(spanIdToSpanMap::get)
.collect(Collectors.toUnmodifiableList());
}

private Map<String, Span> buildSpanIdToSpanMap(SpanResultSet resultSet) {
return resultSet.results().stream()
.collect(Collectors.toUnmodifiableMap(Identifiable::id, Function.identity()));
}

private <T> Single<SpanRequest> buildSpanRequest(Map<T, String> sourceToSpanIdMap) {
Collection<String> spanIds = sourceToSpanIdMap.values();
private <T> Single<SpanRequest> buildSpanRequest(Map<T, Collection<String>> sourceToSpanIdMap) {
Set<String> spanIds =
sourceToSpanIdMap.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
return buildSpanIdsFilter(spanIds)
.flatMap(filterArguments -> buildSpanRequest(spanIds.size(), filterArguments));
}
Expand All @@ -144,16 +157,16 @@ private Single<List<AttributeAssociation<FilterArgument>>> buildSpanIdsFilter(
return filterRequestBuilder.build(context, SPAN, Set.of(new SpanIdFilter(spanIds)));
}

private <T> Single<Map<T, String>> buildSourceToIdMap(
private <T> Single<Map<T, Collection<String>>> buildSourceToIdMap(
Collection<T> joinSources, SpanIdGetter<T> spanIdGetter) {
return Observable.fromIterable(joinSources)
.flatMapSingle(source -> this.maybeBuildMapEntry(source, spanIdGetter))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

private <T> Single<Entry<T, String>> maybeBuildMapEntry(
private <T> Single<Entry<T, Collection<String>>> maybeBuildMapEntry(
T source, SpanIdGetter<T> spanIdGetter) {
return spanIdGetter.getSpanId(source).map(id -> Map.entry(source, id));
return spanIdGetter.getSpanIds(source).map(ids -> Map.entry(source, ids));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import java.util.List;
import org.hypertrace.core.graphql.span.schema.Span;

public interface SpanJoin {
String SPAN_KEY = "span";
String SPANS_KEY = "spans";

@GraphQLField
@GraphQLName(SPAN_KEY)
Span span();
@GraphQLName(SPANS_KEY)
List<Span> spans();
AnandShivansh marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ public interface SpanJoiner {
SpanJoiner NO_OP_JOINER =
new SpanJoiner() {
@Override
public <T> Single<Map<T, Span>> joinSpans(
public <T> Single<Map<T, Collection<Span>>> joinSpans(
Collection<T> joinSources, SpanIdGetter<T> spanIdGetter) {
return Single.just(Collections.emptyMap());
}
};

<T> Single<Map<T, Span>> joinSpans(Collection<T> joinSources, SpanIdGetter<T> spanIdGetter);
<T> Single<Map<T, Collection<Span>>> joinSpans(
AnandShivansh marked this conversation as resolved.
Show resolved Hide resolved
Collection<T> joinSources, SpanIdGetter<T> spanIdGetter);

@FunctionalInterface
interface SpanIdGetter<T> {
Single<String> getSpanId(T source);
Single<Collection<String>> getSpanIds(T source);
AnandShivansh marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import graphql.schema.DataFetchingFieldSelectionSet;
import graphql.schema.SelectedField;
import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -74,13 +75,12 @@ void setup() {
void fetchSpans() {
Span span1 = new TestSpan(FIRST_SPAN_ID);
Span span2 = new TestSpan(SECOND_SPAN_ID);
TestJoinSource joinSource1 = new TestJoinSource(FIRST_SPAN_ID);
TestJoinSource joinSource2 = new TestJoinSource(SECOND_SPAN_ID);
Map<TestJoinSource, Span> expected =
Map.ofEntries(entry(joinSource1, span1), entry(joinSource2, span2));
TestJoinSource joinSource1 = new TestJoinSource(List.of(FIRST_SPAN_ID));
TestJoinSource joinSource2 = new TestJoinSource(List.of(SECOND_SPAN_ID));
Map<TestJoinSource, Collection<Span>> expected =
Map.ofEntries(entry(joinSource1, List.of(span1)), entry(joinSource2, List.of(span2)));
List<TestJoinSource> joinSources = List.of(joinSource1, joinSource2);
mockRequestedSelectionFields(
List.of(mock(SelectedField.class), mock(SelectedField.class)), "pathToSpan");
mockRequestedSelectionFields(List.of(mock(SelectedField.class), mock(SelectedField.class)));
mockRequestBuilding();
mockResult(List.of(span1, span2));
SpanJoiner joiner =
Expand All @@ -91,8 +91,10 @@ void fetchSpans() {
this.mockSelectionSet,
List.of("pathToSpan"))
.blockingGet();
assertEquals(
expected, joiner.joinSpans(joinSources, new TestJoinSourceIdGetter()).blockingGet());
Map<TestJoinSource, Collection<Span>> actual =
joiner.joinSpans(joinSources, new TestJoinSourceIdGetter()).blockingGet();
assertEquals(expected.get(joinSource1), actual.get(joinSource1));
assertEquals(expected.get(joinSource2), actual.get(joinSource2));
}

private void mockRequestBuilding() {
Expand All @@ -112,10 +114,10 @@ private void mockRequestBuilding() {
.thenReturn(Single.just(mockResultSetRequest));
}

private void mockRequestedSelectionFields(List<SelectedField> selectedFields, String location) {
private void mockRequestedSelectionFields(List<SelectedField> selectedFields) {
when(mockSelectionFinder.findSelections(
mockSelectionSet,
SelectionQuery.builder().selectionPath(List.of(location, "span")).build()))
SelectionQuery.builder().selectionPath(List.of("pathToSpan", "spans")).build()))
.thenReturn(selectedFields.stream());
}

Expand All @@ -126,16 +128,16 @@ private void mockResult(List<Span> spans) {

@Value
private static class TestJoinSource {
String spanId;
List<String> spanIds;
}

private static class TestJoinSourceIdGetter implements SpanIdGetter<TestJoinSource> {
@Override
public Single<String> getSpanId(TestJoinSource source) {
if (source.getSpanId() == null || source.getSpanId().isEmpty()) {
public Single<Collection<String>> getSpanIds(TestJoinSource source) {
if (source.getSpanIds() == null || source.getSpanIds().isEmpty()) {
return Single.error(new IllegalArgumentException("Empty spanId"));
}
return Single.just(source.getSpanId());
return Single.just(source.getSpanIds());
}
}

Expand Down
Loading