|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.smoketest; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.google.protobuf.GeneratedMessage; |
| 10 | +import com.google.protobuf.util.JsonFormat; |
| 11 | +import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest; |
| 12 | +import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest; |
| 13 | +import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest; |
| 14 | +import io.opentelemetry.testing.internal.armeria.client.WebClient; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.function.Supplier; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.StreamSupport; |
| 20 | + |
| 21 | +public class TelemetryRetriever { |
| 22 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 23 | + |
| 24 | + final WebClient client; |
| 25 | + |
| 26 | + TelemetryRetriever(int backendPort) { |
| 27 | + client = WebClient.of("http://localhost:" + backendPort); |
| 28 | + } |
| 29 | + |
| 30 | + void clearTelemetry() { |
| 31 | + client.get("/clear").aggregate().join(); |
| 32 | + } |
| 33 | + |
| 34 | + Collection<ExportTraceServiceRequest> waitForTraces() { |
| 35 | + return waitForTelemetry("get-traces", ExportTraceServiceRequest::newBuilder); |
| 36 | + } |
| 37 | + |
| 38 | + Collection<ExportMetricsServiceRequest> waitForMetrics() { |
| 39 | + return waitForTelemetry("get-metrics", ExportMetricsServiceRequest::newBuilder); |
| 40 | + } |
| 41 | + |
| 42 | + Collection<ExportLogsServiceRequest> waitForLogs() { |
| 43 | + return waitForTelemetry("get-logs", ExportLogsServiceRequest::newBuilder); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings({"unchecked", "InterruptedExceptionSwallowed"}) |
| 47 | + private <T extends GeneratedMessage, B extends GeneratedMessage.Builder<?>> |
| 48 | + Collection<T> waitForTelemetry(String path, Supplier<B> builderConstructor) { |
| 49 | + try { |
| 50 | + String content = waitForContent(path); |
| 51 | + |
| 52 | + return StreamSupport.stream(OBJECT_MAPPER.readTree(content).spliterator(), false) |
| 53 | + .map( |
| 54 | + jsonNode -> { |
| 55 | + B builder = builderConstructor.get(); |
| 56 | + // TODO: Register parser into object mapper to avoid de -> re -> deserialize. |
| 57 | + try { |
| 58 | + String json = OBJECT_MAPPER.writeValueAsString(jsonNode); |
| 59 | + JsonFormat.parser().merge(json, builder); |
| 60 | + } catch (Exception e) { |
| 61 | + throw new IllegalStateException(e); |
| 62 | + } |
| 63 | + return (T) builder.build(); |
| 64 | + }) |
| 65 | + .collect(Collectors.toList()); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new IllegalStateException(e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("SystemOut") |
| 72 | + private String waitForContent(String path) throws InterruptedException { |
| 73 | + long previousSize = 0; |
| 74 | + long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(30); |
| 75 | + String content = "[]"; |
| 76 | + while (System.currentTimeMillis() < deadline) { |
| 77 | + content = client.get(path).aggregate().join().contentUtf8(); |
| 78 | + if (content.length() > 2 && content.length() == previousSize) { |
| 79 | + break; |
| 80 | + } |
| 81 | + previousSize = content.length(); |
| 82 | + System.out.println("Current content size $previousSize"); |
| 83 | + TimeUnit.MILLISECONDS.sleep(500); |
| 84 | + } |
| 85 | + |
| 86 | + return content; |
| 87 | + } |
| 88 | +} |
0 commit comments