@Override public ListenableFuture<ClientHttpResponse> intercept(final HttpRequest request, final byte[] body, final AsyncClientHttpRequestExecution execution) throws IOException { final URI originalUri = request.getURI(); String serviceName = originalUri.getHost(); return this.loadBalancer.execute(serviceName, new LoadBalancerRequest<ListenableFuture<ClientHttpResponse>>() { @Override public ListenableFuture<ClientHttpResponse> apply(final ServiceInstance instance) throws Exception { HttpRequest serviceRequest = new ServiceRequestWrapper(request, instance, loadBalancer); return execution.executeAsync(serviceRequest, body); } }); }
@Before public void beforeMethod() throws IOException { resetTracing(); spanRecorder = new SpanRecorder(); Tracer.getInstance().addSpanLifecycleListener(spanRecorder); method = HttpMethod.PATCH; uri = URI.create("http://localhost:4242/" + UUID.randomUUID().toString()); httpRequest = new HttpRequest() { @Override public HttpHeaders getHeaders() { return new HttpHeaders(); } @Override public HttpMethod getMethod() { return method; } @Override public URI getURI() { return uri; } }; body = UUID.randomUUID().toString().getBytes(); executionMock = mock(AsyncClientHttpRequestExecution.class); doAnswer(invocation -> { tracingStateAtTimeOfExecution = TracingState.getCurrentThreadTracingState(); executionResponseFuture = new SettableListenableFuture<>(); return executionResponseFuture; }).when(executionMock).executeAsync(any(HttpRequest.class), any(byte[].class)); }
@Override @SuppressWarnings("deprecation") public ListenableFuture<ClientHttpResponse> intercept( HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution ) throws IOException { HttpRequest wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request); Tracer tracer = Tracer.getInstance(); // Handle subspan stuff if desired. SpanAroundAsyncCallFinisher subspanFinisher = null; TracingState originalThreadInfo = null; if (surroundCallsWithSubspan) { originalThreadInfo = TracingState.getCurrentThreadTracingState(); // This will start a new trace if necessary, or a subspan if a trace is already in progress. tracer.startSpanInCurrentContext(getSubspanSpanName(request), Span.SpanPurpose.CLIENT); // Create the callback that will complete the subspan when the request finishes. subspanFinisher = new SpanAroundAsyncCallFinisher(TracingState.getCurrentThreadTracingState()); } try { // Whether we created a subspan or not we want to add the tracing headers with the current span's info. propagateTracingHeaders(wrapperRequest, tracer.getCurrentSpan()); // Execute the request/interceptor chain, and add the callback to finish the subspan (if one exists). ListenableFuture<ClientHttpResponse> result = execution.executeAsync(wrapperRequest, body); if (subspanFinisher != null) { result.addCallback(subspanFinisher); } return result; } catch(Throwable t) { // Something went wrong in the execution.executeAsync(...) call so we complete the subspan now (if one // exists) if (subspanFinisher != null) { subspanFinisher.finishCallSpan(); } throw t; } finally { // Reset back to the original tracing state that was on this thread when this method began (only relevant // if surroundCallsWithSubspan is true). if (surroundCallsWithSubspan) { unlinkTracingFromCurrentThread(originalThreadInfo); } } }
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8)); request.getHeaders().add("Authorization", "Basic " + token); return execution.executeAsync(request, body); }