|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.jedis.v1_4; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import io.opentelemetry.api.trace.SpanKind; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 14 | +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; |
| 15 | +import org.junit.jupiter.api.AfterAll; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 20 | +import org.testcontainers.containers.GenericContainer; |
| 21 | +import redis.clients.jedis.Jedis; |
| 22 | + |
| 23 | +class JedisClientTest { |
| 24 | + @RegisterExtension |
| 25 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 26 | + |
| 27 | + static GenericContainer<?> redisServer = |
| 28 | + new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379); |
| 29 | + |
| 30 | + static int port; |
| 31 | + |
| 32 | + static Jedis jedis; |
| 33 | + |
| 34 | + @BeforeAll |
| 35 | + static void setupSpec() { |
| 36 | + redisServer.start(); |
| 37 | + port = redisServer.getMappedPort(6379); |
| 38 | + jedis = new Jedis("localhost", port); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterAll |
| 42 | + static void cleanupSpec() { |
| 43 | + redisServer.stop(); |
| 44 | + } |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setup() { |
| 48 | + jedis.flushAll(); |
| 49 | + testing.clearData(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void setCommand() { |
| 54 | + jedis.set("foo", "bar"); |
| 55 | + |
| 56 | + testing.waitAndAssertTraces( |
| 57 | + trace -> |
| 58 | + trace.hasSpansSatisfyingExactly( |
| 59 | + span -> |
| 60 | + span.hasName("SET") |
| 61 | + .hasKind(SpanKind.CLIENT) |
| 62 | + .hasAttributesSatisfyingExactly( |
| 63 | + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), |
| 64 | + equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"), |
| 65 | + equalTo(SemanticAttributes.DB_OPERATION, "SET"), |
| 66 | + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), |
| 67 | + equalTo(SemanticAttributes.NET_PEER_PORT, port)))); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void getCommand() { |
| 72 | + jedis.set("foo", "bar"); |
| 73 | + String value = jedis.get("foo"); |
| 74 | + |
| 75 | + assertThat(value).isEqualTo("bar"); |
| 76 | + |
| 77 | + testing.waitAndAssertTraces( |
| 78 | + trace -> |
| 79 | + trace.hasSpansSatisfyingExactly( |
| 80 | + span -> |
| 81 | + span.hasName("SET") |
| 82 | + .hasKind(SpanKind.CLIENT) |
| 83 | + .hasAttributesSatisfyingExactly( |
| 84 | + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), |
| 85 | + equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"), |
| 86 | + equalTo(SemanticAttributes.DB_OPERATION, "SET"), |
| 87 | + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), |
| 88 | + equalTo(SemanticAttributes.NET_PEER_PORT, port))), |
| 89 | + trace -> |
| 90 | + trace.hasSpansSatisfyingExactly( |
| 91 | + span -> |
| 92 | + span.hasName("GET") |
| 93 | + .hasKind(SpanKind.CLIENT) |
| 94 | + .hasAttributesSatisfyingExactly( |
| 95 | + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), |
| 96 | + equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"), |
| 97 | + equalTo(SemanticAttributes.DB_OPERATION, "GET"), |
| 98 | + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), |
| 99 | + equalTo(SemanticAttributes.NET_PEER_PORT, port)))); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void commandWithNoArguments() { |
| 104 | + jedis.set("foo", "bar"); |
| 105 | + String value = jedis.randomKey(); |
| 106 | + |
| 107 | + assertThat(value).isEqualTo("foo"); |
| 108 | + |
| 109 | + testing.waitAndAssertTraces( |
| 110 | + trace -> |
| 111 | + trace.hasSpansSatisfyingExactly( |
| 112 | + span -> |
| 113 | + span.hasName("SET") |
| 114 | + .hasKind(SpanKind.CLIENT) |
| 115 | + .hasAttributesSatisfyingExactly( |
| 116 | + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), |
| 117 | + equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"), |
| 118 | + equalTo(SemanticAttributes.DB_OPERATION, "SET"), |
| 119 | + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), |
| 120 | + equalTo(SemanticAttributes.NET_PEER_PORT, port))), |
| 121 | + trace -> |
| 122 | + trace.hasSpansSatisfyingExactly( |
| 123 | + span -> |
| 124 | + span.hasName("RANDOMKEY") |
| 125 | + .hasKind(SpanKind.CLIENT) |
| 126 | + .hasAttributesSatisfyingExactly( |
| 127 | + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), |
| 128 | + equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"), |
| 129 | + equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"), |
| 130 | + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), |
| 131 | + equalTo(SemanticAttributes.NET_PEER_PORT, port)))); |
| 132 | + } |
| 133 | +} |
0 commit comments