Java Code Examples for com.linecorp.armeria.client.ClientRequestContext#request()

The following examples show how to use com.linecorp.armeria.client.ClientRequestContext#request() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArmeriaHttpClientParser.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(brave.http.HttpRequest request, TraceContext context, SpanCustomizer span) {
    HttpRequestParser.DEFAULT.parse(request, context, span);

    final Object unwrapped = request.unwrap();
    if (!(unwrapped instanceof ClientRequestContext)) {
        return;
    }

    final ClientRequestContext ctx = (ClientRequestContext) unwrapped;
    final HttpRequest httpReq = ctx.request();
    if (httpReq == null) {
        // Should never reach here because BraveClient is an HTTP-level decorator.
        return;
    }

    span.tag(SpanTags.TAG_HTTP_HOST, httpReq.authority())
        .tag(SpanTags.TAG_HTTP_URL, httpReq.uri().toString());
}
 
Example 2
Source File: RetryingRpcClient.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void onRetryComplete(ClientRequestContext ctx, ClientRequestContext derivedCtx,
                                    RpcResponse res, CompletableFuture<RpcResponse> future) {
    onRetryingComplete(ctx);
    final HttpRequest actualHttpReq = derivedCtx.request();
    if (actualHttpReq != null) {
        ctx.updateRequest(actualHttpReq);
    }
    future.complete(res);
}
 
Example 3
Source File: ClientUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void fail(ClientRequestContext ctx, Throwable cause) {
    final HttpRequest req = ctx.request();
    if (req != null) {
        req.abort(cause);
    }

    final RequestLogBuilder logBuilder = ctx.logBuilder();
    logBuilder.endRequest(cause);
    logBuilder.endResponse(cause);
}
 
Example 4
Source File: BraveClientTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static RequestLog testRemoteInvocation(Tracing tracing, @Nullable String remoteServiceName)
        throws Exception {

    HttpTracing httpTracing = HttpTracing.newBuilder(tracing)
                                         .clientRequestParser(ArmeriaHttpClientParser.get())
                                         .clientResponseParser(ArmeriaHttpClientParser.get())
                                         .build();
    if (remoteServiceName != null) {
        httpTracing = httpTracing.clientOf(remoteServiceName);
    }

    // prepare parameters
    final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/hello/armeria",
                                                             HttpHeaderNames.SCHEME, "http",
                                                             HttpHeaderNames.AUTHORITY, "foo.com"));
    final RpcRequest rpcReq = RpcRequest.of(HelloService.Iface.class, "hello", "Armeria");
    final HttpResponse res = HttpResponse.of(HttpStatus.OK);
    final RpcResponse rpcRes = RpcResponse.of("Hello, Armeria!");
    final ClientRequestContext ctx = ClientRequestContext.builder(req).build();
    final HttpRequest actualReq = ctx.request();
    assertThat(actualReq).isNotNull();

    ctx.logBuilder().requestFirstBytesTransferred();
    ctx.logBuilder().requestContent(rpcReq, actualReq);
    ctx.logBuilder().endRequest();

    try (SafeCloseable ignored = ctx.push()) {
        final HttpClient delegate = mock(HttpClient.class);
        when(delegate.execute(any(), any())).thenReturn(res);

        final BraveClient stub = BraveClient.newDecorator(httpTracing).apply(delegate);
        // do invoke
        final HttpResponse actualRes = stub.execute(ctx, actualReq);

        assertThat(actualRes).isEqualTo(res);

        verify(delegate, times(1)).execute(same(ctx), argThat(arg -> {
            final RequestHeaders headers = arg.headers();
            return headers.contains(HttpHeaderNames.of("x-b3-traceid")) &&
                   headers.contains(HttpHeaderNames.of("x-b3-spanid")) &&
                   headers.contains(HttpHeaderNames.of("x-b3-sampled"));
        }));
    }

    ctx.logBuilder().responseHeaders(ResponseHeaders.of(HttpStatus.OK));
    ctx.logBuilder().responseFirstBytesTransferred();
    ctx.logBuilder().responseContent(rpcRes, res);
    ctx.logBuilder().endResponse();
    return ctx.log().ensureComplete();
}