Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Support span log fields in zipkin sender (#226)
Browse files Browse the repository at this point in the history
* Use JSON encoding for structured data logs in zipkin
  • Loading branch information
pvlugter authored and vprithvi committed Aug 2, 2017
1 parent e2c6d1a commit ec6ac78
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.uber.jaeger.senders.zipkin;

import com.google.gson.Gson;
import com.twitter.zipkin.thriftjava.Annotation;
import com.twitter.zipkin.thriftjava.AnnotationType;
import com.twitter.zipkin.thriftjava.BinaryAnnotation;
Expand All @@ -42,6 +43,7 @@
public class ThriftSpanConverter {

private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Gson gson = new Gson();

public static com.twitter.zipkin.thriftjava.Span convertSpan(Span span) {
Tracer tracer = span.getTracer();
Expand Down Expand Up @@ -78,7 +80,13 @@ private static List<Annotation> buildAnnotations(Span span, Endpoint host) {
List<LogData> logs = span.getLogs();
if (logs != null) {
for (LogData logData : logs) {
annotations.add(new Annotation(logData.getTime(), logData.getMessage()));
String logMessage = logData.getMessage();
Map<String, ?> logFields = logData.getFields();
if (logMessage != null) {
annotations.add(new Annotation(logData.getTime(), logMessage));
} else if (logFields != null) {
annotations.add(new Annotation(logData.getTime(), gson.toJson(logFields)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
import io.opentracing.propagation.TextMapInjectAdapter;
import io.opentracing.tag.Tags;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -277,4 +279,32 @@ public void testRpcChildSpanHasTheSameId() {
assertEquals("client and server must have the same span ID",
client.context().getSpanId(), server.context().getSpanId());
}

@Test
public void testSpanLogsCreateAnnotations() {
Span span = (com.uber.jaeger.Span) tracer.buildSpan("span-with-logs").startManual();

span.log("event");

// use sorted map for consistent ordering in test
Map<String, Object> fields = new TreeMap<String, Object>();
fields.put("event", "structured data");
fields.put("string", "something");
fields.put("number", 42);
fields.put("boolean", true);
span.log(fields);

com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span);

List<String> annotationValues = new ArrayList<String>();
for (Annotation annotation : zipkinSpan.getAnnotations()) {
annotationValues.add(annotation.getValue());
}

List<String> expectedValues = new ArrayList<String>();
expectedValues.add("event");
expectedValues.add("{\"boolean\":true,\"event\":\"structured data\",\"number\":42,\"string\":\"something\"}");

assertEquals("zipkin span should contain matching annotations for span logs", expectedValues, annotationValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
import com.uber.jaeger.reporters.Reporter;
import com.uber.jaeger.samplers.ConstSampler;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.thrift.transport.AutoExpandingBufferWriteTransport;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -143,6 +147,43 @@ public void testFlushSendsSpan() throws Exception {
}
}

@Test
public void testAppendSpanWithLogs() throws Exception {
Span span = (Span) tracer.buildSpan("span-with-logs").startManual();

span.log("event");

// use sorted map for consistent ordering in test
Map<String, Object> fields = new TreeMap<String, Object>();
fields.put("event", "structured data");
fields.put("string", "something");
fields.put("number", 42);
fields.put("boolean", true);
span.log(fields);

sender.append(span);
sender.flush();

List<List<zipkin.Span>> traces = zipkinRule.getTraces();
assertEquals(1, traces.size());
assertEquals(1, traces.get(0).size());

zipkin.Span zipkinSpan = traces.get(0).get(0);
assertEquals(2, zipkinSpan.annotations.size());

// ignore order by using set
Set<String> annotationValues = new HashSet<String>();
for (Annotation annotation : zipkinSpan.annotations) {
annotationValues.add(annotation.value);
}

Set<String> expectedValues = new HashSet<String>();
expectedValues.add("event");
expectedValues.add("{\"boolean\":true,\"event\":\"structured data\",\"number\":42,\"string\":\"something\"}");

assertEquals("zipkin span should contain matching annotations for span logs", expectedValues, annotationValues);
}

private ZipkinSender newSender(int messageMaxBytes) {
return ZipkinSender.create(
URLConnectionSender.builder()
Expand Down

0 comments on commit ec6ac78

Please sign in to comment.