Search in sources :

Example 1 with OperationData

use of bio.terra.cloudres.common.OperationData in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResponseLogger method logResponse.

@Override
public Mono<HttpResponse> logResponse(ClientLogger clientLogger, HttpResponseLoggingContext loggingOptions) {
    final HttpResponse response = loggingOptions.getHttpResponse();
    final HttpRequest request = response.getRequest();
    // Always add request method and request URL
    JsonObject requestDataJson = new JsonObject();
    requestDataJson.addProperty("requestMethod", request.getHttpMethod().toString());
    requestDataJson.addProperty("requestUrl", request.getUrl().toString());
    // Optionally add rich request data if provided in the logging context
    Optional<ResourceManagerRequestData> requestData = Optional.ofNullable(loggingOptions.getContext()).flatMap(c -> c.getData(CLOUD_RESOURCE_REQUEST_DATA_KEY)).map(o -> (ResourceManagerRequestData) o);
    requestData.ifPresent(d -> requestDataJson.add("requestBody", d.serialize()));
    // verbose.
    if (logger.isDebugEnabled()) {
        logBody(request.getHeaders(), request.getBody(), s -> requestDataJson.addProperty("rawRequestBody", s));
        logBody(response.getHeaders(), response.buffer().getBody(), s -> requestDataJson.addProperty("rawResponseBody", s));
    }
    // Build OperationData object.
    OperationData operationData = OperationData.builder().setDuration(Optional.ofNullable(loggingOptions.getResponseDuration()).orElse(Duration.ZERO)).setTryCount(OptionalInt.of(loggingOptions.getTryCount())).setExecutionException(Optional.empty()).setHttpStatusCode(OptionalInt.of(response.getStatusCode())).setCloudOperation(requestData.map(ResourceManagerRequestData::cloudOperation).orElse(ResourceManagerOperation.AZURE_RESOURCE_MANAGER_UNKNOWN_OPERATION)).setRequestData(requestDataJson).build();
    // Invoke OperationAnnotator to record the operation.
    operationAnnotator.recordOperation(operationData);
    return Mono.justOrEmpty(response);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) JsonObject(com.google.gson.JsonObject) HttpResponse(com.azure.core.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) CoreUtils(com.azure.core.util.CoreUtils) OptionalInt(java.util.OptionalInt) OperationData(bio.terra.cloudres.common.OperationData) ByteBuffer(java.nio.ByteBuffer) Duration(java.time.Duration) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) HttpResponseLogger(com.azure.core.http.policy.HttpResponseLogger) ClientLogger(com.azure.core.util.logging.ClientLogger) Channels(java.nio.channels.Channels) ClientConfig(bio.terra.cloudres.common.ClientConfig) OperationAnnotator(bio.terra.cloudres.common.OperationAnnotator) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) CLOUD_RESOURCE_REQUEST_DATA_KEY(bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) HttpRequest(com.azure.core.http.HttpRequest) HttpResponseLoggingContext(com.azure.core.http.policy.HttpResponseLoggingContext) ContentType(com.azure.core.http.ContentType) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OperationData(bio.terra.cloudres.common.OperationData) HttpResponse(com.azure.core.http.HttpResponse) JsonObject(com.google.gson.JsonObject)

Example 2 with OperationData

use of bio.terra.cloudres.common.OperationData in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResponseLoggerTest method testRecordEvent.

@Test
public void testRecordEvent() {
    // Send an Azure request to list resource groups
    List<ResourceGroup> resourceGroups = setUpResourceManager().resourceGroups().list().stream().collect(Collectors.toList());
    // Verify AzureResponseLogger was invoked
    verify(mockOperationAnnotator).recordOperation(operationDataCaptor.capture());
    // Verify OperationData values
    OperationData operationData = operationDataCaptor.getValue();
    assertThat(operationData.duration().toMillis(), Matchers.greaterThan(0L));
    assertTrue(operationData.tryCount().isPresent());
    assertEquals(OptionalInt.of(200), operationData.httpStatusCode());
    assertEquals(ResourceManagerOperation.AZURE_RESOURCE_MANAGER_UNKNOWN_OPERATION, operationData.cloudOperation());
    assertThat(operationData.requestData().entrySet(), Matchers.hasItem(Map.entry("requestMethod", new JsonPrimitive("GET"))));
    assertThat(operationData.requestData().keySet(), Matchers.hasItem("requestUrl"));
    // For good measure verify the Azure response contains our test resource group
    assertThat(resourceGroups.stream().map(ResourceGroup::name).collect(Collectors.toList()), Matchers.hasItem(AzureIntegrationUtils.DEFAULT_AZURE_RESOURCE_GROUP));
}
Also used : OperationData(bio.terra.cloudres.common.OperationData) JsonPrimitive(com.google.gson.JsonPrimitive) ResourceGroup(com.azure.resourcemanager.resources.models.ResourceGroup) Test(org.junit.jupiter.api.Test)

Aggregations

OperationData (bio.terra.cloudres.common.OperationData)2 CLOUD_RESOURCE_REQUEST_DATA_KEY (bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)1 ClientConfig (bio.terra.cloudres.common.ClientConfig)1 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1 ContentType (com.azure.core.http.ContentType)1 HttpHeaders (com.azure.core.http.HttpHeaders)1 HttpRequest (com.azure.core.http.HttpRequest)1 HttpResponse (com.azure.core.http.HttpResponse)1 HttpResponseLogger (com.azure.core.http.policy.HttpResponseLogger)1 HttpResponseLoggingContext (com.azure.core.http.policy.HttpResponseLoggingContext)1 CoreUtils (com.azure.core.util.CoreUtils)1 ClientLogger (com.azure.core.util.logging.ClientLogger)1 ResourceGroup (com.azure.resourcemanager.resources.models.ResourceGroup)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Charsets (com.google.common.base.Charsets)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1