Added docs on how to provide custom serviceName

without this entry in the docs people got confused on how to do this.

fixes #346
This commit is contained in:
Marcin Grzejszczak
2016-08-19 13:25:12 +02:00
parent 08e8e87ba2
commit 1b348abbde
4 changed files with 57 additions and 3 deletions

View File

@@ -103,7 +103,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.1.0</version>
<version>3.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@@ -272,6 +272,19 @@ And you could register them like this:
include::../../../..//spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterCustomExtractorTests.java[tags=configuration,indent=0]
----
=== Custom SA tag in Zipkin
Sometimes you want to create a manual Span that will wrap a call to an external service which is not instrumented.
What you can do is to create a span with the `peer.service` tag that will contain a value of the service that you want to call.
Below you can see an example of a call to Redis that is wrapped in such a span.
[source,java]
----
include::../../../..//spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java[tags=service_name,indent=0]
----
IMPORTANT: Remember not to add both `peer.service` tag and the `SA` tag! You have to add only `peer.service`.
== Span Data as Messages
You can accumulate and send span data over

View File

@@ -195,7 +195,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -1,16 +1,27 @@
package org.springframework.cloud.sleuth.zipkin;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.sleuth.DefaultSpanNamer;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.log.NoOpSpanLogger;
import org.springframework.cloud.sleuth.metric.CounterServiceBasedSpanMetricReporter;
import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
import zipkin.Span;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTracer;
import org.springframework.cloud.sleuth.util.ExceptionUtils;
import zipkin.Span;
import zipkin.junit.HttpFailure;
import zipkin.junit.ZipkinRule;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
public class HttpZipkinSpanReporterTest {
@@ -113,6 +124,36 @@ public class HttpZipkinSpanReporterTest {
assertThat(this.inMemorySpanCounter.getDroppedSpans()).isEqualTo(2);
}
@Test
public void should_change_the_service_name_in_zipkin_to_the_manually_provided_one() {
AtomicReference<Span> receivedSpan = new AtomicReference<>();
Tracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(), new DefaultSpanNamer(),
new NoOpSpanLogger(), new ZipkinSpanListener(receivedSpan::set,
new ServerPropertiesEndpointLocator(new ServerProperties(), "foo")));
// tag::service_name[]
org.springframework.cloud.sleuth.Span newSpan = tracer.createSpan("redis");
try {
newSpan.tag("redis.op", "get");
newSpan.tag("lc", "redis");
newSpan.logEvent(org.springframework.cloud.sleuth.Span.CLIENT_SEND);
// call redis service e.g
// return (SomeObj) redisTemplate.opsForHash().get("MYHASH", someObjKey);
} finally {
newSpan.tag("peer.service", "redisService");
newSpan.tag("peer.ipv4", "1.2.3.4");
newSpan.tag("peer.port", "1234");
newSpan.logEvent(org.springframework.cloud.sleuth.Span.CLIENT_RECV);
tracer.close(newSpan);
}
// end::service_name[]
then(tracer.getCurrentSpan()).isNull();
then(ExceptionUtils.getLastException()).isNull();
then(receivedSpan.get().binaryAnnotations)
.flatExtracting(input -> input.key, input -> new String(input.value))
.contains("peer.service", "redisService");
}
static Span span(long traceId, String spanName) {
return Span.builder().traceId(traceId).id(traceId).name(spanName).build();
}