Skip to content

Commit 313b6e3

Browse files
committed
Fix clock problem with OT API
1 parent 82efe3f commit 313b6e3

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

apm-agent-core/src/main/java/co/elastic/apm/impl/transaction/Span.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public Span(ElasticApmTracer tracer) {
4343
}
4444

4545
public <T> Span start(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext) {
46-
return start(childContextCreator, parentContext, getTraceContext().getClock().getEpochMicros());
46+
// we can't get the timestamp here as the clock has not yet been initialized
47+
return start(childContextCreator, parentContext, -1);
4748
}
4849

4950
public <T> Span start(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext, long epochMicros) {
@@ -56,7 +57,11 @@ public <T> Span start(TraceContext.ChildContextCreator<T> childContextCreator, T
5657
if (dropped) {
5758
traceContext.setRecorded(false);
5859
}
59-
timestamp = epochMicros;
60+
if (epochMicros >= 0) {
61+
timestamp = epochMicros;
62+
} else {
63+
timestamp = getTraceContext().getClock().getEpochMicros();
64+
}
6065
if (logger.isDebugEnabled()) {
6166
logger.debug("startSpan {} {", this);
6267
if (logger.isTraceEnabled()) {

apm-opentracing/src/test/java/co/elastic/apm/opentracing/OpenTracingBridgeTest.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
package co.elastic.apm.opentracing;
2121

2222
import co.elastic.apm.AbstractInstrumentationTest;
23+
import co.elastic.apm.impl.transaction.SystemClock;
2324
import co.elastic.apm.impl.transaction.TraceContext;
2425
import co.elastic.apm.impl.transaction.Transaction;
2526
import io.opentracing.Scope;
2627
import io.opentracing.Span;
2728
import io.opentracing.SpanContext;
29+
import io.opentracing.Tracer;
2830
import io.opentracing.log.Fields;
2931
import io.opentracing.propagation.Format;
3032
import io.opentracing.propagation.TextMapExtractAdapter;
@@ -64,6 +66,22 @@ void testCreateNonActiveTransaction() {
6466
assertThat(reporter.getFirstTransaction().getName().toString()).isEqualTo("test");
6567
}
6668

69+
@Test
70+
void sanityCheckRealTimestamps() {
71+
final Span transactionSpan = apmTracer.buildSpan("transactionSpan").start();
72+
apmTracer.buildSpan("nestedSpan").asChildOf(transactionSpan).start().finish();
73+
transactionSpan.finish();
74+
final long epochMicros = SystemClock.ForJava8CapableVM.INSTANCE.getEpochMicros();
75+
76+
assertThat(reporter.getTransactions()).hasSize(1);
77+
assertThat(reporter.getFirstTransaction().getDuration()).isLessThan(TimeUnit.MINUTES.toMillis(1));
78+
assertThat(reporter.getFirstTransaction().getTimestamp()).isBetween(epochMicros - TimeUnit.MINUTES.toMicros(1), epochMicros);
79+
80+
assertThat(reporter.getSpans()).hasSize(1);
81+
assertThat(reporter.getFirstSpan().getDuration()).isLessThan(TimeUnit.MINUTES.toMillis(1));
82+
assertThat(reporter.getFirstSpan().getTimestamp()).isBetween(epochMicros - TimeUnit.MINUTES.toMicros(1), epochMicros);
83+
}
84+
6785
@Test
6886
void testFinishTwice() {
6987
final Span span = apmTracer.buildSpan("test").withStartTimestamp(0).start();
@@ -153,7 +171,7 @@ void testCreateNonActiveTransactionAsChildOf() {
153171

154172
@Test
155173
void testCreateActiveTransaction() {
156-
final ApmScope scope = apmTracer.buildSpan("test").withStartTimestamp(0).startActive(false);
174+
final ApmScope scope = (ApmScope) apmTracer.buildSpan("test").withStartTimestamp(0).startActive(false);
157175

158176
assertThat(apmTracer.activeSpan()).isNotNull();
159177
assertThat(apmTracer.activeSpan().getSpan()).isSameAs(scope.span().getSpan());
@@ -173,9 +191,9 @@ void testCreateActiveTransaction() {
173191

174192
@Test
175193
void testCreateActiveTransactionAndSpans() {
176-
try (ApmScope transaction = apmTracer.buildSpan("transaction").startActive(true)) {
177-
try (ApmScope span = apmTracer.buildSpan("span").startActive(true)) {
178-
try (ApmScope nestedSpan = apmTracer.buildSpan("nestedSpan").startActive(true)) {
194+
try (Scope transaction = apmTracer.buildSpan("transaction").startActive(true)) {
195+
try (Scope span = apmTracer.buildSpan("span").startActive(true)) {
196+
try (Scope nestedSpan = apmTracer.buildSpan("nestedSpan").startActive(true)) {
179197
}
180198
}
181199
}
@@ -217,10 +235,10 @@ void testResolveServerType() {
217235
@Test
218236
void testCreatingClientTransactionCreatesNoopSpan() {
219237
assertThat(apmTracer.scopeManager().active()).isNull();
220-
try (ApmScope transactionScope = apmTracer.buildSpan("transaction").withTag("span.kind", "client").startActive(true)) {
221-
try (ApmScope spanScope = apmTracer.buildSpan("span").startActive(true)) {
238+
try (ApmScope transactionScope = (ApmScope) apmTracer.buildSpan("transaction").withTag("span.kind", "client").startActive(true)) {
239+
try (ApmScope spanScope = (ApmScope) apmTracer.buildSpan("span").startActive(true)) {
222240
assertThat(apmTracer.scopeManager().active().span().getSpan()).isEqualTo(spanScope.span().getSpan());
223-
try (ApmScope nestedSpanScope = apmTracer.buildSpan("nestedSpan").startActive(true)) {
241+
try (ApmScope nestedSpanScope = (ApmScope) apmTracer.buildSpan("nestedSpan").startActive(true)) {
224242
assertThat(apmTracer.scopeManager().active().span().getSpan()).isEqualTo(nestedSpanScope.span().getSpan());
225243
}
226244
assertThat(apmTracer.scopeManager().active().span().getSpan()).isEqualTo(spanScope.span().getSpan());
@@ -344,7 +362,7 @@ void testAsChildOfSpanNull() {
344362

345363
@Test
346364
void testGetBaggageItem() {
347-
final ApmSpan span = apmTracer.buildSpan("span")
365+
final Span span = apmTracer.buildSpan("span")
348366
.start();
349367

350368
// baggage is not supported yet
@@ -355,8 +373,8 @@ void testGetBaggageItem() {
355373

356374
@Test
357375
void testSpanTags() {
358-
try (ApmScope transaction = apmTracer.buildSpan("transaction").startActive(true)) {
359-
try (ApmScope span = apmTracer.buildSpan("span").startActive(true)) {
376+
try (Scope transaction = apmTracer.buildSpan("transaction").startActive(true)) {
377+
try (Scope span = apmTracer.buildSpan("span").startActive(true)) {
360378
transaction.span().setTag("foo", "bar");
361379
span.span().setTag("bar", "baz");
362380
}
@@ -368,7 +386,7 @@ void testSpanTags() {
368386
}
369387

370388
private Transaction createTransactionFromOtTags(Map<String, String> tags) {
371-
final ApmSpanBuilder spanBuilder = apmTracer.buildSpan("transaction");
389+
final Tracer.SpanBuilder spanBuilder = apmTracer.buildSpan("transaction");
372390
tags.forEach(spanBuilder::withTag);
373391
spanBuilder.start().finish();
374392
assertThat(reporter.getTransactions()).hasSize(1);
@@ -378,9 +396,9 @@ private Transaction createTransactionFromOtTags(Map<String, String> tags) {
378396
}
379397

380398
private co.elastic.apm.impl.transaction.Span createSpanFromOtTags(Map<String, String> tags) {
381-
final ApmSpanBuilder transactionSpanBuilder = apmTracer.buildSpan("transaction");
399+
final Tracer.SpanBuilder transactionSpanBuilder = apmTracer.buildSpan("transaction");
382400
try (Scope transaction = transactionSpanBuilder.startActive(true)) {
383-
final ApmSpanBuilder spanBuilder = apmTracer.buildSpan("transaction");
401+
final Tracer.SpanBuilder spanBuilder = apmTracer.buildSpan("transaction");
384402
tags.forEach(spanBuilder::withTag);
385403
spanBuilder.start().finish();
386404
}

0 commit comments

Comments
 (0)