diff --git a/src/main/java/com/google/cloud/spanner/pgadapter/ProxyServer.java b/src/main/java/com/google/cloud/spanner/pgadapter/ProxyServer.java index 0f310ddb6..587995383 100644 --- a/src/main/java/com/google/cloud/spanner/pgadapter/ProxyServer.java +++ b/src/main/java/com/google/cloud/spanner/pgadapter/ProxyServer.java @@ -80,8 +80,13 @@ public class ProxyServer extends AbstractApiService { private final ConcurrentLinkedQueue debugMessages = new ConcurrentLinkedQueue<>(); private final AtomicInteger debugMessageCount = new AtomicInteger(); - ProxyServer(OptionsMetadata optionsMetadata) { - this(optionsMetadata, OpenTelemetry.noop()); + /** + * Instantiates the ProxyServer from CLI-gathered metadata. + * + * @param optionsMetadata Resulting metadata from CLI. + */ + public ProxyServer(OptionsMetadata optionsMetadata) { + this(optionsMetadata, Server.setupOpenTelemetry(optionsMetadata)); } /** 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 c967638fe..9054e4829 100644 --- a/src/main/java/com/google/cloud/spanner/pgadapter/Server.java +++ b/src/main/java/com/google/cloud/spanner/pgadapter/Server.java @@ -53,7 +53,8 @@ public static void main(String[] args) { } } - static OpenTelemetry setupOpenTelemetry(OptionsMetadata optionsMetadata) throws IOException { + /** Creates an {@link OpenTelemetry} object from the given options. */ + static OpenTelemetry setupOpenTelemetry(OptionsMetadata optionsMetadata) { if (!optionsMetadata.isEnableOpenTelemetry()) { return OpenTelemetry.noop(); } @@ -77,38 +78,42 @@ static OpenTelemetry setupOpenTelemetry(OptionsMetadata optionsMetadata) throws if (projectId != null) { builder.setProjectId(projectId); } - Credentials credentials = optionsMetadata.getTelemetryCredentials(); - if (credentials != null) { - builder.setCredentials(credentials); - } - builder.setFixedAttributes( - ImmutableMap.of( - "service.name", - AttributeValue.newBuilder() - .setStringValue( - TruncatableString.newBuilder() - .setValue( - Objects.requireNonNull(getOpenTelemetrySetting("otel.service.name"))) - .build()) - .build())); - TraceConfiguration configuration = builder.build(); - TraceExporter traceExporter = TraceExporter.createWithConfiguration(configuration); - Sampler sampler; - if (optionsMetadata.getOpenTelemetryTraceRatio() == null) { - sampler = Sampler.parentBased(Sampler.traceIdRatioBased(0.05d)); - } else { - sampler = - Sampler.parentBased( - Sampler.traceIdRatioBased(optionsMetadata.getOpenTelemetryTraceRatio())); + try { + Credentials credentials = optionsMetadata.getTelemetryCredentials(); + if (credentials != null) { + builder.setCredentials(credentials); + } + builder.setFixedAttributes( + ImmutableMap.of( + "service.name", + AttributeValue.newBuilder() + .setStringValue( + TruncatableString.newBuilder() + .setValue( + Objects.requireNonNull(getOpenTelemetrySetting("otel.service.name"))) + .build()) + .build())); + TraceConfiguration configuration = builder.build(); + TraceExporter traceExporter = TraceExporter.createWithConfiguration(configuration); + Sampler sampler; + if (optionsMetadata.getOpenTelemetryTraceRatio() == null) { + sampler = Sampler.parentBased(Sampler.traceIdRatioBased(0.05d)); + } else { + sampler = + Sampler.parentBased( + Sampler.traceIdRatioBased(optionsMetadata.getOpenTelemetryTraceRatio())); + } + return AutoConfiguredOpenTelemetrySdk.builder() + .addTracerProviderCustomizer( + (sdkTracerProviderBuilder, configProperties) -> + sdkTracerProviderBuilder + .setSampler(sampler) + .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())) + .build() + .getOpenTelemetrySdk(); + } catch (IOException exception) { + throw new RuntimeException(exception); } - return AutoConfiguredOpenTelemetrySdk.builder() - .addTracerProviderCustomizer( - (sdkTracerProviderBuilder, configProperties) -> - sdkTracerProviderBuilder - .setSampler(sampler) - .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())) - .build() - .getOpenTelemetrySdk(); } static String getOpenTelemetrySetting(String systemProperty) { diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/AbstractMockServerTest.java b/src/test/java/com/google/cloud/spanner/pgadapter/AbstractMockServerTest.java index b14f201e1..abc0eae3b 100644 --- a/src/test/java/com/google/cloud/spanner/pgadapter/AbstractMockServerTest.java +++ b/src/test/java/com/google/cloud/spanner/pgadapter/AbstractMockServerTest.java @@ -68,6 +68,7 @@ import io.grpc.StatusRuntimeException; import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; import io.grpc.stub.StreamObserver; +import io.opentelemetry.api.OpenTelemetry; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.sql.SQLException; @@ -1036,7 +1037,7 @@ public Listener interceptCall( .setEndpoint(String.format("localhost:%d", spannerServer.getPort())) .setCredentials(NoCredentials.getInstance()); optionsConfigurator.accept(builder); - pgServer = new ProxyServer(builder.build()); + pgServer = new ProxyServer(builder.build(), OpenTelemetry.noop()); pgServer.startServer(); } diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/PgAdapterTestEnv.java b/src/test/java/com/google/cloud/spanner/pgadapter/PgAdapterTestEnv.java index 966c13b68..48a261259 100644 --- a/src/test/java/com/google/cloud/spanner/pgadapter/PgAdapterTestEnv.java +++ b/src/test/java/com/google/cloud/spanner/pgadapter/PgAdapterTestEnv.java @@ -43,6 +43,7 @@ import com.google.common.primitives.Bytes; import com.google.spanner.admin.database.v1.CreateDatabaseMetadata; import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; +import io.opentelemetry.api.OpenTelemetry; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; @@ -209,7 +210,7 @@ private void startPGAdapterServer( } argsListBuilder.addAll(additionalPGAdapterOptions); String[] args = argsListBuilder.build().toArray(new String[0]); - server = new ProxyServer(new OptionsMetadata(args)); + server = new ProxyServer(new OptionsMetadata(args), OpenTelemetry.noop()); server.startServer(); } }