|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package rediscala |
| 7 | + |
| 8 | +import io.opentelemetry.api.trace.SpanKind.CLIENT |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil |
| 10 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension |
| 11 | +import io.opentelemetry.instrumentation.testing.util.ThrowingSupplier |
| 12 | +import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo |
| 13 | +import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} |
| 14 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION |
| 15 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM |
| 16 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemIncubatingValues.REDIS |
| 17 | +import org.assertj.core.api.Assertions.assertThat |
| 18 | +import org.junit.jupiter.api.{AfterAll, BeforeAll, Test, TestInstance} |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 20 | +import org.testcontainers.containers.GenericContainer |
| 21 | +import redis.{RedisClient, RedisDispatcher} |
| 22 | + |
| 23 | +import java.util.function.Consumer |
| 24 | +import scala.concurrent.duration.Duration |
| 25 | +import scala.concurrent.{Await, Future} |
| 26 | + |
| 27 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 28 | +class RediscalaClientTest { |
| 29 | + |
| 30 | + @RegisterExtension val testing = AgentInstrumentationExtension.create |
| 31 | + |
| 32 | + var system: Object = null |
| 33 | + var redisServer: GenericContainer[_] = null |
| 34 | + var redisClient: RedisClient = null |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + def setUp(): Unit = { |
| 38 | + redisServer = |
| 39 | + new GenericContainer("redis:6.2.3-alpine").withExposedPorts(6379) |
| 40 | + redisServer.start() |
| 41 | + |
| 42 | + val host: String = redisServer.getHost |
| 43 | + val port: Integer = redisServer.getMappedPort(6379) |
| 44 | + |
| 45 | + try { |
| 46 | + val clazz = Class.forName("akka.actor.ActorSystem") |
| 47 | + system = clazz.getMethod("create").invoke(null) |
| 48 | + } catch { |
| 49 | + case _: ClassNotFoundException => |
| 50 | + val clazz = Class.forName("org.apache.pekko.actor.ActorSystem") |
| 51 | + system = clazz.getMethod("create").invoke(null) |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + // latest RedisClient constructor takes username as argument |
| 56 | + classOf[RedisClient].getMethod("username") |
| 57 | + redisClient = classOf[RedisClient] |
| 58 | + .getConstructors()(0) |
| 59 | + .newInstance( |
| 60 | + host, |
| 61 | + port, |
| 62 | + Option.apply(null), |
| 63 | + Option.apply(null), |
| 64 | + Option.apply(null), |
| 65 | + "RedisClient", |
| 66 | + Option.apply(null), |
| 67 | + system, |
| 68 | + RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") |
| 69 | + ) |
| 70 | + .asInstanceOf[RedisClient] |
| 71 | + } catch { |
| 72 | + case _: Exception => |
| 73 | + redisClient = classOf[RedisClient] |
| 74 | + .getConstructors()(0) |
| 75 | + .newInstance( |
| 76 | + host, |
| 77 | + port, |
| 78 | + Option.apply(null), |
| 79 | + Option.apply(null), |
| 80 | + "RedisClient", |
| 81 | + Option.apply(null), |
| 82 | + system, |
| 83 | + RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") |
| 84 | + ) |
| 85 | + .asInstanceOf[RedisClient] |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @AfterAll |
| 90 | + def tearDown(): Unit = { |
| 91 | + if (system != null) { |
| 92 | + system.getClass.getMethod("terminate").invoke(system) |
| 93 | + } |
| 94 | + redisServer.stop() |
| 95 | + } |
| 96 | + |
| 97 | + @Test def testSetCommand(): Unit = { |
| 98 | + val value = testing.runWithSpan( |
| 99 | + "parent", |
| 100 | + new ThrowingSupplier[Future[Boolean], Exception] { |
| 101 | + override def get(): Future[Boolean] = { |
| 102 | + redisClient.set("foo", "bar") |
| 103 | + } |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + assertThat(Await.result(value, Duration.apply("3 second"))).isTrue |
| 108 | + testing.waitAndAssertTraces(new Consumer[TraceAssert] { |
| 109 | + override def accept(trace: TraceAssert): Unit = |
| 110 | + trace.hasSpansSatisfyingExactly( |
| 111 | + new Consumer[SpanDataAssert] { |
| 112 | + override def accept(span: SpanDataAssert): Unit = { |
| 113 | + span.hasName("parent").hasNoParent |
| 114 | + } |
| 115 | + }, |
| 116 | + new Consumer[SpanDataAssert] { |
| 117 | + override def accept(span: SpanDataAssert): Unit = { |
| 118 | + span |
| 119 | + .hasName("SET") |
| 120 | + .hasKind(CLIENT) |
| 121 | + .hasParent(trace.getSpan(0)) |
| 122 | + .hasAttributesSatisfyingExactly( |
| 123 | + equalTo(DB_SYSTEM, REDIS), |
| 124 | + equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "SET") |
| 125 | + ) |
| 126 | + } |
| 127 | + } |
| 128 | + ) |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + @Test def testGetCommand(): Unit = { |
| 133 | + val (write, value) = testing.runWithSpan( |
| 134 | + "parent", |
| 135 | + new ThrowingSupplier[ |
| 136 | + (Future[Boolean], Future[Option[String]]), |
| 137 | + Exception |
| 138 | + ] { |
| 139 | + override def get(): (Future[Boolean], Future[Option[String]]) = { |
| 140 | + val write = redisClient.set("bar", "baz") |
| 141 | + val value = redisClient.get[String]("bar") |
| 142 | + (write, value) |
| 143 | + } |
| 144 | + } |
| 145 | + ) |
| 146 | + |
| 147 | + assertThat(Await.result(write, Duration.apply("3 second"))).isTrue |
| 148 | + assertThat( |
| 149 | + Await |
| 150 | + .result(value, Duration.apply("3 second")) |
| 151 | + .get |
| 152 | + ).isEqualTo("baz") |
| 153 | + |
| 154 | + testing.waitAndAssertTraces(new Consumer[TraceAssert] { |
| 155 | + override def accept(trace: TraceAssert): Unit = |
| 156 | + trace.hasSpansSatisfyingExactly( |
| 157 | + new Consumer[SpanDataAssert] { |
| 158 | + override def accept(span: SpanDataAssert): Unit = { |
| 159 | + span.hasName("parent").hasNoParent |
| 160 | + } |
| 161 | + }, |
| 162 | + new Consumer[SpanDataAssert] { |
| 163 | + override def accept(span: SpanDataAssert): Unit = { |
| 164 | + span |
| 165 | + .hasName("SET") |
| 166 | + .hasKind(CLIENT) |
| 167 | + .hasParent(trace.getSpan(0)) |
| 168 | + .hasAttributesSatisfyingExactly( |
| 169 | + equalTo(DB_SYSTEM, REDIS), |
| 170 | + equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "SET") |
| 171 | + ) |
| 172 | + } |
| 173 | + }, |
| 174 | + new Consumer[SpanDataAssert] { |
| 175 | + override def accept(span: SpanDataAssert): Unit = { |
| 176 | + span |
| 177 | + .hasName("GET") |
| 178 | + .hasKind(CLIENT) |
| 179 | + .hasParent(trace.getSpan(0)) |
| 180 | + .hasAttributesSatisfyingExactly( |
| 181 | + equalTo(DB_SYSTEM, REDIS), |
| 182 | + equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "GET") |
| 183 | + ) |
| 184 | + } |
| 185 | + } |
| 186 | + ) |
| 187 | + }) |
| 188 | + } |
| 189 | +} |
0 commit comments