public class ZipkinSpanSenderDefaultHttpImpl extends java.lang.Object implements ZipkinSpanSender
ZipkinSpanSender that collects spans into batches and sends them to the Zipkin server
at a regular intervals over HTTP.| Modifier and Type | Class and Description |
|---|---|
protected static class |
ZipkinSpanSenderDefaultHttpImpl.ZipkinSpanSenderJob |
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
compressZipkinSpanPayload |
protected int |
connectTimeoutMillis |
static int |
DEFAULT_CONNECT_TIMEOUT_MILLIS |
static int |
DEFAULT_READ_TIMEOUT_MILLIS |
static int |
DEFAULT_SPAN_BATCH_SENDING_PERIOD_MILLIS |
protected java.net.URL |
postZipkinSpansUrl |
protected int |
readTimeoutMillis |
protected ZipkinSpanSenderDefaultHttpImpl.ZipkinSpanSenderJob |
senderJob |
protected java.util.concurrent.BlockingQueue<zipkin.Span> |
zipkinSpanSendingQueue |
protected java.util.concurrent.ScheduledExecutorService |
zipkinSpanSendingScheduler |
| Constructor and Description |
|---|
ZipkinSpanSenderDefaultHttpImpl(java.lang.String postZipkinSpansBaseUrl,
boolean compressZipkinSpanPayload)
Convenience constructor that calls the kitchen-sink constructor passing in
DEFAULT_CONNECT_TIMEOUT_MILLIS,
DEFAULT_READ_TIMEOUT_MILLIS, and DEFAULT_SPAN_BATCH_SENDING_PERIOD_MILLIS for the default connect timeout,
read timeout, and span batching period respectively. |
ZipkinSpanSenderDefaultHttpImpl(java.lang.String postZipkinSpansBaseUrl,
boolean compressZipkinSpanPayload,
int connectTimeoutMillis,
int readTimeoutMillis,
int batchSendingPeriodMillis)
Kitchen-sink constructor that creates a new instance allowing you to specify all the given configuration options.
|
| Modifier and Type | Method and Description |
|---|---|
protected java.util.concurrent.ScheduledExecutorService |
configureScheduledExecutorServiceForBatching() |
void |
flush()
Forces any queued/batched spans to be sent to the Zipkin server as quickly as possible.
|
void |
handleSpan(zipkin.Span span)
"Handles" the given Zipkin span.
|
protected void |
sendSpans(byte[] spanListJsonPayloadBytes)
This method uses basic JDK classes to POST the given payload bytes (representing a list of Zipkin Spans that have been serialized to JSON)
to the Zipkin server endpoint at
postZipkinSpansUrl. |
protected void |
sendSpans(java.util.List<zipkin.Span> spanList) |
public static final int DEFAULT_SPAN_BATCH_SENDING_PERIOD_MILLIS
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final int DEFAULT_READ_TIMEOUT_MILLIS
protected final java.net.URL postZipkinSpansUrl
protected final boolean compressZipkinSpanPayload
protected final int connectTimeoutMillis
protected final int readTimeoutMillis
protected final ZipkinSpanSenderDefaultHttpImpl.ZipkinSpanSenderJob senderJob
protected final java.util.concurrent.BlockingQueue<zipkin.Span> zipkinSpanSendingQueue
protected final java.util.concurrent.ScheduledExecutorService zipkinSpanSendingScheduler
public ZipkinSpanSenderDefaultHttpImpl(java.lang.String postZipkinSpansBaseUrl,
boolean compressZipkinSpanPayload,
int connectTimeoutMillis,
int readTimeoutMillis,
int batchSendingPeriodMillis)
postZipkinSpansBaseUrl - The base URL of the Zipkin server. This should include the scheme, host, and port (if non-standard for the scheme).
e.g. http://localhost:9411, or https://zipkinserver.doesnotexist.com/compressZipkinSpanPayload - Pass in true if the payload sent to the Zipkin server should be gzipped, false to pass the payload uncompressed.connectTimeoutMillis - The timeout in milliseconds that should be used when attempting to connect to the Zipkin server.readTimeoutMillis - The read timeout in milliseconds that should be used when waiting for a response from the Zipkin server.batchSendingPeriodMillis - The period in milliseconds that should be used between sending span batches to the Zipkin server. If you pass in
0 it will disable automatic batch sending, at which point flush() is the only
way to send spans. IMPORTANT NOTE: The queue that stores spans is unbounded, so make sure you
select a period that is short enough to keep the queue from getting too big and taking up too much memory on
your server.public ZipkinSpanSenderDefaultHttpImpl(java.lang.String postZipkinSpansBaseUrl,
boolean compressZipkinSpanPayload)
DEFAULT_CONNECT_TIMEOUT_MILLIS,
DEFAULT_READ_TIMEOUT_MILLIS, and DEFAULT_SPAN_BATCH_SENDING_PERIOD_MILLIS for the default connect timeout,
read timeout, and span batching period respectively.postZipkinSpansBaseUrl - The base URL of the Zipkin server. This should include the scheme, host, and port (if non-standard for the scheme).
e.g. http://localhost:9411, or https://zipkinserver.doesnotexist.com/compressZipkinSpanPayload - Pass in true if the payload sent to the Zipkin server should be gzipped, false to pass the payload uncompressed.public void handleSpan(zipkin.Span span)
ZipkinSpanSender"Handles" the given Zipkin span. In a typical implementation spans are stored in a threadsafe collection for batching until some trigger is hit that causes the span batch to be sent to the Zipkin server (e.g. scheduled job that sends batches every x period of time, or after a batch size threshold is hit, or both).
IMPORTANT NOTE: DO NOT BLOCK IN THIS METHOD'S IMPLEMENTATION. If you send data to the Zipkin server directly based on this method call then it should be split out into a separate thread to do the work. The only thing that should happen on the calling thread is to put the span into a queue for later processing, or spinning off a job on a separate thread.
handleSpan in interface ZipkinSpanSenderspan - The Zipkin span to handle.public void flush()
ZipkinSpanSenderForces any queued/batched spans to be sent to the Zipkin server as quickly as possible.
IMPORTANT NOTE: DO NOT BLOCK IN THIS METHOD'S IMPLEMENTATION. This method should spin off a task on a separate thread for executing the push-to-Zipkin-server logic.
flush in interface ZipkinSpanSenderflush in interface java.io.Flushableprotected java.util.concurrent.ScheduledExecutorService configureScheduledExecutorServiceForBatching()
protected void sendSpans(java.util.List<zipkin.Span> spanList)
protected void sendSpans(byte[] spanListJsonPayloadBytes)
throws java.io.IOException
This method uses basic JDK classes to POST the given payload bytes (representing a list of Zipkin Spans that have been serialized to JSON)
to the Zipkin server endpoint at postZipkinSpansUrl. This gives a simple workable implementation that requires no outside dependencies,
however it may lack some of the flexibility your project requires (e.g. around connection pooling). You can extend this class and override
this method to use a different HTTP client for sending spans to Zipkin if desired.
This code was derived from the Zipkin/Brave repository's brave-spancollector-http module v3.9.1 (https://github.com/openzipkin/brave/blob/master/brave-spancollector-http/src/main/java/com/github/kristofa/brave/http/HttpSpanCollector.java) and licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0).
java.io.IOException