2020package co .elastic .apm .opentracing ;
2121
2222import co .elastic .apm .AbstractInstrumentationTest ;
23+ import co .elastic .apm .impl .transaction .SystemClock ;
2324import co .elastic .apm .impl .transaction .TraceContext ;
2425import co .elastic .apm .impl .transaction .Transaction ;
2526import io .opentracing .Scope ;
2627import io .opentracing .Span ;
2728import io .opentracing .SpanContext ;
29+ import io .opentracing .Tracer ;
2830import io .opentracing .log .Fields ;
2931import io .opentracing .propagation .Format ;
3032import 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