Skip to content

Commit 1e1c09e

Browse files
author
Rishabh
authored
feat: Integrate with Gateway log api (#56)
1 parent 03df4ec commit 1e1c09e

File tree

24 files changed

+763
-93
lines changed

24 files changed

+763
-93
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ subprojects {
2121
dependencies {
2222
"api"(platform(project(":hypertrace-core-graphql-platform")))
2323
"annotationProcessor"(platform(project(":hypertrace-core-graphql-platform")))
24+
"testAnnotationProcessor"(platform(project(":hypertrace-core-graphql-platform")))
2425
"testImplementation"(platform(project(":hypertrace-core-graphql-test-platform")))
2526
"compileOnly"(platform(project(":hypertrace-core-graphql-platform")))
2627
}

hypertrace-core-graphql-attribute-scope-constants/src/main/java/org/hypertrace/core/graphql/atttributes/scopes/HypertraceCoreAttributeScopeString.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
public interface HypertraceCoreAttributeScopeString {
44
String SPAN = "EVENT";
55
String TRACE = "TRACE";
6+
String LOG_EVENT = "LOG_EVENT";
67
}

hypertrace-core-graphql-common-schema/src/main/java/org/hypertrace/core/graphql/common/schema/typefunctions/UnknownScalar.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import graphql.schema.CoercingSerializeException;
1616
import graphql.schema.GraphQLScalarType;
1717
import java.lang.reflect.AnnotatedType;
18+
import java.time.Instant;
1819
import java.util.function.Function;
1920
import java.util.stream.Collectors;
2021

@@ -28,6 +29,10 @@ public class UnknownScalar implements TypeFunction {
2829
new Coercing<>() {
2930
@Override
3031
public Object serialize(Object fetcherResult) throws CoercingSerializeException {
32+
if (fetcherResult instanceof Instant) {
33+
// todo handle for other direction as well
34+
return fetcherResult.toString();
35+
}
3136
// Use default serializer
3237
return fetcherResult;
3338
}

hypertrace-core-graphql-gateway-service-utils/src/main/java/org/hypertrace/core/graphql/utils/gateway/AttributeMapConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
import java.util.Map;
88
import java.util.Map.Entry;
99
import javax.inject.Inject;
10+
import org.hypertrace.core.graphql.attributes.AttributeModel;
1011
import org.hypertrace.core.graphql.common.request.AttributeRequest;
1112
import org.hypertrace.core.graphql.common.utils.BiConverter;
1213
import org.hypertrace.core.graphql.common.utils.CollectorUtils;
13-
import org.hypertrace.core.graphql.common.utils.Converter;
1414
import org.hypertrace.gateway.service.v1.common.Value;
1515

1616
class AttributeMapConverter
1717
implements BiConverter<Collection<AttributeRequest>, Map<String, Value>, Map<String, Object>> {
1818

19-
private final Converter<Value, Object> valueConverter;
19+
private final BiConverter<Value, AttributeModel, Object> valueConverter;
2020

2121
@Inject
22-
AttributeMapConverter(Converter<Value, Object> valueConverter) {
22+
AttributeMapConverter(BiConverter<Value, AttributeModel, Object> valueConverter) {
2323
this.valueConverter = valueConverter;
2424
}
2525

@@ -37,7 +37,7 @@ private Single<Entry<String, Object>> buildAttributeMapEntry(
3737
AttributeRequest attributeRequest, Map<String, Value> response) {
3838
// Uses SimpleImmutableEntry to support null values
3939
return this.valueConverter
40-
.convert(response.get(attributeRequest.alias()))
40+
.convert(response.get(attributeRequest.alias()), attributeRequest.attribute())
4141
.map(value -> new SimpleImmutableEntry<>(attributeRequest.attribute().key(), value));
4242
}
4343
}

hypertrace-core-graphql-gateway-service-utils/src/main/java/org/hypertrace/core/graphql/utils/gateway/GatewayUtilsModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ protected void configure() {
3434
.to(AttributeMapConverter.class);
3535

3636
bind(Key.get(new TypeLiteral<Converter<Value, Object>>() {})).to(UnwrappedValueConverter.class);
37+
bind(Key.get(new TypeLiteral<BiConverter<Value, AttributeModel, Object>>() {}))
38+
.to(UnwrappedValueConverter.class);
3739
bind(Key.get(new TypeLiteral<Converter<Collection<AttributeRequest>, Set<Expression>>>() {}))
3840
.to(SelectionExpressionSetConverter.class);
3941

hypertrace-core-graphql-gateway-service-utils/src/main/java/org/hypertrace/core/graphql/utils/gateway/LiteralConstantConverter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ private Value convertValue(Optional<Object> optionalObject) {
4646
if (this.assignableToAnyOfClasses(object.getClass(), Boolean.class)) {
4747
return Value.newBuilder().setValueType(ValueType.BOOL).setBoolean((Boolean) object).build();
4848
}
49+
// todo handle Instant type object
4950
if (this.assignableToAnyOfClasses(object.getClass(), TemporalAccessor.class)) {
5051
return Value.newBuilder()
5152
.setValueType(ValueType.TIMESTAMP)

hypertrace-core-graphql-gateway-service-utils/src/main/java/org/hypertrace/core/graphql/utils/gateway/UnwrappedValueConverter.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
import io.reactivex.rxjava3.core.Single;
44
import java.time.Instant;
55
import java.util.Optional;
6+
import org.hypertrace.core.graphql.attributes.AttributeModel;
7+
import org.hypertrace.core.graphql.common.utils.BiConverter;
68
import org.hypertrace.core.graphql.common.utils.Converter;
79
import org.hypertrace.gateway.service.v1.common.Value;
10+
import org.hypertrace.gateway.service.v1.common.ValueType;
811

9-
class UnwrappedValueConverter implements Converter<Value, Object> {
12+
class UnwrappedValueConverter
13+
implements Converter<Value, Object>, BiConverter<Value, AttributeModel, Object> {
1014

1115
@Override
1216
public Single<Object> convert(Value from) {
@@ -43,4 +47,24 @@ public Single<Object> convert(Value from) {
4347
value.getValueType().name())));
4448
}
4549
}
50+
51+
@Override
52+
public Single<Object> convert(Value from, AttributeModel attributeModel) {
53+
Value value = Optional.ofNullable(from).orElse(Value.getDefaultInstance());
54+
if (ValueType.TIMESTAMP.equals(value.getValueType())) {
55+
return handleTimestamp(value, attributeModel);
56+
}
57+
return convert(from);
58+
}
59+
60+
private Single<Object> handleTimestamp(Value value, AttributeModel attributeModel) {
61+
try {
62+
if ("ns".equals(attributeModel.units())) {
63+
return Single.just(Instant.ofEpochSecond(0, value.getTimestamp()));
64+
}
65+
return Single.just((Instant.ofEpochMilli(value.getTimestamp())));
66+
} catch (Throwable t) {
67+
return Single.error(t);
68+
}
69+
}
4670
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hypertrace.core.graphql.utils.gateway;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
import org.hypertrace.core.graphql.attributes.AttributeModel;
6+
import org.hypertrace.gateway.service.v1.common.Value;
7+
import org.hypertrace.gateway.service.v1.common.ValueType;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.Mock;
12+
import org.mockito.Mockito;
13+
import org.mockito.junit.jupiter.MockitoExtension;
14+
15+
@ExtendWith(MockitoExtension.class)
16+
class UnwrappedValueConverterTest {
17+
18+
@Mock AttributeModel attributeModel;
19+
20+
@Test
21+
void testTimestampConversion() {
22+
UnwrappedValueConverter unwrappedValueConverter = new UnwrappedValueConverter();
23+
long millis = System.currentTimeMillis();
24+
Value value =
25+
Value.newBuilder()
26+
.setValueType(ValueType.TIMESTAMP)
27+
.setTimestamp(Duration.ofMillis(millis).toNanos())
28+
.build();
29+
Mockito.when(attributeModel.units()).thenReturn("ns");
30+
31+
Instant instant =
32+
(Instant) unwrappedValueConverter.convert(value, attributeModel).blockingGet();
33+
Assertions.assertEquals(Instant.ofEpochMilli(millis), instant);
34+
}
35+
}

hypertrace-core-graphql-log-event-schema/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@ dependencies {
2323
implementation(project(":hypertrace-core-graphql-common-schema"))
2424
implementation(project(":hypertrace-core-graphql-attribute-store"))
2525
implementation(project(":hypertrace-core-graphql-deserialization"))
26+
implementation(project(":hypertrace-core-graphql-schema-utils"))
27+
28+
testImplementation("org.junit.jupiter:junit-jupiter")
29+
testImplementation("com.fasterxml.jackson.core:jackson-databind")
30+
testImplementation(project(":hypertrace-core-graphql-gateway-service-utils"))
31+
testAnnotationProcessor("org.projectlombok:lombok")
32+
testCompileOnly("org.projectlombok:lombok")
33+
}
34+
35+
tasks.test {
36+
useJUnitPlatform()
2637
}

hypertrace-core-graphql-log-event-schema/src/main/java/org/hypertrace/core/graphql/log/event/LogEventSchemaModule.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.inject.AbstractModule;
44
import com.google.inject.multibindings.Multibinder;
5-
import org.hypertrace.core.graphql.common.request.ResultSetRequestBuilder;
65
import org.hypertrace.core.graphql.log.event.dao.LogEventDaoModule;
76
import org.hypertrace.core.graphql.log.event.request.LogEventRequestModule;
87
import org.hypertrace.core.graphql.spi.schema.GraphQlSchemaFragment;
@@ -14,8 +13,7 @@ protected void configure() {
1413
.addBinding()
1514
.to(LogEventSchemaFragment.class);
1615

17-
requireBinding(ResultSetRequestBuilder.class);
18-
install(new LogEventDaoModule());
1916
install(new LogEventRequestModule());
17+
install(new LogEventDaoModule());
2018
}
2119
}

0 commit comments

Comments
 (0)