Search in sources :

Example 1 with CLOUD_RESOURCE_REQUEST_DATA_KEY

use of bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResourceCleanupRecorder method logRequest.

@Override
public Mono<Void> logRequest(ClientLogger logger, HttpRequestLoggingContext loggingOptions) {
    final Context context = loggingOptions.getContext();
    Optional.ofNullable(context).flatMap(c -> c.getData(CLOUD_RESOURCE_REQUEST_DATA_KEY)).ifPresent(data -> {
        ResourceManagerRequestData requestData = (ResourceManagerRequestData) data;
        requestData.resourceUidCreation().ifPresent(resourceUid -> CleanupRecorder.record(resourceUid, requestData.resourceCreationMetadata().orElse(null), clientConfig));
    });
    return Mono.empty();
}
Also used : HttpRequestLoggingContext(com.azure.core.http.policy.HttpRequestLoggingContext) Context(com.azure.core.util.Context) HttpRequestLogger(com.azure.core.http.policy.HttpRequestLogger) HttpRequestLoggingContext(com.azure.core.http.policy.HttpRequestLoggingContext) Context(com.azure.core.util.Context) ClientLogger(com.azure.core.util.logging.ClientLogger) ClientConfig(bio.terra.cloudres.common.ClientConfig) CleanupRecorder(bio.terra.cloudres.common.cleanup.CleanupRecorder) Optional(java.util.Optional) Mono(reactor.core.publisher.Mono) CLOUD_RESOURCE_REQUEST_DATA_KEY(bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)

Example 2 with CLOUD_RESOURCE_REQUEST_DATA_KEY

use of bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY 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)2 ClientConfig (bio.terra.cloudres.common.ClientConfig)2 ClientLogger (com.azure.core.util.logging.ClientLogger)2 Optional (java.util.Optional)2 Mono (reactor.core.publisher.Mono)2 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1 OperationData (bio.terra.cloudres.common.OperationData)1 CleanupRecorder (bio.terra.cloudres.common.cleanup.CleanupRecorder)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 HttpRequestLogger (com.azure.core.http.policy.HttpRequestLogger)1 HttpRequestLoggingContext (com.azure.core.http.policy.HttpRequestLoggingContext)1 HttpResponseLogger (com.azure.core.http.policy.HttpResponseLogger)1 HttpResponseLoggingContext (com.azure.core.http.policy.HttpResponseLoggingContext)1 Context (com.azure.core.util.Context)1 CoreUtils (com.azure.core.util.CoreUtils)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Charsets (com.google.common.base.Charsets)1