Search in sources :

Example 1 with OperationAnnotator

use of bio.terra.cloudres.common.OperationAnnotator 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)

Aggregations

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 OperationData (bio.terra.cloudres.common.OperationData)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 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Charsets (com.google.common.base.Charsets)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Channels (java.nio.channels.Channels)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1