From c36e98cfa78158b9ee3830585b0d2c99fcb9fd3c Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Wed, 13 Mar 2024 19:25:13 +0000 Subject: [PATCH] Remove usgae of deprecated service stub API --- .../trace/InternalTraceExporter.java | 63 ++++++++----------- .../trace/TraceConfiguration.java | 20 ------ .../trace/TraceConfigurationTest.java | 6 +- .../trace/TraceExporterTest.java | 29 +++++++-- 4 files changed, 55 insertions(+), 63 deletions(-) diff --git a/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/InternalTraceExporter.java b/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/InternalTraceExporter.java index 0cc13dba..d14143ae 100644 --- a/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/InternalTraceExporter.java +++ b/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/InternalTraceExporter.java @@ -26,7 +26,6 @@ import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.trace.v2.TraceServiceClient; import com.google.cloud.trace.v2.TraceServiceSettings; -import com.google.cloud.trace.v2.stub.TraceServiceStub; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMap; import com.google.devtools.cloudtrace.v2.AttributeValue; @@ -69,46 +68,36 @@ static InternalTraceExporter createWithClient( static SpanExporter createWithConfiguration(TraceConfiguration configuration) throws IOException { String projectId = configuration.getProjectId(); - TraceServiceStub stub = configuration.getTraceServiceStub(); + TraceServiceSettings.Builder builder = TraceServiceSettings.newBuilder(); - // TODO: Remove stub - tracked in issue #198 - if (stub == null) { - TraceServiceSettings.Builder builder = TraceServiceSettings.newBuilder(); - - // We only use the batchWriteSpans API in this exporter. - builder - .batchWriteSpansSettings() - .setSimpleTimeoutNoRetries( - org.threeten.bp.Duration.ofMillis(configuration.getDeadline().toMillis())); - // For testing, we need to hack around our gRPC config. - if (configuration.getInsecureEndpoint()) { - builder.setCredentialsProvider(NoCredentialsProvider.create()); - builder.setTransportChannelProvider( - FixedTransportChannelProvider.create( - GrpcTransportChannel.create( - ManagedChannelBuilder.forTarget(configuration.getTraceServiceEndpoint()) - .usePlaintext() - .build()))); - } else { - Credentials credentials = - configuration.getCredentials() == null - ? GoogleCredentials.getApplicationDefault() - : configuration.getCredentials(); - builder.setCredentialsProvider( - FixedCredentialsProvider.create(checkNotNull(credentials, "credentials"))); - builder.setEndpoint(configuration.getTraceServiceEndpoint()); - builder.setHeaderProvider(HEADER_PROVIDER); - } - - return new InternalTraceExporter( - projectId, - new CloudTraceClientImpl(TraceServiceClient.create(builder.build())), - configuration.getAttributeMapping(), - configuration.getFixedAttributes()); + // We only use the batchWriteSpans API in this exporter. + builder + .batchWriteSpansSettings() + .setSimpleTimeoutNoRetries( + org.threeten.bp.Duration.ofMillis(configuration.getDeadline().toMillis())); + // For testing, we need to hack around our gRPC config. + if (configuration.getInsecureEndpoint()) { + builder.setCredentialsProvider(NoCredentialsProvider.create()); + builder.setTransportChannelProvider( + FixedTransportChannelProvider.create( + GrpcTransportChannel.create( + ManagedChannelBuilder.forTarget(configuration.getTraceServiceEndpoint()) + .usePlaintext() + .build()))); + } else { + Credentials credentials = + configuration.getCredentials() == null + ? GoogleCredentials.getApplicationDefault() + : configuration.getCredentials(); + builder.setCredentialsProvider( + FixedCredentialsProvider.create(checkNotNull(credentials, "credentials"))); + builder.setEndpoint(configuration.getTraceServiceEndpoint()); + builder.setHeaderProvider(HEADER_PROVIDER); } + return new InternalTraceExporter( projectId, - new CloudTraceClientImpl(TraceServiceClient.create(stub)), + new CloudTraceClientImpl(TraceServiceClient.create(builder.build())), configuration.getAttributeMapping(), configuration.getFixedAttributes()); } diff --git a/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/TraceConfiguration.java b/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/TraceConfiguration.java index af42e9e5..1d93c656 100644 --- a/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/TraceConfiguration.java +++ b/exporters/trace/src/main/java/com/google/cloud/opentelemetry/trace/TraceConfiguration.java @@ -19,7 +19,6 @@ import com.google.auth.Credentials; import com.google.auto.value.AutoValue; import com.google.cloud.ServiceOptions; -import com.google.cloud.trace.v2.stub.TraceServiceStub; import com.google.cloud.trace.v2.stub.TraceServiceStubSettings; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -89,15 +88,6 @@ public final String getProjectId() { return getProjectIdSupplier().get(); } - /** - * Returns a TraceServiceStub instance used to make RPC calls. - * - * @return the trace service stub. - */ - @Nullable - @Deprecated - public abstract TraceServiceStub getTraceServiceStub(); - /** * Returns the endpoint where to write traces. * @@ -186,16 +176,6 @@ public final Builder setProjectId(String projectId) { return this; } - /** - * Sets the trace service stub used to send gRPC calls. - * - * @deprecated("Use setTraceServiceEndpoint") - * @param traceServiceStub the {@code TraceServiceStub}. - * @return this. - */ - @Deprecated - public abstract Builder setTraceServiceStub(TraceServiceStub traceServiceStub); - /** Sets the endpoint where to write traces. Defaults to tracing.googleapis.com:443. */ public abstract Builder setTraceServiceEndpoint(String endpoint); diff --git a/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceConfigurationTest.java b/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceConfigurationTest.java index a1962b7f..0ddcfd69 100644 --- a/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceConfigurationTest.java +++ b/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceConfigurationTest.java @@ -16,6 +16,7 @@ package com.google.cloud.opentelemetry.trace; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; @@ -25,6 +26,7 @@ import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.ServiceOptions; +import com.google.cloud.trace.v2.TraceServiceSettings; import com.google.devtools.cloudtrace.v2.AttributeValue; import java.time.Duration; import java.util.Collections; @@ -53,7 +55,9 @@ public void defaultConfiguration() { assertNull(configuration.getCredentials()); assertNotNull(configuration.getProjectId()); - assertNull(configuration.getTraceServiceStub()); + assertFalse(configuration.getInsecureEndpoint()); + assertEquals( + configuration.getTraceServiceEndpoint(), TraceServiceSettings.getDefaultEndpoint()); assertTrue(configuration.getFixedAttributes().isEmpty()); assertEquals(TraceConfiguration.DEFAULT_DEADLINE, configuration.getDeadline()); } diff --git a/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceExporterTest.java b/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceExporterTest.java index 4ff755e2..4623f720 100644 --- a/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceExporterTest.java +++ b/exporters/trace/src/test/java/com/google/cloud/opentelemetry/trace/TraceExporterTest.java @@ -18,11 +18,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.ServiceOptions; import com.google.cloud.trace.v2.TraceServiceClient; import com.google.cloud.trace.v2.TraceServiceSettings; -import com.google.cloud.trace.v2.stub.TraceServiceStub; import com.google.devtools.cloudtrace.v2.ProjectName; import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.sdk.OpenTelemetrySdk; @@ -33,6 +33,7 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import java.io.IOException; import java.util.Collections; +import java.util.Date; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,26 +46,43 @@ public class TraceExporterTest { private static final String PROJECT_ID = "test-id"; + private static final String FAKE_ENDPOINT = "fake.endpoint.com:443"; + private static final GoogleCredentials FAKE_CREDENTIAL = + GoogleCredentials.newBuilder().setAccessToken(new AccessToken("fake", new Date(100))).build(); + @Mock private TraceServiceClient mockedTraceServiceClient; - @Mock private TraceServiceStub mockedTraceServiceStub; + @Mock private CloudTraceClient cloudTraceClient; @After public void tearDown() { GlobalOpenTelemetry.resetForTest(); } + @Test + public void testCreateWithConfigurationSucceeds() { + TraceConfiguration configuration = + TraceConfiguration.builder() + .setCredentials(FAKE_CREDENTIAL) + .setProjectId(PROJECT_ID) + .setTraceServiceEndpoint(FAKE_ENDPOINT) + .build(); + SpanExporter exporter = TraceExporter.createWithConfiguration(configuration); + assertNotNull(exporter); + } + @SuppressWarnings("ResultOfMethodCallIgnored") @Test public void verifyExporterWorksWithConfiguration() { try (MockedStatic mockedTraceServiceClient = Mockito.mockStatic(TraceServiceClient.class)) { mockedTraceServiceClient - .when(() -> TraceServiceClient.create(Mockito.eq(mockedTraceServiceStub))) + .when(() -> TraceServiceClient.create(Mockito.any(TraceServiceSettings.class))) .thenReturn(this.mockedTraceServiceClient); TraceConfiguration configuration = TraceConfiguration.builder() - .setTraceServiceStub(mockedTraceServiceStub) + .setCredentials(FAKE_CREDENTIAL) + .setTraceServiceEndpoint(FAKE_ENDPOINT) .setProjectId(PROJECT_ID) .build(); SpanExporter exporter = TraceExporter.createWithConfiguration(configuration); @@ -73,7 +91,8 @@ public void verifyExporterWorksWithConfiguration() { simulateExport(exporter); mockedTraceServiceClient.verify( - () -> TraceServiceClient.create(Mockito.eq(mockedTraceServiceStub))); + Mockito.times(1), + () -> TraceServiceClient.create(Mockito.any(TraceServiceSettings.class))); Mockito.verify(this.mockedTraceServiceClient) .batchWriteSpans((ProjectName) Mockito.any(), Mockito.anyList()); }