|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.influxdb.v2_4; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; |
| 11 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; |
| 12 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME; |
| 13 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION; |
| 14 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT; |
| 15 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
| 16 | +import static java.util.Arrays.asList; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import io.opentelemetry.api.trace.SpanKind; |
| 20 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 21 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 22 | +import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import org.influxdb.InfluxDB; |
| 27 | +import org.influxdb.InfluxDBFactory; |
| 28 | +import org.influxdb.dto.BatchPoints; |
| 29 | +import org.influxdb.dto.Point; |
| 30 | +import org.influxdb.dto.Query; |
| 31 | +import org.influxdb.dto.QueryResult; |
| 32 | +import org.junit.jupiter.api.AfterAll; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.TestInstance; |
| 36 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 37 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 38 | +import org.testcontainers.containers.GenericContainer; |
| 39 | + |
| 40 | +@TestInstance(Lifecycle.PER_CLASS) |
| 41 | +class InfluxDbClient24Test { |
| 42 | + |
| 43 | + @RegisterExtension |
| 44 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 45 | + |
| 46 | + private static final GenericContainer<?> influxDbServer = |
| 47 | + new GenericContainer<>("influxdb:1.8.10-alpine").withExposedPorts(8086); |
| 48 | + |
| 49 | + private static InfluxDB influxDb; |
| 50 | + |
| 51 | + private static final String databaseName = "mydb"; |
| 52 | + |
| 53 | + private static String host; |
| 54 | + |
| 55 | + private static int port; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + void setup() { |
| 59 | + influxDbServer.start(); |
| 60 | + port = influxDbServer.getMappedPort(8086); |
| 61 | + host = influxDbServer.getHost(); |
| 62 | + String serverUrl = "http://" + host + ":" + port + "/"; |
| 63 | + String username = "root"; |
| 64 | + String password = "root"; |
| 65 | + influxDb = InfluxDBFactory.connect(serverUrl, username, password); |
| 66 | + influxDb.createDatabase(databaseName); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterAll |
| 70 | + void cleanup() { |
| 71 | + influxDb.deleteDatabase(databaseName); |
| 72 | + influxDbServer.stop(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testQueryAndModifyWithOneArgument() { |
| 77 | + String dbName = databaseName + "2"; |
| 78 | + influxDb.createDatabase(dbName); |
| 79 | + BatchPoints batchPoints = |
| 80 | + BatchPoints.database(dbName).tag("async", "true").retentionPolicy("autogen").build(); |
| 81 | + Point point1 = |
| 82 | + Point.measurement("cpu") |
| 83 | + .tag("atag", "test") |
| 84 | + .addField("idle", 90L) |
| 85 | + .addField("usertime", 9L) |
| 86 | + .addField("system", 1L) |
| 87 | + .build(); |
| 88 | + Point point2 = |
| 89 | + Point.measurement("disk") |
| 90 | + .tag("atag", "test") |
| 91 | + .addField("used", 80L) |
| 92 | + .addField("free", 1L) |
| 93 | + .build(); |
| 94 | + batchPoints.point(point1); |
| 95 | + batchPoints.point(point2); |
| 96 | + influxDb.write(batchPoints); |
| 97 | + Query query = new Query("SELECT * FROM cpu GROUP BY *", dbName); |
| 98 | + QueryResult result = influxDb.query(query); |
| 99 | + assertThat(result.getResults().get(0).getSeries().get(0).getTags()).isNotEmpty(); |
| 100 | + influxDb.deleteDatabase(dbName); |
| 101 | + testing.waitAndAssertTraces( |
| 102 | + trace -> |
| 103 | + trace.hasSpansSatisfyingExactly( |
| 104 | + span -> |
| 105 | + span.hasName("CREATE DATABASE " + dbName) |
| 106 | + .hasKind(SpanKind.CLIENT) |
| 107 | + .hasAttributesSatisfying( |
| 108 | + attributeAssertions(null, "CREATE DATABASE", dbName))), |
| 109 | + trace -> |
| 110 | + trace.hasSpansSatisfyingExactly( |
| 111 | + span -> |
| 112 | + span.hasName("WRITE " + dbName) |
| 113 | + .hasKind(SpanKind.CLIENT) |
| 114 | + .hasAttributesSatisfying(attributeAssertions(null, "WRITE", dbName))), |
| 115 | + trace -> |
| 116 | + trace.hasSpansSatisfyingExactly( |
| 117 | + span -> |
| 118 | + span.hasName("SELECT " + dbName) |
| 119 | + .hasKind(SpanKind.CLIENT) |
| 120 | + .hasAttributesSatisfying( |
| 121 | + attributeAssertions("SELECT * FROM cpu GROUP BY *", "SELECT", dbName))), |
| 122 | + trace -> |
| 123 | + trace.hasSpansSatisfyingExactly( |
| 124 | + span -> |
| 125 | + span.hasName("DROP DATABASE " + dbName) |
| 126 | + .hasKind(SpanKind.CLIENT) |
| 127 | + .hasAttributesSatisfying( |
| 128 | + attributeAssertions(null, "DROP DATABASE", dbName)))); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testQueryWithTwoArguments() { |
| 133 | + Query query = new Query("SELECT * FROM cpu_load where test1 = 'influxDb'", databaseName); |
| 134 | + influxDb.query(query, TimeUnit.MILLISECONDS); |
| 135 | + |
| 136 | + testing.waitAndAssertTraces( |
| 137 | + trace -> |
| 138 | + trace.hasSpansSatisfyingExactly( |
| 139 | + span -> |
| 140 | + span.hasName("SELECT " + databaseName) |
| 141 | + .hasKind(SpanKind.CLIENT) |
| 142 | + .hasAttributesSatisfying( |
| 143 | + attributeAssertions( |
| 144 | + "SELECT * FROM cpu_load where test1 = ?", |
| 145 | + "SELECT", |
| 146 | + databaseName)))); |
| 147 | + } |
| 148 | + |
| 149 | + @SuppressWarnings("deprecation") // using deprecated semconv |
| 150 | + private static List<AttributeAssertion> attributeAssertions( |
| 151 | + String statement, String operation, String databaseName) { |
| 152 | + List<AttributeAssertion> result = new ArrayList<>(); |
| 153 | + result.addAll( |
| 154 | + asList( |
| 155 | + equalTo(DB_SYSTEM, "influxdb"), |
| 156 | + equalTo(maybeStable(DB_NAME), databaseName), |
| 157 | + equalTo(SERVER_ADDRESS, host), |
| 158 | + equalTo(SERVER_PORT, port), |
| 159 | + equalTo(maybeStable(DB_OPERATION), operation))); |
| 160 | + if (statement != null) { |
| 161 | + result.add(equalTo(maybeStable(DB_STATEMENT), statement)); |
| 162 | + } |
| 163 | + return result; |
| 164 | + } |
| 165 | +} |
0 commit comments