|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.PekkoHttpServerSingletons.instrumenter; |
| 10 | + |
| 11 | +import io.opentelemetry.context.Context; |
| 12 | +import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; |
| 13 | +import io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route.PekkoRouteHolder; |
| 14 | +import java.util.ArrayDeque; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Queue; |
| 17 | +import org.apache.pekko.http.javadsl.model.HttpHeader; |
| 18 | +import org.apache.pekko.http.scaladsl.model.HttpRequest; |
| 19 | +import org.apache.pekko.http.scaladsl.model.HttpResponse; |
| 20 | +import org.apache.pekko.stream.Attributes; |
| 21 | +import org.apache.pekko.stream.BidiShape; |
| 22 | +import org.apache.pekko.stream.Inlet; |
| 23 | +import org.apache.pekko.stream.Outlet; |
| 24 | +import org.apache.pekko.stream.scaladsl.BidiFlow; |
| 25 | +import org.apache.pekko.stream.stage.AbstractInHandler; |
| 26 | +import org.apache.pekko.stream.stage.AbstractOutHandler; |
| 27 | +import org.apache.pekko.stream.stage.GraphStage; |
| 28 | +import org.apache.pekko.stream.stage.GraphStageLogic; |
| 29 | + |
| 30 | +public class PekkoHttpServerTracer |
| 31 | + extends GraphStage<BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest>> { |
| 32 | + private final Inlet<HttpRequest> requestIn = Inlet.create("otel.requestIn"); |
| 33 | + private final Outlet<HttpRequest> requestOut = Outlet.create("otel.requestOut"); |
| 34 | + private final Inlet<HttpResponse> responseIn = Inlet.create("otel.responseIn"); |
| 35 | + private final Outlet<HttpResponse> responseOut = Outlet.create("otel.responseOut"); |
| 36 | + |
| 37 | + private final BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest> shape = |
| 38 | + BidiShape.of(responseIn, responseOut, requestIn, requestOut); |
| 39 | + |
| 40 | + public static BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> wrap( |
| 41 | + BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> handler) { |
| 42 | + return BidiFlow.fromGraph(new PekkoHttpServerTracer()).atopMat(handler, (a, b) -> b); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest> shape() { |
| 47 | + return shape; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public GraphStageLogic createLogic(Attributes attributes) { |
| 52 | + return new TracingLogic(); |
| 53 | + } |
| 54 | + |
| 55 | + private class TracingLogic extends GraphStageLogic { |
| 56 | + private final Queue<PekkoTracingRequest> requests = new ArrayDeque<>(); |
| 57 | + |
| 58 | + public TracingLogic() { |
| 59 | + super(shape); |
| 60 | + |
| 61 | + // server pulls response, pass response from user code to server |
| 62 | + setHandler( |
| 63 | + responseOut, |
| 64 | + new AbstractOutHandler() { |
| 65 | + @Override |
| 66 | + public void onPull() { |
| 67 | + pull(responseIn); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onDownstreamFinish(Throwable cause) { |
| 72 | + cancel(responseIn); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // user code pulls request, pass request from server to user code |
| 77 | + setHandler( |
| 78 | + requestOut, |
| 79 | + new AbstractOutHandler() { |
| 80 | + @Override |
| 81 | + public void onPull() { |
| 82 | + pull(requestIn); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onDownstreamFinish(Throwable cause) { |
| 87 | + // Invoked on errors. Don't complete this stage to allow error-capturing |
| 88 | + cancel(requestIn); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + // new request from server |
| 93 | + setHandler( |
| 94 | + requestIn, |
| 95 | + new AbstractInHandler() { |
| 96 | + @Override |
| 97 | + public void onPush() { |
| 98 | + HttpRequest request = grab(requestIn); |
| 99 | + PekkoTracingRequest tracingRequest = PekkoTracingRequest.EMPTY; |
| 100 | + Context parentContext = currentContext(); |
| 101 | + if (instrumenter().shouldStart(parentContext, request)) { |
| 102 | + Context context = instrumenter().start(parentContext, request); |
| 103 | + context = PekkoRouteHolder.init(context); |
| 104 | + tracingRequest = new PekkoTracingRequest(context, request); |
| 105 | + request = |
| 106 | + (HttpRequest) |
| 107 | + request.addAttribute(PekkoTracingRequest.ATTR_KEY, tracingRequest); |
| 108 | + } |
| 109 | + // event if span wasn't started we need to push TracingRequest to match response |
| 110 | + // with request |
| 111 | + requests.add(tracingRequest); |
| 112 | + |
| 113 | + push(requestOut, request); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onUpstreamFinish() { |
| 118 | + complete(requestOut); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onUpstreamFailure(Throwable exception) { |
| 123 | + fail(requestOut, exception); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + // response from user code |
| 128 | + setHandler( |
| 129 | + responseIn, |
| 130 | + new AbstractInHandler() { |
| 131 | + @Override |
| 132 | + public void onPush() { |
| 133 | + HttpResponse response = grab(responseIn); |
| 134 | + |
| 135 | + PekkoTracingRequest tracingRequest = requests.poll(); |
| 136 | + if (tracingRequest != null && tracingRequest != PekkoTracingRequest.EMPTY) { |
| 137 | + // pekko response is immutable so the customizer just captures the added headers |
| 138 | + PekkoHttpResponseMutator responseMutator = new PekkoHttpResponseMutator(); |
| 139 | + HttpServerResponseCustomizerHolder.getCustomizer() |
| 140 | + .customize(tracingRequest.context, response, responseMutator); |
| 141 | + // build a new response with the added headers |
| 142 | + List<HttpHeader> headers = responseMutator.getHeaders(); |
| 143 | + if (!headers.isEmpty()) { |
| 144 | + response = (HttpResponse) response.addHeaders(headers); |
| 145 | + } |
| 146 | + |
| 147 | + instrumenter().end(tracingRequest.context, tracingRequest.request, response, null); |
| 148 | + } |
| 149 | + push(responseOut, response); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void onUpstreamFailure(Throwable exception) { |
| 154 | + // End the span for the request that failed |
| 155 | + PekkoTracingRequest tracingRequest = requests.poll(); |
| 156 | + if (tracingRequest != null && tracingRequest != PekkoTracingRequest.EMPTY) { |
| 157 | + instrumenter() |
| 158 | + .end( |
| 159 | + tracingRequest.context, |
| 160 | + tracingRequest.request, |
| 161 | + PekkoHttpServerSingletons.errorResponse(), |
| 162 | + exception); |
| 163 | + } |
| 164 | + |
| 165 | + fail(responseOut, exception); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void onUpstreamFinish() { |
| 170 | + // End any ongoing spans, though there should be none. |
| 171 | + PekkoTracingRequest tracingRequest; |
| 172 | + while ((tracingRequest = requests.poll()) != null) { |
| 173 | + if (tracingRequest == PekkoTracingRequest.EMPTY) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + instrumenter() |
| 177 | + .end( |
| 178 | + tracingRequest.context, |
| 179 | + tracingRequest.request, |
| 180 | + PekkoHttpServerSingletons.errorResponse(), |
| 181 | + null); |
| 182 | + } |
| 183 | + completeStage(); |
| 184 | + } |
| 185 | + }); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments