Search in sources :

Example 6 with MetricCollector

use of software.amazon.awssdk.metrics.MetricCollector in project aws-sdk-java-v2 by aws.

the class IdleConnectionCountingChannelPoolTest method collectChannelPoolMetrics_failes_futureFailed.

@Test
public void collectChannelPoolMetrics_failes_futureFailed() throws Exception {
    MockChannel channel = new MockChannel();
    eventLoopGroup.register(channel);
    RuntimeException errorToThrow = new RuntimeException("failed!");
    MetricCollector mockMetricCollector = mock(MetricCollector.class);
    doThrow(errorToThrow).when(mockMetricCollector).reportMetric(any(), any());
    CompletableFuture<Void> collectFuture = idleCountingPool.collectChannelPoolMetrics(mockMetricCollector);
    assertThatThrownBy(() -> collectFuture.get(1, TimeUnit.SECONDS)).hasCause(errorToThrow);
}
Also used : MetricCollector(software.amazon.awssdk.metrics.MetricCollector) Test(org.junit.Test)

Example 7 with MetricCollector

use of software.amazon.awssdk.metrics.MetricCollector in project aws-sdk-java-v2 by aws.

the class AsyncApiCallMetricCollectionStage method execute.

@Override
public CompletableFuture<OutputT> execute(SdkHttpFullRequest input, RequestExecutionContext context) throws Exception {
    MetricCollector metricCollector = context.executionContext().metricCollector();
    CompletableFuture<OutputT> future = new CompletableFuture<>();
    long callStart = System.nanoTime();
    CompletableFuture<OutputT> executeFuture = wrapped.execute(input, context);
    executeFuture.whenComplete((r, t) -> {
        long duration = System.nanoTime() - callStart;
        metricCollector.reportMetric(CoreMetric.API_CALL_DURATION, Duration.ofNanos(duration));
        if (t != null) {
            future.completeExceptionally(t);
        } else {
            future.complete(r);
        }
    });
    return CompletableFutureUtils.forwardExceptionTo(future, executeFuture);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MetricCollector(software.amazon.awssdk.metrics.MetricCollector)

Example 8 with MetricCollector

use of software.amazon.awssdk.metrics.MetricCollector in project aws-sdk-java-v2 by aws.

the class MakeAsyncHttpRequestStage method executeHttpRequest.

private CompletableFuture<Response<OutputT>> executeHttpRequest(SdkHttpFullRequest request, RequestExecutionContext context) {
    CompletableFuture<Response<OutputT>> responseFuture = new CompletableFuture<>();
    CompletableFuture<Response<OutputT>> responseHandlerFuture = responseHandler.prepare();
    SdkHttpContentPublisher requestProvider = context.requestProvider() == null ? new SimpleHttpContentPublisher(request) : new SdkHttpContentPublisherAdapter(context.requestProvider());
    // Set content length if it hasn't been set already.
    SdkHttpFullRequest requestWithContentLength = getRequestWithContentLength(request, requestProvider);
    MetricCollector httpMetricCollector = MetricUtils.createHttpMetricsCollector(context);
    AsyncExecuteRequest.Builder executeRequestBuilder = AsyncExecuteRequest.builder().request(requestWithContentLength).requestContentPublisher(requestProvider).responseHandler(responseHandler).fullDuplex(isFullDuplex(context.executionAttributes())).metricCollector(httpMetricCollector);
    if (context.executionAttributes().getAttribute(SDK_HTTP_EXECUTION_ATTRIBUTES) != null) {
        executeRequestBuilder.httpExecutionAttributes(context.executionAttributes().getAttribute(SDK_HTTP_EXECUTION_ATTRIBUTES));
    }
    CompletableFuture<Void> httpClientFuture = doExecuteHttpRequest(context, executeRequestBuilder.build());
    TimeoutTracker timeoutTracker = setupAttemptTimer(responseFuture, context);
    context.apiCallAttemptTimeoutTracker(timeoutTracker);
    // Forward the cancellation
    responseFuture.whenComplete((r, t) -> {
        if (t != null) {
            httpClientFuture.completeExceptionally(t);
        }
    });
    // Offload the completion of the future returned from this stage onto
    // the future completion executor
    responseHandlerFuture.whenCompleteAsync((r, t) -> {
        if (t == null) {
            responseFuture.complete(r);
        } else {
            responseFuture.completeExceptionally(t);
        }
    }, futureCompletionExecutor);
    return responseFuture;
}
Also used : SimpleHttpContentPublisher(software.amazon.awssdk.core.internal.http.async.SimpleHttpContentPublisher) Response(software.amazon.awssdk.core.Response) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) CompletableFuture(java.util.concurrent.CompletableFuture) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) TimeoutTracker(software.amazon.awssdk.core.internal.http.timers.TimeoutTracker) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher)

Example 9 with MetricCollector

use of software.amazon.awssdk.metrics.MetricCollector in project aws-sdk-java-v2 by aws.

the class SigningStage method signRequest.

/**
 * Sign the request if the signer if provided and credentials are present.
 */
private SdkHttpFullRequest signRequest(SdkHttpFullRequest request, RequestExecutionContext context) throws Exception {
    updateInterceptorContext(request, context.executionContext());
    Signer signer = context.signer();
    MetricCollector metricCollector = context.attemptMetricCollector();
    if (shouldSign(signer)) {
        adjustForClockSkew(context.executionAttributes());
        Pair<SdkHttpFullRequest, Duration> measuredSign = MetricUtils.measureDuration(() -> signer.sign(request, context.executionAttributes()));
        metricCollector.reportMetric(CoreMetric.SIGNING_DURATION, measuredSign.right());
        SdkHttpFullRequest signedRequest = measuredSign.left();
        if (signer instanceof AsyncRequestBodySigner) {
            // Transform request body provider with signing operator
            AsyncRequestBody transformedRequestProvider = ((AsyncRequestBodySigner) signer).signAsyncRequestBody(signedRequest, context.requestProvider(), context.executionAttributes());
            context.requestProvider(transformedRequestProvider);
        }
        updateInterceptorContext(signedRequest, context.executionContext());
        return signedRequest;
    }
    return request;
}
Also used : AsyncRequestBodySigner(software.amazon.awssdk.core.signer.AsyncRequestBodySigner) Signer(software.amazon.awssdk.core.signer.Signer) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Duration(java.time.Duration) AsyncRequestBodySigner(software.amazon.awssdk.core.signer.AsyncRequestBodySigner)

Example 10 with MetricCollector

use of software.amazon.awssdk.metrics.MetricCollector in project aws-sdk-java-v2 by aws.

the class ApacheHttpClient method prepareRequest.

@Override
public ExecutableHttpRequest prepareRequest(HttpExecuteRequest request) {
    MetricCollector metricCollector = request.metricCollector().orElseGet(NoOpMetricCollector::create);
    metricCollector.reportMetric(HTTP_CLIENT_NAME, clientName());
    HttpRequestBase apacheRequest = toApacheRequest(request);
    return new ExecutableHttpRequest() {

        @Override
        public HttpExecuteResponse call() throws IOException {
            HttpExecuteResponse executeResponse = execute(apacheRequest, metricCollector);
            collectPoolMetric(metricCollector);
            return executeResponse;
        }

        @Override
        public void abort() {
            apacheRequest.abort();
        }
    };
}
Also used : NoOpMetricCollector(software.amazon.awssdk.metrics.NoOpMetricCollector) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) NoOpMetricCollector(software.amazon.awssdk.metrics.NoOpMetricCollector) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest)

Aggregations

MetricCollector (software.amazon.awssdk.metrics.MetricCollector)115 NoOpMetricCollector (software.amazon.awssdk.metrics.NoOpMetricCollector)65 MetricPublisher (software.amazon.awssdk.metrics.MetricPublisher)64 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)59 ClientExecutionParams (software.amazon.awssdk.core.client.handler.ClientExecutionParams)47 CompletableFuture (java.util.concurrent.CompletableFuture)37 JsonOperationMetadata (software.amazon.awssdk.protocols.json.JsonOperationMetadata)35 Collections (java.util.Collections)34 List (java.util.List)34 Logger (org.slf4j.Logger)34 LoggerFactory (org.slf4j.LoggerFactory)34 Generated (software.amazon.awssdk.annotations.Generated)34 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)34 AwsAsyncClientHandler (software.amazon.awssdk.awscore.client.handler.AwsAsyncClientHandler)34 RequestOverrideConfiguration (software.amazon.awssdk.core.RequestOverrideConfiguration)34 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)34 SdkClientOption (software.amazon.awssdk.core.client.config.SdkClientOption)34 AsyncClientHandler (software.amazon.awssdk.core.client.handler.AsyncClientHandler)34 HttpResponseHandler (software.amazon.awssdk.core.http.HttpResponseHandler)34 CoreMetric (software.amazon.awssdk.core.metrics.CoreMetric)34