
ratpack.zipkin.ServerTracingModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ratpack-zipkin Show documentation
Show all versions of ratpack-zipkin Show documentation
Zipkin support for Ratpack
/*
* Copyright 2016 the original author or 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 ratpack.zipkin;
import brave.Tracing;
import brave.http.HttpClientParser;
import brave.http.HttpSampler;
import brave.http.HttpServerParser;
import brave.http.HttpTracing;
import brave.internal.Platform;
import brave.sampler.Sampler;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;
import ratpack.api.Nullable;
import ratpack.guice.ConfigurableModule;
import ratpack.handling.HandlerDecorator;
import ratpack.http.client.HttpClient;
import ratpack.server.ServerConfig;
import ratpack.zipkin.internal.DefaultServerTracingHandler;
import ratpack.zipkin.internal.RatpackCurrentTraceContext;
import ratpack.zipkin.internal.ZipkinHttpClientImpl;
import zipkin.Endpoint;
import zipkin.Span;
import zipkin.reporter.Reporter;
import java.net.InetAddress;
/**
* Module for Zipkin distributed tracing.
*/
public class ServerTracingModule extends ConfigurableModule {
@Override
protected void configure() {
bind(ServerTracingHandler.class).to(DefaultServerTracingHandler.class);
Provider serverTracingHandlerProvider =
getProvider(ServerTracingHandler.class);
bind(HttpClient.class).annotatedWith(Zipkin.class).to(ZipkinHttpClientImpl.class);
bind(ZipkinHttpClientImpl.class);
Multibinder.newSetBinder(binder(), HandlerDecorator.class).addBinding()
.toProvider(() -> HandlerDecorator.prepend(serverTracingHandlerProvider.get()));
}
@Provides
public HttpTracing getTracing(final Config config, final ServerConfig serverConfig) {
Tracing tracing = Tracing.newBuilder()
.sampler(config.sampler)
.currentTraceContext(new RatpackCurrentTraceContext())
.localEndpoint(buildLocalEndpoint(config.serviceName, serverConfig.getPort(),
serverConfig.getAddress()))
.localServiceName(config.serviceName)
.reporter(config.spanReporter)
.build();
return HttpTracing.newBuilder(tracing)
.clientParser(config.clientParser)
.serverParser(config.serverParser)
.serverSampler(config.serverSampler)
.clientSampler(config.clientSampler)
.build();
}
private static Endpoint buildLocalEndpoint(String serviceName, int port, @Nullable InetAddress configAddress) {
Endpoint.Builder builder = Endpoint.builder();
if (!builder.parseIp(configAddress)) {
builder = Platform.get().localEndpoint().toBuilder();
}
return builder.serviceName(serviceName).port(port).build();
}
/**
* Configuration class for {@link ServerTracingModule}.
*/
public static class Config {
private String serviceName = "unknown";
private Reporter spanReporter = Reporter.NOOP;
private Sampler sampler = Sampler.NEVER_SAMPLE;
private HttpSampler serverSampler = HttpSampler.TRACE_ID;
private HttpSampler clientSampler = HttpSampler.TRACE_ID;
private HttpClientParser clientParser = new HttpClientParser();
private HttpServerParser serverParser = new HttpServerParser();
/**
* Set the service name.
*
* @param serviceName the service name.
* @return the config
*/
public Config serviceName(final String serviceName) {
this.serviceName = serviceName;
return this;
}
/**
* Set the Span reporter.
*
* If not set, defaults to {@link Reporter#NOOP}.
*
* @param reporter the reporter
*
* @return the config
*/
public Config spanReporter(final Reporter reporter) {
this.spanReporter = reporter;
return this;
}
/**
* Set the sampler.
*
* If not set, defaults to {@link Sampler#NEVER_SAMPLE}.
*
* @param sampler the sampler
*
* @return the config
*/
public Config sampler(final Sampler sampler) {
this.sampler = sampler;
return this;
}
/**
* Set the {@link HttpSampler} for client requests.
*
* If not set, defaults to {@link HttpSampler#TRACE_ID}.
*
* @param clientSampler the client sampler
*
* @return the config
*/
public Config clientSampler(final HttpSampler clientSampler) {
this.clientSampler = clientSampler;
return this;
}
/**
* Set the {@link HttpSampler} for server requests.
*
* If not set, defaults to {@link HttpSampler#TRACE_ID}.
*
* @param httpSampler the server sampler
*
* @return the config
*/
public Config serverSampler(final HttpSampler httpSampler) {
this.serverSampler = httpSampler;
return this;
}
/**
* Set the {@link HttpClientParser}.
*
* Defaults to {@link HttpClientParser}, which implements some reasonable
* defaults for client spans.
* Provide a subclass of {@link HttpClientParser} to customize behaviour.
*
* @param clientParser the client parser
*
* @return the config
*/
public Config clientParser(final HttpClientParser clientParser) {
this.clientParser = clientParser;
return this;
}
/**
* Set the {@link HttpServerParser}.
*
* Defaults to {@link HttpServerParser}, which implements some reasonable
* defaults for server spans.
* Provide a subclass of {@link HttpServerParser} to customize behaviour.
*
* @param serverParser the client parser
*
* @return the config
*/
public Config serverParser(final HttpServerParser serverParser) {
this.serverParser = serverParser;
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy