Search in sources :

Example 1 with RedirectPolicy

use of com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy in project ApplicationInsights-Java by microsoft.

the class TelemetryChannel method internalSend.

/**
 * Object can be a list of {@link ByteBuffer} or a raw byte array. Regular telemetries will be
 * sent as {@code List<ByteBuffer>}. Persisted telemetries will be sent as byte[]
 */
private CompletableResultCode internalSend(List<ByteBuffer> byteBuffers, String instrumentationKey, Runnable onSuccess, Consumer<Boolean> onFailure, OperationLogger operationLogger) {
    HttpRequest request = new HttpRequest(HttpMethod.POST, endpointUrl);
    request.setBody(Flux.fromIterable(byteBuffers));
    int contentLength = byteBuffers.stream().mapToInt(ByteBuffer::limit).sum();
    request.setHeader("Content-Length", Integer.toString(contentLength));
    // need to suppress the default User-Agent "ReactorNetty/dev", otherwise Breeze ingestion
    // service will put that
    // User-Agent header into the client_Browser field for all telemetry that doesn't explicitly set
    // it's own
    // UserAgent (ideally Breeze would only have this behavior for ingestion directly from browsers)
    // TODO(trask)
    // not setting User-Agent header at all would be a better option, but haven't figured out how
    // to do that yet
    request.setHeader("User-Agent", "");
    request.setHeader("Content-Encoding", "gzip");
    // TODO(trask) subscribe with listener
    // * retry on first failure (may not need to worry about this if retry policy in pipeline
    // already, see above)
    // * write to disk on second failure
    CompletableResultCode result = new CompletableResultCode();
    final long startTime = System.currentTimeMillis();
    // Add instrumentation key to context to use in redirectPolicy
    Map<Object, Object> contextKeyValues = new HashMap<>();
    contextKeyValues.put(RedirectPolicy.INSTRUMENTATION_KEY, instrumentationKey);
    contextKeyValues.put(Tracer.DISABLE_TRACING_KEY, true);
    pipeline.send(request, Context.of(contextKeyValues)).subscribe(responseHandler(instrumentationKey, startTime, () -> {
        onSuccess.run();
        result.succeed();
    }, retryable -> {
        onFailure.accept(retryable);
        result.fail();
    }, operationLogger), errorHandler(instrumentationKey, retryable -> {
        onFailure.accept(retryable);
        result.fail();
    }, operationLogger));
    return result;
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) RedirectPolicy(com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy) Context(com.azure.core.util.Context) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) URL(java.net.URL) HttpPipeline(com.azure.core.http.HttpPipeline) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Collections.singletonList(java.util.Collections.singletonList) LocalFileWriter(com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileWriter) ArrayList(java.util.ArrayList) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Tracer(com.azure.core.util.tracing.Tracer) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Nullable(javax.annotation.Nullable) NetworkFriendlyExceptions(com.microsoft.applicationinsights.agent.internal.common.NetworkFriendlyExceptions) Logger(org.slf4j.Logger) Configuration(com.microsoft.applicationinsights.agent.internal.configuration.Configuration) StringWriter(java.io.StringWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) UnknownHostException(java.net.UnknownHostException) OperationLogger(com.microsoft.applicationinsights.agent.internal.common.OperationLogger) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpRequest(com.azure.core.http.HttpRequest) LazyHttpClient(com.microsoft.applicationinsights.agent.internal.httpclient.LazyHttpClient) Cache(io.opentelemetry.instrumentation.api.cache.Cache) HttpMethod(com.azure.core.http.HttpMethod) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) GZIPOutputStream(java.util.zip.GZIPOutputStream) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) HashMap(java.util.HashMap) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode)

Example 2 with RedirectPolicy

use of com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy 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)

Aggregations

RedirectPolicy (com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy)2 LocalFileWriter (com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileWriter)2 StatsbeatModule (com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 HttpMethod (com.azure.core.http.HttpMethod)1 HttpPipeline (com.azure.core.http.HttpPipeline)1 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)1 HttpRequest (com.azure.core.http.HttpRequest)1 HttpResponse (com.azure.core.http.HttpResponse)1 HttpPipelinePolicy (com.azure.core.http.policy.HttpPipelinePolicy)1 Context (com.azure.core.util.Context)1 Tracer (com.azure.core.util.tracing.Tracer)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SerializedString (com.fasterxml.jackson.core.io.SerializedString)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1