Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metric Attributes #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.google.auto.value.AutoValue;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/** Implementation of TransportChannel based on gRPC. */
Expand Down Expand Up @@ -100,17 +102,19 @@ public void close() {
}

public static Builder newBuilder() {
return new AutoValue_GrpcTransportChannel.Builder();
return new AutoValue_GrpcTransportChannel.Builder().setConfigurations(new HashMap<>());
}

public static GrpcTransportChannel create(ManagedChannel channel) {
return newBuilder().setManagedChannel(channel).build();
return newBuilder().setManagedChannel(channel).setConfigurations(new HashMap<>()).build();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setManagedChannel(ManagedChannel value);

abstract Builder setConfigurations(Map<String, String> configurations);

public abstract GrpcTransportChannel build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.internal.EnvironmentProvider;
import com.google.api.gax.rpc.mtls.MtlsProvider;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -239,9 +240,15 @@ public TransportChannel getTransportChannel() throws IOException {
}

private TransportChannel createChannel() throws IOException {
return GrpcTransportChannel.create(
ChannelPool.create(
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel));
return GrpcTransportChannel.newBuilder()
.setManagedChannel(
ChannelPool.create(
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel))
.setConfigurations(
ImmutableMap.of(
ApiTracerFactory.MetricAttribute.DIRECTPATH_ENABLED.getAttribute(),
String.valueOf(isCompatibleForDirectPath())))
.build();
}

private boolean isDirectPathEnabled() {
Expand Down Expand Up @@ -370,10 +377,7 @@ private ManagedChannel createSingleChannel() throws IOException {

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

/**
* DirectPath must be enabled via the settings and a few other configurations/settings must also
* be valid for the request to go through DirectPath (valid credentials, running on Compute, and
* routed through Google Servers).
*
* @return if DirectPath is enabled for the client and is compatible to be used with DirectPath
*/
public boolean isCompatibleForDirectPath() {
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 @@ -32,6 +32,8 @@
import com.google.api.core.InternalExtensionOnly;
import com.google.api.gax.rpc.TransportChannel;
import com.google.auto.value.AutoValue;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/** Implementation of TransportChannel based on http/json. */
Expand Down Expand Up @@ -97,13 +99,15 @@ public static Builder newBuilder() {
}

public static HttpJsonTransportChannel create(ManagedHttpJsonChannel channel) {
return newBuilder().setManagedChannel(channel).build();
return newBuilder().setManagedChannel(channel).setConfigurations(new HashMap<>()).build();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setManagedChannel(ManagedHttpJsonChannel value);

abstract Builder setConfigurations(Map<String, String> configurations);

public abstract HttpJsonTransportChannel build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.mtls.MtlsProvider;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.auth.Credentials;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
Expand Down Expand Up @@ -203,7 +205,12 @@ private HttpJsonTransportChannel createChannel() throws IOException, GeneralSecu
}
}

return HttpJsonTransportChannel.newBuilder().setManagedChannel(channel).build();
return HttpJsonTransportChannel.newBuilder()
.setManagedChannel(channel)
.setConfigurations(
ImmutableMap.of(
ApiTracerFactory.MetricAttribute.DIRECTPATH_ENABLED.getAttribute(), "false"))
.build();
}

/** The endpoint to be used for the channel. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public void testWithTransportChannel() {

HttpJsonCallContext context =
HttpJsonCallContext.createDefault()
.withTransportChannel(
HttpJsonTransportChannel.newBuilder().setManagedChannel(channel).build());
.withTransportChannel(HttpJsonTransportChannel.create(channel));
Truth.assertThat(context.getChannel()).isSameInstanceAs(channel);
}

Expand Down
10 changes: 10 additions & 0 deletions gax-java/gax/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@
<className>com/google/api/gax/rpc/ApiCallContext</className>
<method>* validateUniverseDomain()</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/api/gax/rpc/TransportChannel</className>
<method>* getConfigurations()</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/api/gax/tracing/ApiTracerFactory</className>
<method>* addAttributes(*)</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public static ClientContext create(ClientSettings settings) throws IOException {
* settings.
*/
public static ClientContext create(StubSettings settings) throws IOException {
Map<String, String> clientInitializationOtelAttributes = new HashMap<>();

ApiClock clock = settings.getClock();

ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
Expand Down Expand Up @@ -224,6 +226,9 @@ public static ClientContext create(StubSettings settings) throws IOException {
transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
}
TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
clientInitializationOtelAttributes.put(
"directpath_enabled",
String.valueOf(transportChannel.getConfigurations().get("directpath_enabled")));

ApiCallContext defaultCallContext =
transportChannel.getEmptyCallContext().withTransportChannel(transportChannel);
Expand Down Expand Up @@ -261,6 +266,9 @@ public static ClientContext create(StubSettings settings) throws IOException {
backgroundResources.add(watchdog);
}

ApiTracerFactory apiTracerFactory = settings.getTracerFactory();
apiTracerFactory.addAttributes(clientInitializationOtelAttributes);

return newBuilder()
.setBackgroundResources(backgroundResources.build())
.setExecutor(backgroundExecutor)
Expand All @@ -276,7 +284,7 @@ public static ClientContext create(StubSettings settings) throws IOException {
.setQuotaProjectId(settings.getQuotaProjectId())
.setStreamWatchdog(watchdog)
.setStreamWatchdogCheckInterval(settings.getStreamWatchdogCheckInterval())
.setTracerFactory(settings.getTracerFactory())
.setTracerFactory(apiTracerFactory)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.google.api.core.InternalExtensionOnly;
import com.google.api.gax.core.BackgroundResource;
import java.util.Map;

/** Class whose instances can issue RPCs on a particular transport. */
@InternalExtensionOnly
Expand All @@ -47,4 +48,6 @@ public interface TransportChannel extends BackgroundResource {
* Returns an empty {@link ApiCallContext} that is compatible with this {@code TransportChannel}.
*/
ApiCallContext getEmptyCallContext();

Map<String, String> getConfigurations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import com.google.api.core.InternalApi;
import com.google.api.core.InternalExtensionOnly;
import com.google.common.collect.ImmutableSet;
import java.util.Map;
import java.util.Set;

/**
* A factory to create new instances of {@link ApiTracer}s.
Expand All @@ -53,6 +56,36 @@ enum OperationType {
BidiStreaming
}

enum MetricFlags {
DIRECTPATH(
ImmutableSet.of(MetricAttribute.DIRECTPATH_ENABLED, MetricAttribute.DIRECTPATH_USED));

private final Set<MetricAttribute> otelAttributeKey;

MetricFlags(Set<MetricAttribute> otelAttributeKey) {
this.otelAttributeKey = otelAttributeKey;
}

public Set<MetricAttribute> getOtelAttributeKey() {
return otelAttributeKey;
}
}

enum MetricAttribute {
DIRECTPATH_ENABLED("directpath_enabled"),
DIRECTPATH_USED("directpath_used");

private final String attribute;

MetricAttribute(String attribute) {
this.attribute = attribute;
}

public String getAttribute() {
return attribute;
}
}

/**
* Create a new {@link ApiTracer} that will be a child of the current context.
*
Expand All @@ -61,4 +94,6 @@ enum OperationType {
* @param operationType the type of operation that the tracer will trace
*/
ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType);

default void addAttributes(Map<String, String> attributes) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

private final Set<MetricFlags> metricFlagsSet;

private final Map<String, String> additionalDefaultAttributes;

public MetricsTracerFactory(MetricsRecorder metricsRecorder) {
this(metricsRecorder, ImmutableSet.of());
}

public MetricsTracerFactory(MetricsRecorder metricsRecorder, Set<MetricFlags> metricFlagsSet) {
this.metricsRecorder = metricsRecorder;
this.metricFlagsSet = metricFlagsSet;
this.additionalDefaultAttributes = new HashMap<>();
}

@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> additionalDefaultAttribute :
additionalDefaultAttributes.entrySet()) {
metricsTracer.addAttributes(
additionalDefaultAttribute.getKey(), additionalDefaultAttribute.getValue());
}
return metricsTracer;
}

@Override
public void addAttributes(Map<String, String> attributes) {
for (Map.Entry<String, String> attributeEntry : attributes.entrySet()) {
String attribute = attributeEntry.getKey();
for (MetricFlags metricFlags : metricFlagsSet) {
for (MetricAttribute metricAttribute : metricFlags.getOtelAttributeKey()) {
if (metricAttribute.getAttribute().equals(attribute)) {
additionalDefaultAttributes.put(attributeEntry.getKey(), attributeEntry.getValue());
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.TransportChannel;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -65,6 +66,11 @@ public FakeCallContext getEmptyCallContext() {
return FakeCallContext.createDefault();
}

@Override
public Map<String, String> getConfigurations() {
return ImmutableMap.of();
}

@Override
public void shutdown() {
isShutdown = true;
Expand Down
Loading