Преглед изворни кода

Convert couchbase client 3.1 from groovy to java (#10880)

Co-authored-by: Lauri Tulmin <tulmin@gmail.com>
Jay DeLuca пре 11 месеци
родитељ
комит
e3f90d235f

+ 2 - 1
instrumentation/couchbase/couchbase-3.1/javaagent/build.gradle.kts

@@ -31,7 +31,8 @@ dependencies {
     ),
   )
 
-  library("com.couchbase.client:java-client:3.1.0")
+  // 3.1.4 (instead of 3.1.0) needed for test stability and for compatibility with server versions that run on M1 processors
+  library("com.couchbase.client:java-client:3.1.4")
 
   testImplementation("org.testcontainers:couchbase")
 

+ 0 - 83
instrumentation/couchbase/couchbase-3.1/javaagent/src/test/groovy/CouchbaseClient31Test.groovy

@@ -1,83 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import com.couchbase.client.core.env.TimeoutConfig
-import com.couchbase.client.core.error.DocumentNotFoundException
-import com.couchbase.client.java.Cluster
-import com.couchbase.client.java.ClusterOptions
-import com.couchbase.client.java.Collection
-import com.couchbase.client.java.env.ClusterEnvironment
-import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
-import org.testcontainers.containers.output.Slf4jLogConsumer
-import org.testcontainers.couchbase.BucketDefinition
-import org.testcontainers.couchbase.CouchbaseContainer
-import org.testcontainers.couchbase.CouchbaseService
-import spock.lang.Shared
-
-import java.time.Duration
-
-// Couchbase instrumentation is owned upstream so we don't assert on the contents of the spans, only
-// that the instrumentation is properly registered by the agent, meaning some spans were generated.
-class CouchbaseClient31Test extends AgentInstrumentationSpecification {
-  private static final Logger logger = LoggerFactory.getLogger("couchbase-container")
-
-  @Shared
-  CouchbaseContainer couchbase
-  @Shared
-  Cluster cluster
-  @Shared
-  Collection collection
-
-  def setupSpec() {
-    couchbase = new CouchbaseContainer()
-      .withExposedPorts(8091)
-      .withEnabledServices(CouchbaseService.KV)
-      .withBucket(new BucketDefinition("test"))
-      .withLogConsumer(new Slf4jLogConsumer(logger))
-      .withStartupTimeout(Duration.ofSeconds(120))
-    couchbase.start()
-
-    ClusterEnvironment environment = ClusterEnvironment.builder()
-      .timeoutConfig(TimeoutConfig.kvTimeout(Duration.ofSeconds(10)))
-      .build()
-
-    cluster = Cluster.connect(couchbase.connectionString, ClusterOptions
-      .clusterOptions(couchbase.username, couchbase.password)
-      .environment(environment))
-    def bucket = cluster.bucket("test")
-    collection = bucket.defaultCollection()
-    bucket.waitUntilReady(Duration.ofSeconds(30))
-  }
-
-  def cleanupSpec() {
-    couchbase.stop()
-  }
-
-  def "emits spans"() {
-    when:
-    try {
-      collection.get("id")
-    } catch (DocumentNotFoundException e) {
-      // Expected
-    }
-
-    then:
-    assertTracesWithoutScopeVersionVerification(1) {
-      trace(0, 2) {
-        span(0) {
-          name(~/.*get/)
-        }
-        span(1) {
-          name(~/.*dispatch_to_server/)
-        }
-      }
-    }
-
-    cleanup:
-    cluster.disconnect()
-  }
-}

+ 90 - 0
instrumentation/couchbase/couchbase-3.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/couchbase/v3_1/CouchbaseClient31Test.java

@@ -0,0 +1,90 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.couchbase.v3_1;
+
+import com.couchbase.client.core.env.TimeoutConfig;
+import com.couchbase.client.core.error.DocumentNotFoundException;
+import com.couchbase.client.java.Bucket;
+import com.couchbase.client.java.Cluster;
+import com.couchbase.client.java.ClusterOptions;
+import com.couchbase.client.java.Collection;
+import com.couchbase.client.java.env.ClusterEnvironment;
+import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
+import java.time.Duration;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.couchbase.BucketDefinition;
+import org.testcontainers.couchbase.CouchbaseContainer;
+import org.testcontainers.couchbase.CouchbaseService;
+
+// Couchbase instrumentation is owned upstream, so we don't assert on the contents of the spans,
+// only that the instrumentation is properly registered by the agent, meaning some spans were
+// generated.
+class CouchbaseClient31Test {
+  @RegisterExtension
+  private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
+
+  private static final Logger logger = LoggerFactory.getLogger("couchbase-container");
+
+  private static CouchbaseContainer couchbase;
+  private static Cluster cluster;
+  private static Collection collection;
+
+  @BeforeAll
+  static void setup() {
+    couchbase =
+        new CouchbaseContainer("couchbase/server:7.6.0")
+            .withExposedPorts(8091)
+            .withEnabledServices(CouchbaseService.KV)
+            .withBucket(new BucketDefinition("test"))
+            .withLogConsumer(new Slf4jLogConsumer(logger))
+            .withStartupTimeout(Duration.ofMinutes(2));
+    couchbase.start();
+
+    ClusterEnvironment environment =
+        ClusterEnvironment.builder()
+            .timeoutConfig(TimeoutConfig.kvTimeout(Duration.ofSeconds(30)))
+            .build();
+
+    cluster =
+        Cluster.connect(
+            couchbase.getConnectionString(),
+            ClusterOptions.clusterOptions(couchbase.getUsername(), couchbase.getPassword())
+                .environment(environment));
+
+    Bucket bucket = cluster.bucket("test");
+    collection = bucket.defaultCollection();
+
+    // Wait 1 minute due to slow startup contributing to flakiness
+    bucket.waitUntilReady(Duration.ofMinutes(1));
+  }
+
+  @AfterAll
+  static void cleanup() {
+    cluster.disconnect();
+    couchbase.stop();
+  }
+
+  @Test
+  void testEmitsSpans() {
+    try {
+      collection.get("id");
+    } catch (DocumentNotFoundException e) {
+      // Expected
+    }
+
+    testing.waitAndAssertTracesWithoutScopeVersionVerification(
+        trace ->
+            trace.hasSpansSatisfyingExactly(
+                span -> span.hasName("get"), span -> span.hasName("dispatch_to_server")));
+  }
+}