Search in sources :

Example 1 with ArtifactAcceptedResponse

use of com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse in project ApplicationInsights-Java by microsoft.

the class ProfilerFrontendClientV2Test method uploadFinishedHitsCorrectUrl.

@Test
void uploadFinishedHitsCorrectUrl() throws IOException {
    AtomicReference<HttpRequest> requestHolder = new AtomicReference<>();
    HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(request -> {
        requestHolder.set(request);
        return Mono.just(mockResponse(request, 201, "{\"acceptedTime\":\"a-time\",\"blobUri\":\"a-blob-uri\",\"correlationId\":\"a-correlation-id\",\"stampId\":\"a-stamp\"}"));
    }).build();
    ProfilerFrontendClientV2 profilerFrontendClientV2 = new ProfilerFrontendClientV2(new URL("http://a-host"), "a-instrumentation-key", httpPipeline);
    UUID id = UUID.randomUUID();
    ArtifactAcceptedResponse artifactAcceptedResponse = profilerFrontendClientV2.reportUploadFinish(id, "an-etag").block();
    HttpRequest request = requestHolder.get();
    String url = request.getUrl().toString();
    assertThat(request.getHttpMethod()).isEqualTo(HttpMethod.POST);
    assertThat(url.contains("/api/apps/a-instrumentation-key/artifactkinds/profile/artifacts/" + id)).isTrue();
    assertThat(url.contains("action=commit")).isTrue();
    assertThat(artifactAcceptedResponse.getAcceptedTime()).isEqualTo("a-time");
    assertThat(artifactAcceptedResponse.getBlobUri()).isEqualTo("a-blob-uri");
    assertThat(artifactAcceptedResponse.getCorrelationId()).isEqualTo("a-correlation-id");
    assertThat(artifactAcceptedResponse.getStampId()).isEqualTo("a-stamp");
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) BlobAccessPass(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass) URL(java.net.URL) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpPipeline(com.azure.core.http.HttpPipeline) IOException(java.io.IOException) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) UUID(java.util.UUID) Instant(java.time.Instant) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) HttpRequest(com.azure.core.http.HttpRequest) HttpMethod(com.azure.core.http.HttpMethod) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) ProfilerFrontendClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ProfilerFrontendClientV2) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) HttpPipeline(com.azure.core.http.HttpPipeline) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProfilerFrontendClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ProfilerFrontendClientV2) UUID(java.util.UUID) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 2 with ArtifactAcceptedResponse

use of com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse in project ApplicationInsights-Java by microsoft.

the class ProfilerFrontendClientV2 method reportUploadFinish.

/**
 * Report to Service Profiler that the profile upload has been completed.
 */
@Override
public Mono<ArtifactAcceptedResponse> reportUploadFinish(UUID profileId, String etag) {
    URL requestUrl = uploadFinishedRequestUrl(profileId, etag);
    return executePostWithRedirect(requestUrl).flatMap(response -> {
        if (response == null) {
            // this shouldn't happen, the mono should complete with a response or a failure
            return Mono.error(new AssertionError("http response mono returned empty"));
        }
        int statusCode = response.getStatusCode();
        if (statusCode != 201 && statusCode != 202) {
            LOGGER.error("Trace upload failed: {}", statusCode);
            consumeResponseBody(response);
            return Mono.error(new AssertionError("http request failed"));
        }
        return response.getBodyAsString();
    }).flatMap(json -> {
        if (json == null) {
            // this shouldn't happen, the mono should complete with a response or a failure
            return Mono.error(new AssertionError("response body mono returned empty"));
        }
        try {
            JsonAdapter<ArtifactAcceptedResponse> builder = new Builder().build().adapter(ArtifactAcceptedResponse.class);
            ArtifactAcceptedResponse data = builder.fromJson(json);
            if (data == null) {
                return Mono.error(new IllegalStateException("Failed to deserialize response"));
            }
            return Mono.just(data);
        } catch (IOException e) {
            return Mono.error(new IllegalStateException("Failed to deserialize response", e));
        }
    });
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) JsonAdapter(com.squareup.moshi.JsonAdapter) BlobAccessPass(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass) TimestampContract(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.TimestampContract) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Date(java.util.Date) HttpPipeline(com.azure.core.http.HttpPipeline) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) HttpResponseException(com.azure.core.exception.HttpResponseException) HttpRequest(com.azure.core.http.HttpRequest) HttpMethod(com.azure.core.http.HttpMethod) Builder(com.squareup.moshi.Moshi.Builder) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) Builder(com.squareup.moshi.Moshi.Builder) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

HttpMethod (com.azure.core.http.HttpMethod)2 HttpPipeline (com.azure.core.http.HttpPipeline)2 HttpRequest (com.azure.core.http.HttpRequest)2 ArtifactAcceptedResponse (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse)2 BlobAccessPass (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Date (java.util.Date)2 UUID (java.util.UUID)2 Mono (reactor.core.publisher.Mono)2 HttpResponseException (com.azure.core.exception.HttpResponseException)1 HttpHeaders (com.azure.core.http.HttpHeaders)1 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)1 HttpResponse (com.azure.core.http.HttpResponse)1 ProfilerFrontendClientV2 (com.microsoft.applicationinsights.serviceprofilerapi.client.ProfilerFrontendClientV2)1 TimestampContract (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.TimestampContract)1 JsonAdapter (com.squareup.moshi.JsonAdapter)1 Builder (com.squareup.moshi.Moshi.Builder)1 MalformedURLException (java.net.MalformedURLException)1 StandardCharsets (java.nio.charset.StandardCharsets)1