Search in sources :

Example 11 with HttpPipelineBuilder

use of com.azure.core.http.HttpPipelineBuilder in project azure-maven-plugins by microsoft.

the class AzureFunctionsResourceManager method getClient.

public static AzureFunctionsResourceManager getClient(@Nonnull WebAppBase functionApp, @Nonnull IFunctionAppBase appService) {
    // com/azure/resourcemanager/appservice/implementation/KuduClient.java
    if (!(functionApp instanceof FunctionApp || functionApp instanceof FunctionDeploymentSlot)) {
        throw new AzureToolkitRuntimeException("Functions resource manager only applies to Azure Functions");
    }
    final List<HttpPipelinePolicy> policies = Utils.getPolicyFromPipeline(functionApp.manager().httpPipeline(), policy -> !(policy instanceof AuthenticationPolicy || policy instanceof ProviderRegistrationPolicy || policy instanceof AuxiliaryAuthenticationPolicy));
    policies.add(new AddHeadersPolicy(new HttpHeaders(Collections.singletonMap("x-functions-key", appService.getMasterKey()))));
    final HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(functionApp.manager().httpPipeline().getHttpClient()).build();
    final FunctionsService functionsService = RestProxy.create(FunctionsService.class, httpPipeline, SerializerFactory.createDefaultManagementSerializerAdapter());
    return new AzureFunctionsResourceManager(functionsService, appService);
}
Also used : HttpHeaders(com.azure.core.http.HttpHeaders) AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) FunctionApp(com.azure.resourcemanager.appservice.models.FunctionApp) HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) FunctionDeploymentSlot(com.azure.resourcemanager.appservice.models.FunctionDeploymentSlot) AuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuthenticationPolicy) AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) HttpPipeline(com.azure.core.http.HttpPipeline) AddHeadersPolicy(com.azure.core.http.policy.AddHeadersPolicy)

Example 12 with HttpPipelineBuilder

use of com.azure.core.http.HttpPipelineBuilder 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 13 with HttpPipelineBuilder

use of com.azure.core.http.HttpPipelineBuilder in project ApplicationInsights-Java by microsoft.

the class TelemetryChannelTest method getTelemetryChannel.

private TelemetryChannel getTelemetryChannel() throws MalformedURLException {
    List<HttpPipelinePolicy> policies = new ArrayList<>();
    policies.add(new RedirectPolicy(Cache.bounded(5)));
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(recordingHttpClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    StatsbeatModule mockedStatsModule = Mockito.mock(StatsbeatModule.class);
    when(mockedStatsModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
    return new TelemetryChannel(pipelineBuilder.build(), new URL(END_POINT_URL), new LocalFileWriter(localFileCache, tempFolder, null), mockedStatsModule, false);
}
Also used : HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) RedirectPolicy(com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy) LocalFileCache(com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileCache) NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) LocalFileWriter(com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileWriter) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 14 with HttpPipelineBuilder

use of com.azure.core.http.HttpPipelineBuilder in project ApplicationInsights-Java by microsoft.

the class LocalFileLoaderTests method testDeleteFilePermanentlyOnSuccess.

@Test
public void testDeleteFilePermanentlyOnSuccess() throws Exception {
    HttpClient mockedClient = getMockHttpClientSuccess();
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
    LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
    StatsbeatModule mockedStatsbeatModule = Mockito.mock(StatsbeatModule.class);
    when(mockedStatsbeatModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
    TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, mockedStatsbeatModule, false);
    // persist 10 files to disk
    for (int i = 0; i < 10; i++) {
        localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
    Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    int expectedCount = 10;
    // send persisted files one by one and then delete it permanently.
    for (int i = 0; i < 10; i++) {
        LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
        CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
        }, retryable -> {
        });
        completableResultCode.join(10, SECONDS);
        assertThat(completableResultCode.isSuccess()).isEqualTo(true);
        localFileLoader.updateProcessedFileStatus(true, persistedFile.file);
        // sleep 1 second to wait for delete to complete
        Thread.sleep(1000);
        files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
        assertThat(files.size()).isEqualTo(--expectedCount);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(0);
}
Also used : NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryChannel(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel) URL(java.net.URL) HttpClient(com.azure.core.http.HttpClient) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 15 with HttpPipelineBuilder

use of com.azure.core.http.HttpPipelineBuilder in project ApplicationInsights-Java by microsoft.

the class LocalFileLoaderTests method testDeleteFilePermanentlyOnFailure.

@Test
public void testDeleteFilePermanentlyOnFailure() throws Exception {
    HttpClient mockedClient = mock(HttpClient.class);
    when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.error(() -> new Exception("this is expected to be logged by the operation logger")));
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
    LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
    StatsbeatModule statsbeatModule = mock(StatsbeatModule.class);
    when(statsbeatModule.getNetworkStatsbeat()).thenReturn(mock(NetworkStatsbeat.class));
    TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, statsbeatModule, false);
    // persist 10 files to disk
    for (int i = 0; i < 10; i++) {
        localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
    Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    // fail to send persisted files and expect them to be kept on disk
    for (int i = 0; i < 10; i++) {
        LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
        assertThat(persistedFile.instrumentationKey).isEqualTo(INSTRUMENTATION_KEY);
        CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
        }, retryable -> {
        });
        completableResultCode.join(10, SECONDS);
        assertThat(completableResultCode.isSuccess()).isEqualTo(false);
        localFileLoader.updateProcessedFileStatus(false, persistedFile.file);
    }
    files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) Context(com.azure.core.util.Context) NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryChannel(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel) IOException(java.io.IOException) URL(java.net.URL) HttpClient(com.azure.core.http.HttpClient) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)15 HttpPipeline (com.azure.core.http.HttpPipeline)7 HttpPipelinePolicy (com.azure.core.http.policy.HttpPipelinePolicy)7 URL (java.net.URL)7 Test (org.junit.jupiter.api.Test)7 HttpHeaders (com.azure.core.http.HttpHeaders)6 HttpRequest (com.azure.core.http.HttpRequest)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Mono (reactor.core.publisher.Mono)5 HttpClient (com.azure.core.http.HttpClient)4 NetworkStatsbeat (com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat)4 StatsbeatModule (com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule)4 IOException (java.io.IOException)4 HttpMethod (com.azure.core.http.HttpMethod)3 HttpLoggingPolicy (com.azure.core.http.policy.HttpLoggingPolicy)3 TelemetryChannel (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel)3 ProfilerFrontendClientV2 (com.microsoft.applicationinsights.serviceprofilerapi.client.ProfilerFrontendClientV2)3 ArtifactAcceptedResponse (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse)3