Skip to content

Commit

Permalink
Remove usgae of deprecated service stub API
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Mar 13, 2024
1 parent d3abd2a commit c36e98c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<TraceServiceClient> 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);
Expand All @@ -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());
}
Expand Down

0 comments on commit c36e98c

Please sign in to comment.