com.github.kristofa.brave.http.HttpClientRequestAdapter Maven / Gradle / Ivy
package com.github.kristofa.brave.http;
import com.github.kristofa.brave.ClientRequestAdapter;
import com.github.kristofa.brave.IdConversion;
import com.github.kristofa.brave.KeyValueAnnotation;
import com.github.kristofa.brave.SpanId;
import com.github.kristofa.brave.internal.Nullable;
import com.twitter.zipkin.gen.Endpoint;
import zipkin.TraceKeys;
import java.util.Collection;
import java.util.Collections;
public class HttpClientRequestAdapter implements ClientRequestAdapter {
private final HttpClientRequest request;
private final SpanNameProvider spanNameProvider;
public HttpClientRequestAdapter(HttpClientRequest request, SpanNameProvider spanNameProvider) {
this.request = request;
this.spanNameProvider = spanNameProvider;
}
@Override
public String getSpanName() {
return spanNameProvider.spanName(request);
}
@Override
public void addSpanIdToRequest(@Nullable SpanId spanId) {
if (spanId == null) {
request.addHeader(BraveHttpHeaders.Sampled.getName(), "0");
} else {
request.addHeader(BraveHttpHeaders.Sampled.getName(), "1");
request.addHeader(BraveHttpHeaders.TraceId.getName(), spanId.traceIdString());
request.addHeader(BraveHttpHeaders.SpanId.getName(), IdConversion.convertToString(spanId.spanId));
if (spanId.nullableParentId() != null) {
request.addHeader(BraveHttpHeaders.ParentSpanId.getName(), IdConversion.convertToString(spanId.parentId));
}
}
}
@Override
public Collection requestAnnotations() {
return Collections.singleton(KeyValueAnnotation.create(
TraceKeys.HTTP_URL, request.getUri().toString()));
}
@Override
public Endpoint serverAddress() {
return null;
}
}