Repository: GoogleCloudPlatform/pgadapter
Pull request #1558 (2024-03-16 17:30:11)

chore: fix uds benchmark for Go

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/1558?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 90.80%. Comparing base [(`7c14284`)](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/commit/7c1428441406a386f6db11a3d0a54260bacae83a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) to head [(`6dc3859`)](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/1558?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform).


<details><summary>Additional details and impacted files</summary>


```diff
@@                  Coverage Diff                  @@
##             postgresql-dialect    #1558   +/-   ##
=====================================================
  Coverage                 90.79%   90.80%           
  Complexity                 2702     2702           
=====================================================
  Files                       144      144           
  Lines                      9249     9250    +1     
  Branches                   1343     1343           
=====================================================
+ Hits                       8398     8399    +1     
  Misses                      573      573           
  Partials                    278      278           
```

| [Flag](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/1558/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) | Coverage Δ | |
|---|---|---|
| [all_tests](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/1558/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) | `90.80% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/1558?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform).


Patch:
diff --git a/benchmarks/latency-comparison/golang/runners/client_library_runner.go b/benchmarks/latency-comparison/golang/runners/client_library_runner.go
index 3acf11eb..ad9c8cfd 100644
--- a/benchmarks/latency-comparison/golang/runners/client_library_runner.go
+++ b/benchmarks/latency-comparison/golang/runners/client_library_runner.go
@@ -142,7 +142,7 @@ func printProgress(finished *atomic.Bool, ops *atomic.Int64, totalOps int) {
 	go func() {
 		for !finished.Load() {
 			time.Sleep(time.Second)
-			fmt.Printf("\r%d/%d", ops.Load(), totalOps)
+			fmt.Printf("\r%d/%d        ", ops.Load(), totalOps)
 		}
 	}()
 }
diff --git a/benchmarks/latency-comparison/golang/runners/pgx_runner.go b/benchmarks/latency-comparison/golang/runners/pgx_runner.go
index 4ed1f003..9319369a 100644
--- a/benchmarks/latency-comparison/golang/runners/pgx_runner.go
+++ b/benchmarks/latency-comparison/golang/runners/pgx_runner.go
@@ -31,7 +31,7 @@ func RunPgx(database, sql string, readWrite bool, numOperations, numClients, wai
 	// Connect to Cloud Spanner through PGAdapter.
 	var connString string
 	if useUnixSocket {
-		connString = fmt.Sprintf("host=%s port=%d database=%s", host, port, url.QueryEscape(database))
+		connString = fmt.Sprintf("host=%s port=%d database=%s", host, port, database)
 	} else {
 		connString = fmt.Sprintf("postgres://uid:pwd@%s:%d/%s?sslmode=disable", host, port, url.QueryEscape(database))
 	}
diff --git a/benchmarks/latency-comparison/golang/runners/pgx_v4_runner.go b/benchmarks/latency-comparison/golang/runners/pgx_v4_runner.go
index be07c942..6fa8d308 100644
--- a/benchmarks/latency-comparison/golang/runners/pgx_v4_runner.go
+++ b/benchmarks/latency-comparison/golang/runners/pgx_v4_runner.go
@@ -31,7 +31,7 @@ func RunPgxV4(database, sql string, readWrite bool, numOperations, numClients, w
 	// Connect to Cloud Spanner through PGAdapter.
 	var connString string
 	if useUnixSocket {
-		connString = fmt.Sprintf("host=%s port=%d database=%s", host, port, url.QueryEscape(database))
+		connString = fmt.Sprintf("host=%s port=%d database=%s", host, port, database)
 	} else {
 		connString = fmt.Sprintf("postgres://uid:pwd@%s:%d/%s?sslmode=disable", host, port, url.QueryEscape(database))
 	}
diff --git a/benchmarks/latency-comparison/java/pom.xml b/benchmarks/latency-comparison/java/pom.xml
index 619de3e0..f78b3f63 100644
--- a/benchmarks/latency-comparison/java/pom.xml
+++ b/benchmarks/latency-comparison/java/pom.xml
@@ -35,7 +35,7 @@
   <parent>
     <groupId>com.google.cloud</groupId>
     <artifactId>google-cloud-shared-config</artifactId>
-    <version>1.7.6</version>
+    <version>1.7.7</version>
   </parent>
 
   <dependencyManagement>
@@ -92,7 +92,7 @@
     <dependency>
       <groupId>commons-cli</groupId>
       <artifactId>commons-cli</artifactId>
-      <version>1.6.0</version>
+      <version>1.7.0</version>
     </dependency>
 
     <dependency>
diff --git a/pom.xml b/pom.xml
index aa963873..600a0a20 100644
--- a/pom.xml
+++ b/pom.xml
@@ -54,7 +54,7 @@
   <parent>
     <groupId>com.google.cloud</groupId>
     <artifactId>google-cloud-shared-config</artifactId>
-    <version>1.7.6</version>
+    <version>1.7.7</version>
   </parent>
   <scm>
     <connection>scm:git:git@github.com:GoogleCloudPlatform/pgadapter.git</connection>
@@ -185,7 +185,7 @@
     <dependency>
       <groupId>commons-cli</groupId>
       <artifactId>commons-cli</artifactId>
-      <version>1.6.0</version>
+      <version>1.7.0</version>
     </dependency>
     <dependency>
       <groupId>org.apache.commons</groupId>
@@ -205,7 +205,7 @@
     <dependency>
       <groupId>org.bouncycastle</groupId>
       <artifactId>bcpkix-jdk18on</artifactId>
-      <version>1.78</version>
+      <version>1.78.1</version>
     </dependency>
 
     <!-- Test dependencies -->
diff --git a/samples/java/hibernate/pom.xml b/samples/java/hibernate/pom.xml
index 6083af67..741e904f 100644
--- a/samples/java/hibernate/pom.xml
+++ b/samples/java/hibernate/pom.xml
@@ -57,7 +57,7 @@
     <dependency>
       <groupId>io.hypersistence</groupId>
       <artifactId>hypersistence-utils-hibernate-63</artifactId>
-      <version>3.7.3</version>
+      <version>3.7.4</version>
     </dependency>
     <!-- Add testcontainers, so we can run a Docker container with PGAdapter+Spanner Emulator -->
     <dependency>
diff --git a/samples/java/liquibase/pom.xml b/samples/java/liquibase/pom.xml
index c0b5a750..129115fc 100644
--- a/samples/java/liquibase/pom.xml
+++ b/samples/java/liquibase/pom.xml
@@ -37,7 +37,7 @@
   <parent>
     <groupId>com.google.cloud</groupId>
     <artifactId>google-cloud-shared-config</artifactId>
-    <version>1.7.6</version>
+    <version>1.7.7</version>
   </parent>
   <scm>
     <connection>scm:git:git@github.com:GoogleCloudPlatform/pgadapter.git</connection>
diff --git a/samples/java/spring-data-jpa/pom.xml b/samples/java/spring-data-jpa/pom.xml
index 5d209d6c..bff17185 100644
--- a/samples/java/spring-data-jpa/pom.xml
+++ b/samples/java/spring-data-jpa/pom.xml
@@ -54,7 +54,7 @@
     <dependency>
       <groupId>io.hypersistence</groupId>
       <artifactId>hypersistence-utils-hibernate-63</artifactId>
-      <version>3.7.3</version>
+      <version>3.7.4</version>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
diff --git a/src/main/java/com/google/cloud/spanner/pgadapter/Server.java b/src/main/java/com/google/cloud/spanner/pgadapter/Server.java
index 56b5d37c..f0617ddb 100644
--- a/src/main/java/com/google/cloud/spanner/pgadapter/Server.java
+++ b/src/main/java/com/google/cloud/spanner/pgadapter/Server.java
@@ -14,6 +14,8 @@
 
 package com.google.cloud.spanner.pgadapter;
 
+import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
+
 import com.google.auth.Credentials;
 import com.google.cloud.opentelemetry.metric.GoogleCloudMetricExporter;
 import com.google.cloud.opentelemetry.metric.MetricConfiguration;
@@ -24,10 +26,12 @@ import com.google.common.collect.ImmutableMap;
 import com.google.devtools.cloudtrace.v2.AttributeValue;
 import com.google.devtools.cloudtrace.v2.TruncatableString;
 import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
 import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
 import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
 import io.opentelemetry.sdk.metrics.export.MetricExporter;
 import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
+import io.opentelemetry.sdk.resources.Resource;
 import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
 import io.opentelemetry.sdk.trace.export.SpanExporter;
 import io.opentelemetry.sdk.trace.samplers.Sampler;
@@ -38,6 +42,7 @@ import java.io.PrintStream;
 import java.time.Duration;
 import java.util.Locale;
 import java.util.Objects;
+import java.util.concurrent.ThreadLocalRandom;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 
@@ -76,8 +81,9 @@ public class Server {
       System.setProperty("otel.logs.exporter", "none");
     }
     if (getOpenTelemetrySetting("otel.service.name") == null) {
-      System.setProperty("otel.service.name", "pgadapter");
+      System.setProperty("otel.service.name", "pgadapter-" + ThreadLocalRandom.current().nextInt());
     }
+    String serviceName = Objects.requireNonNull(getOpenTelemetrySetting("otel.service.name"));
 
     try {
       String projectId = optionsMetadata.getTelemetryProjectId();
@@ -95,14 +101,9 @@ public class Server {
         }
         builder.setFixedAttributes(
             ImmutableMap.of(
-                "service.name",
+                SERVICE_NAME.getKey(),
                 AttributeValue.newBuilder()
-                    .setStringValue(
-                        TruncatableString.newBuilder()
-                            .setValue(
-                                Objects.requireNonNull(
-                                    getOpenTelemetrySetting("otel.service.name")))
-                            .build())
+                    .setStringValue(TruncatableString.newBuilder().setValue(serviceName).build())
                     .build()));
         TraceConfiguration configuration = builder.build();
         SpanExporter traceExporter = TraceExporter.createWithConfiguration(configuration);
@@ -131,8 +132,10 @@ public class Server {
                     .build());
         openTelemetryBuilder.addMeterProviderCustomizer(
             (sdkMeterProviderBuilder, configProperties) ->
-                sdkMeterProviderBuilder.registerMetricReader(
-                    PeriodicMetricReader.builder(cloudMonitoringExporter).build()));
+                sdkMeterProviderBuilder
+                    .addResource(Resource.create(Attributes.of(SERVICE_NAME, serviceName)))
+                    .registerMetricReader(
+                        PeriodicMetricReader.builder(cloudMonitoringExporter).build()));
       }
       return openTelemetryBuilder.build().getOpenTelemetrySdk();
     } catch (IOException exception) {
diff --git a/src/main/java/com/google/cloud/spanner/pgadapter/statements/ExtendedQueryProtocolHandler.java b/src/main/java/com/google/cloud/spanner/pgadapter/statements/ExtendedQueryProtocolHandler.java
index 1cef475e..3c74fce1 100644
--- a/src/main/java/com/google/cloud/spanner/pgadapter/statements/ExtendedQueryProtocolHandler.java
+++ b/src/main/java/com/google/cloud/spanner/pgadapter/statements/ExtendedQueryProtocolHandler.java
@@ -66,7 +66,9 @@ public class ExtendedQueryProtocolHandler {
                 .getServer()
                 .getTracer(ConnectionHandler.class.getName(), getVersion()),
             connectionHandler.getServer().getMetrics(),
-            createMetricAttributes(connectionHandler.getDatabaseId()),
+            createMetricAttributes(
+                connectionHandler.getDatabaseId(),
+                connectionHandler.getTraceConnectionId().toString()),
             connectionHandler.getTraceConnectionId().toString(),
             connectionHandler::closeAllPortals,
             connectionHandler.getDatabaseId(),
@@ -95,8 +97,9 @@ public class ExtendedQueryProtocolHandler {
   }
 
   @VisibleForTesting
-  static Attributes createMetricAttributes(DatabaseId databaseId) {
+  static Attributes createMetricAttributes(DatabaseId databaseId, String connectionId) {
     AttributesBuilder attributesBuilder = Attributes.builder();
+    attributesBuilder.put("pgadapter.connection_id", connectionId);
     attributesBuilder.put("database", databaseId.getDatabase());
     attributesBuilder.put("instance_id", databaseId.getInstanceId().getInstance());
     attributesBuilder.put("project_id", databaseId.getInstanceId().getProject());


Test patch:
diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/statements/BackendConnectionTest.java b/src/test/java/com/google/cloud/spanner/pgadapter/statements/BackendConnectionTest.java
index d1809527..f56f53e2 100644
--- a/src/test/java/com/google/cloud/spanner/pgadapter/statements/BackendConnectionTest.java
+++ b/src/test/java/com/google/cloud/spanner/pgadapter/statements/BackendConnectionTest.java
@@ -85,7 +85,7 @@ public class BackendConnectionTest {
   private static final Runnable DO_NOTHING = () -> {};
   private static final DatabaseId DATABASE_ID = DatabaseId.of("p", "i", "d");
   private static final Attributes METRIC_ATTRIBUTES =
-      ExtendedQueryProtocolHandler.createMetricAttributes(DATABASE_ID);
+      ExtendedQueryProtocolHandler.createMetricAttributes(DATABASE_ID, "test-id");
 
   @Test
   public void testExtractDdlUpdateCounts() {
diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/statements/StatementTest.java b/src/test/java/com/google/cloud/spanner/pgadapter/statements/StatementTest.java
index a5513ef5..9cac0270 100644
--- a/src/test/java/com/google/cloud/spanner/pgadapter/statements/StatementTest.java
+++ b/src/test/java/com/google/cloud/spanner/pgadapter/statements/StatementTest.java
@@ -97,7 +97,7 @@ public class StatementTest {
   private static final Runnable DO_NOTHING = () -> {};
   private static final DatabaseId DATABASE_ID = DatabaseId.of("p", "i", "d");
   private static final Attributes METRIC_ATTRIBUTES =
-      ExtendedQueryProtocolHandler.createMetricAttributes(DATABASE_ID);
+      ExtendedQueryProtocolHandler.createMetricAttributes(DATABASE_ID, "test-connection");
 
   private static ParsedStatement parse(String sql) {
     return PARSER.parse(Statement.of(sql));
@@ -521,7 +521,7 @@ public class StatementTest {
         new BackendConnection(
             NOOP_OTEL,
             NOOP_OTEL_METER,
-            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId),
+            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId, "test-connection"),
             UUID.randomUUID().toString(),
             DO_NOTHING,
             databaseId,
@@ -629,7 +629,7 @@ public class StatementTest {
         new BackendConnection(
             NOOP_OTEL,
             NOOP_OTEL_METER,
-            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId),
+            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId, "test-connection"),
             UUID.randomUUID().toString(),
             DO_NOTHING,
             databaseId,
@@ -688,7 +688,7 @@ public class StatementTest {
         new BackendConnection(
             NOOP_OTEL,
             NOOP_OTEL_METER,
-            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId),
+            ExtendedQueryProtocolHandler.createMetricAttributes(databaseId, "test-connection"),
             UUID.randomUUID().toString(),
             DO_NOTHING,
             databaseId,