Skip to content

Commit

Permalink
feat: Add DirectPath Compatible Getter to gRPC channel provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Apr 2, 2024
1 parent d0c5191 commit 05b1cde
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,7 @@ private ManagedChannel createSingleChannel() throws IOException {

// Check DirectPath traffic.
boolean useDirectPathXds = false;
if (isDirectPathEnabled()
&& isCredentialDirectPathCompatible()
&& isOnComputeEngine()
&& canUseDirectPathWithUniverseDomain()) {
if (canUseDirectPath()) {
CallCredentials callCreds = MoreCallCredentials.from(credentials);
ChannelCredentials channelCreds =
GoogleDefaultChannelCredentials.newBuilder().callCredentials(callCreds).build();
Expand Down Expand Up @@ -446,6 +443,24 @@ && canUseDirectPathWithUniverseDomain()) {
return managedChannel;
}

/**
* Marked as Internal Api and intended for internal use. DirectPath must be enabled via the
* settings and a few other configurations/settings must also be valid for the request to go
* through DirectPath.
*
* <p>Checks: 1. Credentials are compatible 2.Running on Compute Engine 3. Universe Domain is
* configured to for the Google Default Universe
*
* @return if DirectPath is enabled for the client AND if the configurations are valid
*/
@InternalApi
public boolean canUseDirectPath() {
return isDirectPathEnabled()
&& isCredentialDirectPathCompatible()
&& isOnComputeEngine()
&& canUseDirectPathWithUniverseDomain();
}

/** The endpoint to be used for the channel. */
@Override
public String getEndpoint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class MetricsTracer implements ApiTracer {
"Operation has already been completed";
private Stopwatch attemptTimer;
private final Stopwatch operationTimer = Stopwatch.createStarted();
// These are RPC specific attributes and pertain to a specific API Trace
private final Map<String, String> attributes = new HashMap<>();
private final MetricsRecorder metricsRecorder;
private final AtomicBoolean operationFinished;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.common.collect.ImmutableMap;
import java.util.Map;

/**
* A {@link ApiTracerFactory} to build instances of {@link MetricsTracer}.
Expand All @@ -45,13 +47,31 @@
public class MetricsTracerFactory implements ApiTracerFactory {
protected MetricsRecorder metricsRecorder;

// These are client attributes and pertain every single trace
private final Map<String, String> attributes;

/** Creates a MetricsTracerFactory with no additional client level attributes. */
public MetricsTracerFactory(MetricsRecorder metricsRecorder) {
this(metricsRecorder, ImmutableMap.of());
}

/**
* Pass in a Map of client level attributes which will be added to every single MetricsTracer
* created from the ApiTracerFactory.
*/
public MetricsTracerFactory(MetricsRecorder metricsRecorder, Map<String, String> attributes) {
this.metricsRecorder = metricsRecorder;
this.attributes = attributes;
}

@Override
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) {
return new MetricsTracer(
MethodName.of(spanName.getClientName(), spanName.getMethodName()), metricsRecorder);
MetricsTracer metricsTracer =
new MetricsTracer(
MethodName.of(spanName.getClientName(), spanName.getMethodName()), metricsRecorder);
for (Map.Entry<String, String> attributeEntrySet : attributes.entrySet()) {
metricsTracer.addAttributes(attributeEntrySet.getKey(), attributeEntrySet.getValue());
}
return metricsTracer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
import static org.mockito.Mockito.when;

import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import com.google.common.collect.ImmutableMap;
import com.google.common.truth.Truth;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

public class MetricsTracerFactoryTest {
private static final int DEFAULT_ATTRIBUTES_COUNT = 2;
@Mock private MetricsRecorder metricsRecorder;
@Mock private ApiTracer parent;
private SpanName spanName;
Expand All @@ -56,26 +59,39 @@ public void setUp() {

@Test
public void testNewTracer_notNull() {
// Call the newTracer method
ApiTracer apiTracer = metricsTracerFactory.newTracer(parent, spanName, OperationType.Unary);

// Assert that the apiTracer created has expected type and not null
Truth.assertThat(apiTracer).isInstanceOf(MetricsTracer.class);
Truth.assertThat(apiTracer).isNotNull();
Truth.assertThat(apiTracer).isInstanceOf(MetricsTracer.class);
}

@Test
public void testNewTracer_HasCorrectParameters() {

// Call the newTracer method
ApiTracer apiTracer = metricsTracerFactory.newTracer(parent, spanName, OperationType.Unary);
public void testNewTracer_hasCorrectNumberAttributes_hasDefaultAttributes() {
MetricsTracer metricsTracer =
(MetricsTracer) metricsTracerFactory.newTracer(parent, spanName, OperationType.Unary);
Map<String, String> attributes = metricsTracer.getAttributes();
Truth.assertThat(attributes.size()).isEqualTo(DEFAULT_ATTRIBUTES_COUNT);
Truth.assertThat(attributes.get(MetricsTracer.METHOD_NAME_ATTRIBUTE))
.isEqualTo("testService.testMethod");
Truth.assertThat(attributes.get(MetricsTracer.LANGUAGE_ATTRIBUTE))
.isEqualTo(MetricsTracer.DEFAULT_LANGUAGE);
}

// Assert that the apiTracer created has expected type and not null
Truth.assertThat(apiTracer).isInstanceOf(MetricsTracer.class);
Truth.assertThat(apiTracer).isNotNull();
@Test
public void testClientAttributes_additionalClientAttributes() {
Map<String, String> clientAttributes =
ImmutableMap.of("attribute1", "value1", "attribute2", "value2");
MetricsTracerFactory metricsTracerFactory =
new MetricsTracerFactory(metricsRecorder, clientAttributes);

MetricsTracer metricsTracer = (MetricsTracer) apiTracer;
Truth.assertThat(metricsTracer.getAttributes().get("method_name"))
.isEqualTo("testService.testMethod");
MetricsTracer metricsTracer =
(MetricsTracer) metricsTracerFactory.newTracer(parent, spanName, OperationType.Unary);
Map<String, String> attributes = metricsTracer.getAttributes();
Truth.assertThat(attributes.size())
.isEqualTo(DEFAULT_ATTRIBUTES_COUNT + clientAttributes.size());
// Default attributes already tested above
Truth.assertThat(attributes.containsKey("attribute1")).isTrue();
Truth.assertThat(attributes.containsKey("attribute2")).isTrue();
}
}

0 comments on commit 05b1cde

Please sign in to comment.