io.micronaut.tracing.brave.sender.HttpClientSender Maven / Gradle / Ivy
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.tracing.brave.sender;
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.discovery.exceptions.NoAvailableServiceException;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.client.*;
import io.micronaut.http.client.netty.DefaultHttpClient;
import io.micronaut.tracing.brave.ZipkinServiceInstanceList;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import zipkin2.Call;
import zipkin2.Callback;
import zipkin2.CheckResult;
import zipkin2.codec.Encoding;
import zipkin2.reporter.Sender;
import javax.inject.Provider;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/**
* An {@link Sender} implementation that uses Micronaut's {@link io.micronaut.http.client.HttpClient}.
*
* @author graemerocher
* @since 1.0
*/
public final class HttpClientSender extends Sender {
private final Encoding encoding;
private final int messageMaxBytes;
private final boolean compressionEnabled;
private final URI endpoint;
private final Provider loadBalancerResolver;
private final HttpClientConfiguration clientConfiguration;
private HttpClient httpClient;
private HttpClientSender(
Encoding encoding,
int messageMaxBytes,
boolean compressionEnabled,
HttpClientConfiguration clientConfiguration,
Provider loadBalancerResolver,
String path) {
this.loadBalancerResolver = loadBalancerResolver;
this.clientConfiguration = clientConfiguration;
this.encoding = encoding;
this.messageMaxBytes = messageMaxBytes;
this.compressionEnabled = compressionEnabled;
this.endpoint = path != null ? URI.create(path) : URI.create(Builder.DEFAULT_PATH);
}
@Override
public Encoding encoding() {
return encoding;
}
@Override
public int messageMaxBytes() {
return messageMaxBytes;
}
@Override
public int messageSizeInBytes(List encodedSpans) {
return encoding().listSizeInBytes(encodedSpans);
}
@Override
public Call sendSpans(List encodedSpans) {
initHttpClient();
if (httpClient != null && httpClient.isRunning()) {
return new HttpCall(httpClient, endpoint, compressionEnabled, encodedSpans);
} else {
throw new IllegalStateException("HTTP Client Closed");
}
}
@Override
public CheckResult check() {
initHttpClient();
if (httpClient == null) {
return CheckResult.failed(new NoAvailableServiceException(ZipkinServiceInstanceList.SERVICE_ID));
}
try {
HttpResponse
© 2015 - 2025 Weber Informatics LLC | Privacy Policy