Skip to content

Commit

Permalink
Ability to provide a custom service name for spans #865
Browse files Browse the repository at this point in the history
Original pull request: #866
  • Loading branch information
worldtiki authored and mp911de committed Oct 1, 2018
1 parent af1dcad commit 27a7a3a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Usage example:
```java
Tracing tracing = …;

ClientResources clientResources = ClientResources.builder().tracing(BraveTracing.create(tracing)).build();
ClientResources clientResources = ClientResources.builder().tracing(BraveTracing.builder(tracing).build()).build();

RedisClient client = RedisClient.create(clientResources, redisUri);

Expand Down
55 changes: 48 additions & 7 deletions src/main/java/io/lettuce/core/tracing/BraveTracing.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@
public class BraveTracing implements Tracing {

private final BraveTracer tracer;
private final String serviceName;

/**
* Create a new {@link BraveTracing} instance.
*
* @param tracer
* @param builder the {@link BraveTracing.Builder}.
*/
private BraveTracing(BraveTracer tracer) {
public BraveTracing(Builder builder) {

LettuceAssert.notNull(tracer, "Tracer must not be null");
LettuceAssert.notNull(builder.tracing, "Tracing must not be null");
LettuceAssert.notNull(builder.serviceName, "Service name must not be null");

this.tracer = tracer;
this.serviceName = builder.serviceName;
this.tracer = new BraveTracer(builder.tracing);
}

/**
Expand All @@ -72,7 +75,45 @@ private BraveTracing(BraveTracer tracer) {
* @return the {@link BraveTracing}.
*/
public static BraveTracing create(brave.Tracing tracing) {
return new BraveTracing(new BraveTracer(tracing));
return (BraveTracing) builder(tracing).build();
}

public static BraveTracing.Builder builder(brave.Tracing tracing) {
return new BraveTracing.Builder(tracing);
}

public static class Builder implements Tracing.Builder {

private String serviceName = "redis";
private brave.Tracing tracing;

private Builder() {
}

private Builder(brave.Tracing tracing) {
this.tracing = tracing;
}

/**
* Sets the name used in the {@link zipkin2.Endpoint}.
*
* @param serviceName the name for the {@link zipkin2.Endpoint}, must not be {@literal null}.
* @return this
* @since 5.2
*/
@Override
public Tracing.Builder serviceName(String serviceName) {
this.serviceName = serviceName;
return this;
}

/**
* @return a new instance of {@link BraveTracing}
*/
@Override
public Tracing build() {
return new BraveTracing(this);
}
}

@Override
Expand All @@ -98,11 +139,11 @@ public Endpoint createEndpoint(SocketAddress socketAddress) {
if (socketAddress instanceof InetSocketAddress) {

InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
return new BraveEndpoint(builder.serviceName("redis").ip(inetSocketAddress.getAddress())
return new BraveEndpoint(builder.serviceName(serviceName).ip(inetSocketAddress.getAddress())
.port(inetSocketAddress.getPort()).build());
}

return new BraveEndpoint(builder.serviceName("redis").build());
return new BraveEndpoint(builder.serviceName(serviceName).build());
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/lettuce/core/tracing/Tracing.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,46 @@ static Context withTraceContextProvider(TraceContextProvider supplier) {
*/
interface Endpoint {
}

/**
* Create a new {@link Tracing} using default settings.
*
* @return a new instance of a tracing.
* @since 5.2
*/
static Tracing create(brave.Tracing tracing) {
return BraveTracing.builder(tracing).build();
}

/**
* Create a new {@link BraveTracing.Builder} using default settings.
*
* @return a new instance of a tracing builder.
* @since 5.2
*/
static BraveTracing.Builder builder(brave.Tracing tracing) {
return BraveTracing.builder(tracing);
}

/**
* Builder for {@link Tracing}.
*
* @since 5.2
*/
interface Builder {

/**
* Sets the name used in the {@link zipkin2.Endpoint}.
*
* @param serviceName the name for the {@link zipkin2.Endpoint}, must not be {@literal null}.
* @return this
* @since 5.2
*/
Builder serviceName(String serviceName);

/**
* @return a new instance of {@link BraveTracing}
*/
Tracing build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,21 @@ void shouldReportSimpleServiceName() {
assertThat(endpoint.endpoint.ipv4()).isNull();
assertThat(endpoint.endpoint.ipv6()).isNull();
}

@Test
public void shouldReportCustomServiceName() {

DefaultClientResources clientResourcesWithOverridenServiceName = DefaultClientResources.builder()
.tracing(BraveTracing.builder(clientTracing)
.serviceName("custom-name-goes-here")
.build())
.build();
BraveTracing.BraveEndpoint endpoint = (BraveTracing.BraveEndpoint) clientResourcesWithOverridenServiceName.tracing().createEndpoint(
new DomainSocketAddress("foo"));

assertThat(endpoint.endpoint.serviceName()).isEqualTo("custom-name-goes-here");
assertThat(endpoint.endpoint.port()).isNull();
assertThat(endpoint.endpoint.ipv4()).isNull();
assertThat(endpoint.endpoint.ipv6()).isNull();
}
}

0 comments on commit 27a7a3a

Please sign in to comment.