Skip to content

Commit

Permalink
Add a configurable id formatter (#15)
Browse files Browse the repository at this point in the history
* adding string and uuid based id extractors

* making string id formatter as default and bug fix
  • Loading branch information
doctorXWrites authored and ashishagg committed Jul 18, 2019
1 parent 01b65bf commit cfc977e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.expedia.www</groupId>
<artifactId>spring-cloud-sleuth-haystack-reporter</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>

<packaging>jar</packaging>
<name>Spring Cloud Sleuth Haystack Reporter</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.expedia.www.haystack.remote.clients.Client;
import com.expedia.www.haystack.remote.clients.GRPCAgentProtoClient;
import com.expedia.www.haystack.remote.clients.HttpCollectorProtoClient;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors.IdExtractor;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors.StringIdExtractor;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors.UUIDIdExtractor;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.reporters.HaystackReporter;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.HashMap;
Expand Down Expand Up @@ -58,8 +61,8 @@ public String serviceName() {
}

@Bean
public HaystackReporter spanReporter(String serviceName, List<Client> clients) {
return new HaystackReporter(serviceName, clients);
public HaystackReporter spanReporter(String serviceName, List<Client> clients, IdExtractor idExtractor) {
return new HaystackReporter(serviceName, clients, idExtractor);
}

@Bean
Expand Down Expand Up @@ -87,6 +90,16 @@ public List<Client> clients(MetricsRegistry metricsRegistry,
return clients;
}

@Bean
@ConditionalOnMissingBean
public IdExtractor idExtractor(HaystackSettings settings) {
if (settings.getIdFormat().equalsIgnoreCase("UUID")) {
return new UUIDIdExtractor();
} else {
return new StringIdExtractor();
}
}

@Bean
@ConditionalOnMissingBean
public MetricsRegistry metricsRegistry(final ObjectProvider<MeterRegistry> meterRegistryObjectProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@ConfigurationProperties("spring.sleuth.haystack")
public class HaystackSettings {
private boolean enabled = true;
private String idFormat = "string";

private ClientConfiguration client = new ClientConfiguration();

public ClientConfiguration getClient() {
Expand All @@ -43,6 +45,14 @@ public boolean isEnabled() {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getIdFormat() {
return idFormat;
}

public void setIdFormat(String idFormat) {
this.idFormat = idFormat;
}

public static class ClientConfiguration {
private GrpcConfiguration grpc;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors;

import zipkin2.Span;

public interface IdExtractor {
Object getTraceId(Span span);

Object getSpanId(Span span);

Object getParentSpanId(Span span);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors;

import zipkin2.Span;

public class StringIdExtractor implements IdExtractor {

@Override
public String getTraceId(Span span) {
return span.traceId();
}

@Override
public String getSpanId(Span span) {
return span.id();
}

@Override
public String getParentSpanId(Span span) {
if (span.parentId() == null) {
return "";
}
return span.parentId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors;

import brave.internal.HexCodec;
import zipkin2.Span;

import java.util.UUID;

public class UUIDIdExtractor implements IdExtractor {

@Override
public String getTraceId(Span span) {
final Long lowTraceId = HexCodec.lowerHexToUnsignedLong(span.traceId());
final Long highTraceId = span.traceId().length() == 32 ? HexCodec.lowerHexToUnsignedLong(span.traceId(), 0) : 0;

final UUID traceId = new UUID(highTraceId, lowTraceId);
return traceId.toString();
}

@Override
public String getSpanId(Span span) {
return new UUID(0, HexCodec.lowerHexToUnsignedLong(span.id())).toString();
}

@Override
public String getParentSpanId(Span span) {
if (span.parentId() == null) {
return "";
}
return new UUID(0, HexCodec.lowerHexToUnsignedLong(span.parentId())).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/
package com.expedia.www.spring.cloud.sleuth.haystack.reporter.reporters;

import brave.internal.HexCodec;
import com.expedia.open.tracing.Log;
import com.expedia.open.tracing.Tag;
import com.expedia.www.haystack.remote.clients.Client;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors.IdExtractor;
import com.expedia.www.spring.cloud.sleuth.haystack.reporter.idextractors.UUIDIdExtractor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import zipkin2.Span;
import zipkin2.reporter.Reporter;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static zipkin2.Span.Kind.*;
Expand All @@ -41,9 +41,16 @@ public class HaystackReporter implements Reporter<Span> {

private final String serviceName;

private final IdExtractor idExtractor;

public HaystackReporter(String serviceName, List<Client> clients) {
this(serviceName, clients, new UUIDIdExtractor());
}

public HaystackReporter(String serviceName, List<Client> clients, IdExtractor idExtractor) {
this.clients = clients;
this.serviceName = serviceName;
this.idExtractor = idExtractor;
}

@Override
Expand All @@ -56,9 +63,9 @@ private com.expedia.open.tracing.Span convertZipKinSpanToHaystackSpan(Span span)
final com.expedia.open.tracing.Span.Builder builder = com.expedia.open.tracing.Span.newBuilder()
.setServiceName(serviceName)
.setOperationName(getSpanName(span))
.setTraceId(getTraceId(span))
.setSpanId(getSpanId(span))
.setParentSpanId(getParentSpanId(span))
.setTraceId(idExtractor.getTraceId(span).toString())
.setSpanId(idExtractor.getSpanId(span).toString())
.setParentSpanId(idExtractor.getParentSpanId(span).toString())
.setStartTime(span.timestampAsLong())
.setDuration(span.durationAsLong())
.addAllTags(getTags(span))
Expand Down Expand Up @@ -136,25 +143,6 @@ private boolean filterTags(String key) {
return !("REQUEST".equals(key) || "RESPONSE".equals(key));
}

private String getTraceId(Span span) {
final Long lowTraceId = HexCodec.lowerHexToUnsignedLong(span.traceId());
final Long highTraceId = span.traceId().length() == 32 ? HexCodec.lowerHexToUnsignedLong(span.traceId(), 0) : 0;

final UUID traceId = new UUID(highTraceId, lowTraceId);
return traceId.toString();
}

private String getSpanId(Span span) {
return new UUID(0, HexCodec.lowerHexToUnsignedLong(span.id())).toString();
}

private String getParentSpanId(Span span) {
if (span.parentId() == null) {
return "";
}
return new UUID(0, HexCodec.lowerHexToUnsignedLong(span.parentId())).toString();
}

private String getSpanName(Span span) {
if (span.name() != null) {
return span.name();
Expand Down

0 comments on commit cfc977e

Please sign in to comment.