Search in sources :

Example 26 with HttpRequest

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

the class QuickPulseDataFetcher method prepareQuickPulseDataForSend.

public void prepareQuickPulseDataForSend(String redirectedEndpoint) {
    try {
        QuickPulseDataCollector.FinalCounters counters = QuickPulseDataCollector.INSTANCE.getAndRestart();
        Date currentDate = new Date();
        String endpointPrefix = Strings.isNullOrEmpty(redirectedEndpoint) ? getQuickPulseEndpoint() : redirectedEndpoint;
        HttpRequest request = networkHelper.buildRequest(currentDate, this.getEndpointUrl(endpointPrefix));
        request.setBody(buildPostEntity(counters));
        if (!sendQueue.offer(request)) {
            logger.trace("Quick Pulse send queue is full");
        }
    } catch (ThreadDeath td) {
        throw td;
    } catch (Throwable e) {
        try {
            logger.error("Quick Pulse failed to prepare data for send", e);
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable t2) {
        // chomp
        }
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) Date(java.util.Date)

Example 27 with HttpRequest

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

the class QuickPulseNetworkHelper method buildPingRequest.

public HttpRequest buildPingRequest(Date currentDate, String address, String quickPulseId, String machineName, String roleName, String instanceName) {
    HttpRequest request = buildRequest(currentDate, address);
    request.setHeader(QPS_ROLE_NAME_HEADER, roleName);
    request.setHeader(QPS_MACHINE_NAME_HEADER, machineName);
    request.setHeader(QPS_STREAM_ID_HEADER, quickPulseId);
    request.setHeader(QPS_INSTANCE_NAME_HEADER, instanceName);
    request.setHeader(QPS_INVARIANT_VERSION_HEADER, Integer.toString(QuickPulse.QP_INVARIANT_VERSION));
    return request;
}
Also used : HttpRequest(com.azure.core.http.HttpRequest)

Example 28 with HttpRequest

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

the class QuickPulsePingSender method ping.

public QuickPulseHeaderInfo ping(String redirectedEndpoint) {
    String instrumentationKey = getInstrumentationKey();
    if (Strings.isNullOrEmpty(instrumentationKey)) {
        // turn off quick pulse.
        return new QuickPulseHeaderInfo(QuickPulseStatus.QP_IS_OFF);
    }
    Date currentDate = new Date();
    String endpointPrefix = Strings.isNullOrEmpty(redirectedEndpoint) ? getQuickPulseEndpoint() : redirectedEndpoint;
    HttpRequest request = networkHelper.buildPingRequest(currentDate, getQuickPulsePingUri(endpointPrefix), quickPulseId, machineName, telemetryClient.getRoleName(), instanceName);
    long sendTime = System.nanoTime();
    HttpResponse response = null;
    try {
        request.setBody(buildPingEntity(currentDate.getTime()));
        response = httpPipeline.send(request).block();
        if (response == null) {
            // this shouldn't happen, the mono should complete with a response or a failure
            throw new AssertionError("http response mono returned empty");
        }
        // response body is not consumed below
        LazyHttpClient.consumeResponseBody(response);
        if (networkHelper.isSuccess(response)) {
            QuickPulseHeaderInfo quickPulseHeaderInfo = networkHelper.getQuickPulseHeaderInfo(response);
            switch(quickPulseHeaderInfo.getQuickPulseStatus()) {
                case QP_IS_OFF:
                case QP_IS_ON:
                    lastValidTransmission = sendTime;
                    operationLogger.recordSuccess();
                    return quickPulseHeaderInfo;
                default:
                    break;
            }
        }
    } catch (Throwable t) {
        if (!NetworkFriendlyExceptions.logSpecialOneTimeFriendlyException(t, getQuickPulseEndpoint(), friendlyExceptionThrown, logger)) {
            operationLogger.recordFailure(t.getMessage(), t);
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
    return onPingError(sendTime);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) Date(java.util.Date)

Example 29 with HttpRequest

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

the class AzureMetadataService method run.

@Override
public void run() {
    HttpRequest request = new HttpRequest(HttpMethod.GET, ENDPOINT);
    request.setHeader("Metadata", "true");
    HttpResponse response;
    try {
        response = LazyHttpClient.getInstance().send(request).block();
    } catch (RuntimeException e) {
        logger.debug("Shutting down AzureMetadataService scheduler: is not running on Azure VM or VMSS");
        logger.trace(e.getMessage(), e);
        scheduledExecutor.shutdown();
        return;
    }
    if (response == null) {
        // this shouldn't happen, the mono should complete with a response or a failure
        throw new AssertionError("http response mono returned empty");
    }
    String json = response.getBodyAsString().block();
    if (json == null) {
        // this shouldn't happen, the mono should complete with a response or a failure
        throw new AssertionError("response body mono returned empty");
    }
    MetadataInstanceResponse metadataInstanceResponse;
    try {
        metadataInstanceResponse = mapper.readValue(json, MetadataInstanceResponse.class);
    } catch (IOException e) {
        logger.debug("Shutting down AzureMetadataService scheduler:" + " error parsing response from Azure Metadata Service: {}", json, e);
        scheduledExecutor.shutdown();
        return;
    }
    updateMetadata(metadataInstanceResponse);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) IOException(java.io.IOException)

Example 30 with HttpRequest

use of com.azure.core.http.HttpRequest in project lowkey-vault by nagyesta.

the class LowkeyVaultManagementClientImpl method recover.

@Override
public VaultModel recover(@NonNull final URI baseUri) {
    final Map<String, String> parameters = Map.of(BASE_URI_QUERY_PARAM, baseUri.toString());
    final URI uri = UriUtil.uriBuilderForPath(vaultUrl, MANAGEMENT_VAULT_RECOVERY_PATH, parameters);
    final HttpRequest request = new HttpRequest(HttpMethod.PUT, uri.toString()).setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
    return sendAndProcess(request, r -> r.getResponseObject(VaultModel.class));
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) URI(java.net.URI)

Aggregations

HttpRequest (com.azure.core.http.HttpRequest)31 Test (org.junit.jupiter.api.Test)13 HttpMethod (com.azure.core.http.HttpMethod)9 HttpResponse (com.azure.core.http.HttpResponse)9 HttpClient (com.azure.core.http.HttpClient)8 URI (java.net.URI)8 URL (java.net.URL)8 HttpHeaders (com.azure.core.http.HttpHeaders)7 IOException (java.io.IOException)7 Mono (reactor.core.publisher.Mono)7 Date (java.util.Date)6 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 StandardCharsets (java.nio.charset.StandardCharsets)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpPipeline (com.azure.core.http.HttpPipeline)3 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)3 WireMockConfiguration (com.github.tomakehurst.wiremock.core.WireMockConfiguration)3 Duration (java.time.Duration)3 HashMap (java.util.HashMap)3