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);
}
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));
}
Aggregations