|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pekkoactor.v1_0 |
| 7 | + |
| 8 | +import io.opentelemetry.api.GlobalOpenTelemetry |
| 9 | +import io.opentelemetry.api.trace.Span |
| 10 | +import org.apache.pekko.actor.ActorSystem |
| 11 | +import org.apache.pekko.pattern.after |
| 12 | +import org.assertj.core.api.Assertions.assertThat |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | +import scala.concurrent.{ Await, Future } |
| 15 | +import scala.concurrent.duration.DurationInt |
| 16 | + |
| 17 | +class PekkoSchedulerTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + def checkThatSpanWorksWithPekkoScheduledEvents(): Unit = { |
| 21 | + val system = ActorSystem("my-system") |
| 22 | + implicit val executionContext = system.dispatcher |
| 23 | + val tracer = GlobalOpenTelemetry.get.getTracer("test-tracer") |
| 24 | + val initialSpan = tracer.spanBuilder("test").startSpan |
| 25 | + try { |
| 26 | + val scope = initialSpan.makeCurrent() |
| 27 | + val futureResult = for { |
| 28 | + result1 <- Future { |
| 29 | + assertThat(Span.current().getSpanContext().getTraceId()).isEqualTo(initialSpan.getSpanContext().getTraceId()) |
| 30 | + assertThat(Span.current().getSpanContext().getSpanId()).isEqualTo(initialSpan.getSpanContext().getSpanId()) |
| 31 | + 1 |
| 32 | + } |
| 33 | + _ = assertThat(Span.current().getSpanContext().getTraceId()).isEqualTo(initialSpan.getSpanContext().getTraceId()) |
| 34 | + _ = assertThat(Span.current().getSpanContext().getSpanId()).isEqualTo(initialSpan.getSpanContext().getSpanId()) |
| 35 | + result2 <- after(200.millis, system.scheduler)(Future.successful(2)) |
| 36 | + _ = assertThat(Span.current().getSpanContext().getTraceId()).isEqualTo(initialSpan.getSpanContext().getTraceId()) |
| 37 | + _ = assertThat(Span.current().getSpanContext().getSpanId()).isEqualTo(initialSpan.getSpanContext().getSpanId()) |
| 38 | + } yield result1 + result2 |
| 39 | + assertThat(Await.result(futureResult, 5.seconds)).isEqualTo(3) |
| 40 | + } finally { |
| 41 | + system.terminate() |
| 42 | + initialSpan.end() |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments