Refactoring protected fields to private ones
MODIFY
src/main/java/com/datadoghq/trace/impl/DDSpan.java
src/main/java/com/datadoghq/trace/impl/DDSpan.java
@@ -24,27 +24,27 @@ public class DDSpan implements io.opentracing.Span {
/**
* Each span have an operation name describing the current span
*/
- protected String operationName;
+ private String operationName;
/**
* Tags are associated to the current span, they will not propagate to the children span
*/
- protected Map<String, Object> tags;
+ private Map<String, Object> tags;
/**
* StartTime stores the creation time of the span in milliseconds
*/
- protected long startTimeMicro;
+ private long startTimeMicro;
/**
* StartTimeNano stores the only the nanoseconds for more accuracy
*/
- protected long startTimeNano;
+ private long startTimeNano;
/**
* The duration in nanoseconds computed using the startTimeMicro and startTimeNano
*/
- protected long durationNano;
+ private long durationNano;
/**
* The context attached to the span
*/
- protected final DDSpanContext context;
+ private final DDSpanContext context;
private final static Logger logger = LoggerFactory.getLogger(DDSpan.class);
@@ -240,7 +240,7 @@ public class DDSpan implements io.opentracing.Span {
//Getters and JSON serialisation instructions
- @JsonGetter(value = "name")
+ @JsonGetter("name")
public String getOperationName() {
return operationName;
}
@@ -317,4 +317,28 @@ public class DDSpan implements io.opentracing.Span {
return context.toString();
}
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((context == null) ? 0 : context.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ DDSpan other = (DDSpan) obj;
+ if (context == null) {
+ if (other.context != null)
+ return false;
+ } else if (!context.equals(other.context))
+ return false;
+ return true;
+ }
}

MODIFY
src/test/java/com/datadoghq/trace/impl/DDSpanSerializerTest.java
src/test/java/com/datadoghq/trace/impl/DDSpanSerializerTest.java
@@ -3,50 +3,57 @@ package com.datadoghq.trace.impl;
import org.junit.Before;
import org.junit.Test;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
public class DDSpanSerializerTest {
- DDSpan parent;
- DDSpan child;
+
DDSpanSerializer serializer;
+ DDSpan span;
@Before
public void setUp() throws Exception {
- //Setup
- DDTracer tracer = new DDTracer();
-
- parent = tracer.buildSpan("op1").withServiceName("service-name").withSpanType("web").start();
- parent.setBaggageItem("a-baggage", "value");
- parent.setTag("k1", "v1");
-
-
- child = tracer.buildSpan("op2").asChildOf(parent).withResourceName("res2").start();
- child.setTag("k2", "v2");
-
- child.finish();
- parent.finish();
-
- //Forces variable values at a fixed value.
- parent.startTimeMicro = 0L;
- parent.durationNano = 0L;
- parent.context.traceId = 1L;
- parent.context.spanId = 1L;
- child.startTimeMicro = 0L;
- child.durationNano = 0L;
- child.context.traceId = 1L;
- child.context.parentId = 1L;
- child.context.spanId = 2L;
-
+ Map<String, String> baggage = new HashMap<String, String>();
+ baggage.put("a-baggage", "value");
+ Map<String, Object> tags = new HashMap<String, Object>();
+ baggage.put("k1", "v1");
+
+
+ DDSpanContext context = new DDSpanContext(
+ 1L,
+ 2L,
+ 0L,
+ "service",
+ "resource",
+ baggage,
+ false,
+ "type",
+ null,
+ null);
+
+ span = new DDSpan(
+ "operation",
+ tags,
+ 100L,
+ context);
+
+ span.finish(133L);
serializer = new DDSpanSerializer();
}
@Test
public void test() throws Exception {
+
+ String expected = "{\"meta\":{\"a-baggage\":\"value\",\"k1\":\"v1\"},\"service\":\"service\",\"error\":0,\"type\":\"type\",\"name\":\"operation\",\"duration\":33000,\"resource\":\"resource\",\"start\":100000,\"span_id\":2,\"parent_id\":0,\"trace_id\":1}";
//FIXME attributes order is not maintained I disabled the test for now
-// assertEquals("{\"type\":\"web\",\"meta\":{\"a-baggage\":\"value\",\"k1\":\"v1\"},\"service\":\"service-name\",\"error\":0,\"name\":\"op1\",\"start\":0,\"duration\":0,\"resource\":\"op1\",\"span_id\":1,\"parent_id\":0,\"trace_id\":1}"
-// , serializer.serialize(parent));
-// assertEquals("{\"type\":\"web\",\"meta\":{\"a-baggage\":\"value\",\"k2\":\"v2\"},\"service\":\"service-name\",\"error\":0,\"name\":\"op2\",\"start\":0,\"duration\":0,\"trace_id\":1,\"span_id\":2,\"parent_id\":1,\"resource\":\"res2\"}",
-// serializer.serialize(child));
+ //assertEquals("
+ // , serializer.serialize(span));
+ // FIXME At the moment, just compare the string sizes
+ assertThat(serializer.serialize(span).length()).isEqualTo(expected.length());
}
}

Refactoring protected fields to private ones