|
| 1 | +package io.netifi.proteus.tracing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.core.JsonToken; |
| 5 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 9 | +import com.fasterxml.jackson.databind.module.SimpleDeserializers; |
| 10 | +import com.hubspot.jackson.datatype.protobuf.ProtobufModule; |
| 11 | +import io.netty.handler.codec.json.JsonObjectDecoder; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.function.Function; |
| 16 | +import org.reactivestreams.Publisher; |
| 17 | +import reactor.core.Exceptions; |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | +import reactor.netty.http.client.HttpClient; |
| 21 | +import zipkin2.proto3.Span; |
| 22 | + |
| 23 | +public class TracesStreamer { |
| 24 | + |
| 25 | + private final ObjectMapper objectMapper = protoMapper(); |
| 26 | + private Function<Integer, Publisher<InputStream>> inputSource; |
| 27 | + |
| 28 | + public TracesStreamer(String zipkinUrl, Mono<HttpClient> client) { |
| 29 | + this(zipkinServerStream(zipkinUrl, client)); |
| 30 | + } |
| 31 | + |
| 32 | + public TracesStreamer(Publisher<InputStream> tracesSource) { |
| 33 | + this(v -> tracesSource); |
| 34 | + } |
| 35 | + |
| 36 | + private TracesStreamer(Function<Integer, Publisher<InputStream>> inputSource) { |
| 37 | + this.inputSource = inputSource; |
| 38 | + } |
| 39 | + |
| 40 | + public Flux<Trace> streamTraces(int lookbackSeconds) { |
| 41 | + return streamTraces(inputSource.apply(lookbackSeconds)); |
| 42 | + } |
| 43 | + |
| 44 | + Flux<Trace> streamTraces(Publisher<InputStream> input) { |
| 45 | + return Flux.from(input) |
| 46 | + .filter( |
| 47 | + is -> { |
| 48 | + try { |
| 49 | + return is.available() > 0; |
| 50 | + } catch (IOException e) { |
| 51 | + throw Exceptions.propagate(e); |
| 52 | + } |
| 53 | + }) |
| 54 | + .map( |
| 55 | + is -> { |
| 56 | + try { |
| 57 | + return objectMapper.readValue(is, new TypeReference<Trace>() {}); |
| 58 | + } catch (IOException e) { |
| 59 | + throw Exceptions.propagate(e); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private static Function<Integer, Publisher<InputStream>> zipkinServerStream( |
| 65 | + String zipkinUrl, Mono<HttpClient> client) { |
| 66 | + return lookbackSeconds -> |
| 67 | + client.flatMapMany( |
| 68 | + c -> |
| 69 | + c.doOnRequest( |
| 70 | + (__, connection) -> connection.addHandler(new JsonObjectDecoder(true))) |
| 71 | + .get() |
| 72 | + .uri(zipkinQuery(zipkinUrl, lookbackSeconds)) |
| 73 | + .responseContent() |
| 74 | + .asInputStream()); |
| 75 | + } |
| 76 | + |
| 77 | + private static String zipkinQuery(String zipkinUrl, int lookbackSeconds) { |
| 78 | + long lookbackMillis = TimeUnit.SECONDS.toMillis(lookbackSeconds); |
| 79 | + return zipkinUrl + "?lookback=" + lookbackMillis + "&limit=100000"; |
| 80 | + } |
| 81 | + |
| 82 | + private ObjectMapper protoMapper() { |
| 83 | + ObjectMapper mapper = new ObjectMapper(); |
| 84 | + ProtobufModule module = new CustomProtoModule(); |
| 85 | + mapper.registerModule(module); |
| 86 | + return mapper; |
| 87 | + } |
| 88 | + |
| 89 | + public static class CustomProtoModule extends ProtobufModule { |
| 90 | + @Override |
| 91 | + public void setupModule(SetupContext context) { |
| 92 | + super.setupModule(context); |
| 93 | + SimpleDeserializers deser = new SimpleDeserializers(); |
| 94 | + deser.addDeserializer(Trace.class, new TracersDeserializer()); |
| 95 | + context.addDeserializers(deser); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static class TracersDeserializer extends StdDeserializer<Trace> { |
| 100 | + |
| 101 | + public TracersDeserializer() { |
| 102 | + this(null); |
| 103 | + } |
| 104 | + |
| 105 | + protected TracersDeserializer(Class<?> vc) { |
| 106 | + super(vc); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Trace deserialize(JsonParser p, DeserializationContext ctx) throws IOException { |
| 111 | + Trace.Builder traceBuilder = Trace.newBuilder(); |
| 112 | + while (p.nextToken() != JsonToken.END_ARRAY) { |
| 113 | + traceBuilder.addSpans(ctx.readValue(p, Span.class)); |
| 114 | + } |
| 115 | + return traceBuilder.build(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments