Search in sources :

Example 11 with MetricCollector

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

the class MetricReportingTest method prepareRequest_connectionManagerNotPooling_callableCalled_metricsReported.

@Test
public void prepareRequest_connectionManagerNotPooling_callableCalled_metricsReported() throws IOException {
    ApacheHttpClient client = newClient();
    when(mockHttpClient.getHttpClientConnectionManager()).thenReturn(mock(HttpClientConnectionManager.class));
    MetricCollector collector = MetricCollector.create("test");
    HttpExecuteRequest executeRequest = newRequest(collector);
    client.prepareRequest(executeRequest).call();
    MetricCollection collected = collector.collect();
    assertThat(collected.metricValues(HTTP_CLIENT_NAME)).containsExactly("Apache");
    assertThat(collected.metricValues(LEASED_CONCURRENCY)).isEmpty();
    assertThat(collected.metricValues(PENDING_CONCURRENCY_ACQUIRES)).isEmpty();
    assertThat(collected.metricValues(AVAILABLE_CONCURRENCY)).isEmpty();
    assertThat(collected.metricValues(MAX_CONCURRENCY)).isEmpty();
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) MetricCollection(software.amazon.awssdk.metrics.MetricCollection) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) Test(org.junit.Test)

Example 12 with MetricCollector

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

the class MetricReportingTest method prepareRequest_callableCalled_metricsReported.

@Test
public void prepareRequest_callableCalled_metricsReported() throws IOException {
    ApacheHttpClient client = newClient();
    MetricCollector collector = MetricCollector.create("test");
    HttpExecuteRequest executeRequest = newRequest(collector);
    client.prepareRequest(executeRequest).call();
    MetricCollection collected = collector.collect();
    assertThat(collected.metricValues(HTTP_CLIENT_NAME)).containsExactly("Apache");
    assertThat(collected.metricValues(LEASED_CONCURRENCY)).containsExactly(1);
    assertThat(collected.metricValues(PENDING_CONCURRENCY_ACQUIRES)).containsExactly(2);
    assertThat(collected.metricValues(AVAILABLE_CONCURRENCY)).containsExactly(3);
    assertThat(collected.metricValues(MAX_CONCURRENCY)).containsExactly(4);
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) MetricCollection(software.amazon.awssdk.metrics.MetricCollection) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) Test(org.junit.Test)

Example 13 with MetricCollector

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

the class MakeHttpRequestStageTest method testExecute_contextContainsMetricCollector_addsChildToExecuteRequest.

@Test
public void testExecute_contextContainsMetricCollector_addsChildToExecuteRequest() {
    SdkHttpFullRequest sdkRequest = SdkHttpFullRequest.builder().method(SdkHttpMethod.GET).host("mybucket.s3.us-west-2.amazonaws.com").protocol("https").build();
    MetricCollector mockCollector = mock(MetricCollector.class);
    MetricCollector childCollector = mock(MetricCollector.class);
    when(mockCollector.createChild(any(String.class))).thenReturn(childCollector);
    ExecutionContext executionContext = ExecutionContext.builder().executionAttributes(new ExecutionAttributes()).build();
    RequestExecutionContext context = RequestExecutionContext.builder().originalRequest(ValidSdkObjects.sdkRequest()).executionContext(executionContext).build();
    context.attemptMetricCollector(mockCollector);
    context.apiCallAttemptTimeoutTracker(mock(TimeoutTracker.class));
    context.apiCallTimeoutTracker(mock(TimeoutTracker.class));
    try {
        stage.execute(sdkRequest, context);
    } catch (Exception e) {
    // ignored, don't really care about successful execution of the stage in this case
    } finally {
        ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
        verify(mockCollector).createChild(eq("HttpClient"));
        verify(mockClient).prepareRequest(httpRequestCaptor.capture());
        assertThat(httpRequestCaptor.getValue().metricCollector()).contains(childCollector);
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) RequestExecutionContext(software.amazon.awssdk.core.internal.http.RequestExecutionContext) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) TimeoutTracker(software.amazon.awssdk.core.internal.http.timers.TimeoutTracker) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) RequestExecutionContext(software.amazon.awssdk.core.internal.http.RequestExecutionContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with MetricCollector

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

the class CloudWatchMetricPublisherTest method metricLevelSettingIsHonored.

@Test
public void metricLevelSettingIsHonored() {
    try (CloudWatchMetricPublisher publisher = publisherBuilder.metricLevel(MetricLevel.INFO).build()) {
        MetricCollector collector = newCollector();
        collector.reportMetric(CoreMetric.SERVICE_ID, "ServiceId");
        collector.reportMetric(CoreMetric.API_CALL_SUCCESSFUL, true);
        collector.reportMetric(HttpMetric.HTTP_STATUS_CODE, 404);
        publisher.publish(new FixedTimeMetricCollection(collector.collect()));
    }
    PutMetricDataRequest call = getPutMetricCall();
    MetricDatum metric = call.metricData().get(0);
    assertThat(call.metricData()).hasSize(1);
    assertThat(metric.dimensions()).containsExactly(Dimension.builder().name(CoreMetric.SERVICE_ID.name()).value("ServiceId").build());
    assertThat(metric.metricName()).isEqualTo(CoreMetric.API_CALL_SUCCESSFUL.name());
}
Also used : PutMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) MetricDatum(software.amazon.awssdk.services.cloudwatch.model.MetricDatum) Test(org.junit.Test)

Example 15 with MetricCollector

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

the class CloudWatchMetricPublisherTest method closeWaitsForUploadToComplete.

@Test(timeout = 10_000)
public void closeWaitsForUploadToComplete() throws InterruptedException {
    CountDownLatch cloudwatchPutCalledLatch = new CountDownLatch(1);
    CompletableFuture<PutMetricDataResponse> result = new CompletableFuture<>();
    CloudWatchAsyncClient cloudWatch = Mockito.mock(CloudWatchAsyncClient.class);
    try (CloudWatchMetricPublisher publisher = CloudWatchMetricPublisher.builder().cloudWatchClient(cloudWatch).uploadFrequency(Duration.ofMinutes(60)).build()) {
        MetricCollector collector = newCollector();
        collector.reportMetric(HttpMetric.AVAILABLE_CONCURRENCY, 5);
        publisher.publish(new FixedTimeMetricCollection(collector.collect()));
        Mockito.when(cloudWatch.putMetricData(any(PutMetricDataRequest.class))).thenAnswer(x -> {
            cloudwatchPutCalledLatch.countDown();
            return result;
        });
        publisher.publish(MetricCollector.create("test").collect());
        Thread closeThread = new Thread(publisher::close);
        assertThat(publisher.isShutdown()).isFalse();
        closeThread.start();
        // Wait until cloudwatch is called
        cloudwatchPutCalledLatch.await();
        // Wait to make sure the close thread seems to be waiting for the cloudwatch call to complete
        Thread.sleep(1_000);
        assertThat(closeThread.isAlive()).isTrue();
        // Complete the cloudwatch call
        result.complete(null);
        // Make sure the close thread finishes
        closeThread.join(5_000);
        assertThat(closeThread.isAlive()).isFalse();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CloudWatchAsyncClient(software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient) PutMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest) PutMetricDataResponse(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataResponse) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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