Skip to content

Commit 9202fa7

Browse files
authored
Merge branch 'main' into feat/actionable-errors-logging-utils
2 parents 182ce02 + 8be3f9d commit 9202fa7

File tree

22 files changed

+614
-101
lines changed

22 files changed

+614
-101
lines changed

.github/workflows/sonar.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
jobs:
99
build:
1010
name: Build
11+
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request'
1112
runs-on: ubuntu-22.04
1213
steps:
1314
- uses: actions/checkout@v3

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,8 @@ ChannelCredentials createS2ASecuredChannelCredentials() {
693693
return s2aChannelCredentials;
694694
}
695695

696-
private ManagedChannel createSingleChannel() throws IOException {
697-
GrpcHeaderInterceptor headerInterceptor =
698-
new GrpcHeaderInterceptor(headersWithDuplicatesRemoved);
699-
700-
GrpcMetadataHandlerInterceptor metadataHandlerInterceptor =
701-
new GrpcMetadataHandlerInterceptor();
702-
696+
@InternalApi("For internal use by google-cloud-java clients only")
697+
public ManagedChannelBuilder<?> createChannelBuilder() throws IOException {
703698
int colon = endpoint.lastIndexOf(':');
704699
if (colon < 0) {
705700
throw new IllegalStateException("invalid endpoint - should have been validated: " + endpoint);
@@ -775,8 +770,19 @@ private ManagedChannel createSingleChannel() throws IOException {
775770
// See https://github.com/googleapis/gapic-generator/issues/2816
776771
builder.disableServiceConfigLookUp();
777772
}
778-
builder =
779-
builder
773+
return builder;
774+
}
775+
776+
@InternalApi("For internal use by google-cloud-java clients only")
777+
public ManagedChannelBuilder<?> createDecoratedChannelBuilder() throws IOException {
778+
GrpcHeaderInterceptor headerInterceptor =
779+
new GrpcHeaderInterceptor(headersWithDuplicatesRemoved);
780+
781+
GrpcMetadataHandlerInterceptor metadataHandlerInterceptor =
782+
new GrpcMetadataHandlerInterceptor();
783+
784+
ManagedChannelBuilder<?> builder =
785+
createChannelBuilder()
780786
.intercept(new GrpcChannelUUIDInterceptor())
781787
.intercept(new GrpcLoggingInterceptor())
782788
.intercept(headerInterceptor)
@@ -806,6 +812,12 @@ private ManagedChannel createSingleChannel() throws IOException {
806812
builder = channelConfigurator.apply(builder);
807813
}
808814

815+
return builder;
816+
}
817+
818+
private ManagedChannel createSingleChannel() throws IOException {
819+
ManagedChannelBuilder<?> builder = createDecoratedChannelBuilder();
820+
809821
ManagedChannel managedChannel = builder.build();
810822
if (channelPrimer != null) {
811823
channelPrimer.primeChannel(managedChannel);
@@ -1422,7 +1434,7 @@ public ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> getChannelConfi
14221434
}
14231435

14241436
private static ImmutableMap<String, ?> getDefaultDirectPathServiceConfig() {
1425-
// When channel pooling is enabled, force the pick_first grpclb strategy.
1437+
// When channel pooling is enabled, force the pick_first strategy.
14261438
// This is necessary to avoid the multiplicative effect of creating channel pool with
14271439
// `poolSize` number of `ManagedChannel`s, each with a `subSetting` number of number of
14281440
// subchannels.
@@ -1431,13 +1443,8 @@ public ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> getChannelConfi
14311443
ImmutableMap<String, Object> pickFirstStrategy =
14321444
ImmutableMap.<String, Object>of("pick_first", ImmutableMap.of());
14331445

1434-
ImmutableMap<String, Object> childPolicy =
1435-
ImmutableMap.<String, Object>of("childPolicy", ImmutableList.of(pickFirstStrategy));
1436-
1437-
ImmutableMap<String, Object> grpcLbPolicy =
1438-
ImmutableMap.<String, Object>of("grpclb", childPolicy);
1439-
1440-
return ImmutableMap.<String, Object>of("loadBalancingConfig", ImmutableList.of(grpcLbPolicy));
1446+
return ImmutableMap.<String, Object>of(
1447+
"loadBalancingConfig", ImmutableList.of(pickFirstStrategy));
14411448
}
14421449

14431450
private static void validateEndpoint(String endpoint) {

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void testToBuilder() {
235235
builder -> {
236236
throw new UnsupportedOperationException();
237237
};
238-
Map<String, ?> directPathServiceConfig = ImmutableMap.of("loadbalancingConfig", "grpclb");
238+
Map<String, ?> directPathServiceConfig = ImmutableMap.of("loadBalancingConfig", "pick_first");
239239
List<InstantiatingGrpcChannelProvider.HardBoundTokenTypes> hardBoundTokenTypes =
240240
new ArrayList<>();
241241
hardBoundTokenTypes.add(InstantiatingGrpcChannelProvider.HardBoundTokenTypes.ALTS);
@@ -549,11 +549,7 @@ void testWithDefaultDirectPathServiceConfig() {
549549
List<Map<String, ?>> lbConfigs = getAsObjectList(defaultServiceConfig, "loadBalancingConfig");
550550
assertThat(lbConfigs).hasSize(1);
551551
Map<String, ?> lbConfig = lbConfigs.get(0);
552-
Map<String, ?> grpclb = getAsObject(lbConfig, "grpclb");
553-
List<Map<String, ?>> childPolicies = getAsObjectList(grpclb, "childPolicy");
554-
assertThat(childPolicies).hasSize(1);
555-
Map<String, ?> childPolicy = childPolicies.get(0);
556-
assertThat(childPolicy.keySet()).containsExactly("pick_first");
552+
assertThat(lbConfig.keySet()).containsExactly("pick_first");
557553
}
558554

559555
@Nullable
@@ -599,10 +595,10 @@ void testWithCustomDirectPathServiceConfig() {
599595
ImmutableMap<String, Object> childPolicy =
600596
ImmutableMap.<String, Object>of(
601597
"childPolicy", ImmutableList.of(pickFirstStrategy), "foo", "bar");
602-
ImmutableMap<String, Object> grpcLbPolicy =
603-
ImmutableMap.<String, Object>of("grpclb", childPolicy);
598+
ImmutableMap<String, Object> customLbPolicy =
599+
ImmutableMap.<String, Object>of("my_custom_lb", childPolicy);
604600
Map<String, Object> passedServiceConfig = new HashMap<>();
605-
passedServiceConfig.put("loadBalancingConfig", ImmutableList.of(grpcLbPolicy));
601+
passedServiceConfig.put("loadBalancingConfig", ImmutableList.of(customLbPolicy));
606602

607603
InstantiatingGrpcChannelProvider provider =
608604
InstantiatingGrpcChannelProvider.newBuilder()

gax-java/gax/src/main/java/com/google/api/gax/rpc/LibraryMetadata.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public abstract class LibraryMetadata {
6565
@Nullable
6666
public abstract String artifactName();
6767

68+
@Nullable
69+
public abstract String version();
70+
6871
public static LibraryMetadata empty() {
6972
return newBuilder().build();
7073
}
@@ -83,6 +86,8 @@ public abstract static class Builder {
8386

8487
public abstract Builder setArtifactName(@Nullable String artifactName);
8588

89+
public abstract Builder setVersion(@Nullable String version);
90+
8691
public abstract LibraryMetadata build();
8792
}
8893
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ String rpcSystemName() {
136136
@Nullable
137137
public abstract OperationType operationType();
138138

139+
/** The service name of a client (e.g. "bigtable", "spanner"). */
140+
@Nullable
141+
public abstract String serviceName();
142+
143+
/** The url domain of the request (e.g. "pubsub.googleapis.com"). */
144+
@Nullable
145+
public abstract String urlDomain();
146+
147+
/** The url template of the request (e.g. /v1/{name}:access). */
148+
@Nullable
149+
public abstract String urlTemplate();
150+
139151
/**
140152
* @return a map of attributes to be included in attempt-level spans
141153
*/
@@ -165,6 +177,34 @@ public Map<String, Object> getAttemptAttributes() {
165177
return attributes;
166178
}
167179

180+
Map<String, Object> getMetricsAttributes() {
181+
Map<String, Object> attributes = new HashMap<>();
182+
if (!Strings.isNullOrEmpty(serverAddress())) {
183+
attributes.put(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE, serverAddress());
184+
}
185+
if (serverPort() != null) {
186+
attributes.put(ObservabilityAttributes.SERVER_PORT_ATTRIBUTE, serverPort());
187+
}
188+
if (!Strings.isNullOrEmpty(serviceName())) {
189+
attributes.put(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE, serviceName());
190+
}
191+
if (!Strings.isNullOrEmpty(rpcSystemName())) {
192+
attributes.put(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE, rpcSystemName());
193+
}
194+
if (!Strings.isNullOrEmpty(fullMethodName())) {
195+
attributes.put(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, fullMethodName());
196+
}
197+
if (transport() == Transport.HTTP) {
198+
if (!Strings.isNullOrEmpty(urlDomain())) {
199+
attributes.put(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, urlDomain());
200+
}
201+
if (!Strings.isNullOrEmpty(urlTemplate())) {
202+
attributes.put(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE, urlTemplate());
203+
}
204+
}
205+
return attributes;
206+
}
207+
168208
/**
169209
* Merges this context with another context. The values in the other context take precedence.
170210
*
@@ -191,6 +231,15 @@ ApiTracerContext merge(ApiTracerContext other) {
191231
if (other.operationType() != null) {
192232
builder.setOperationType(other.operationType());
193233
}
234+
if (other.serviceName() != null) {
235+
builder.setServiceName(other.serviceName());
236+
}
237+
if (other.urlDomain() != null) {
238+
builder.setUrlDomain(other.urlDomain());
239+
}
240+
if (other.urlTemplate() != null) {
241+
builder.setUrlTemplate(other.urlTemplate());
242+
}
194243
return builder.build();
195244
}
196245

@@ -218,6 +267,12 @@ public abstract static class Builder {
218267

219268
public abstract Builder setServerPort(@Nullable Integer serverPort);
220269

270+
public abstract Builder setServiceName(@Nullable String serviceName);
271+
272+
public abstract Builder setUrlDomain(@Nullable String urlDomain);
273+
274+
public abstract Builder setUrlTemplate(@Nullable String urlTemplate);
275+
221276
public abstract ApiTracerContext build();
222277
}
223278
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class GoldenSignalsMetricsRecorder {
6565
.build();
6666
}
6767

68-
void recordOperationLatency(double operationLatency, Map<String, String> attributes) {
68+
void recordOperationLatency(double operationLatency, Map<String, Object> attributes) {
6969
clientRequestDurationRecorder.record(
7070
operationLatency, ObservabilityUtils.toOtelAttributes(attributes));
7171
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracer.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,23 @@
4848
class GoldenSignalsMetricsTracer implements ApiTracer {
4949
private final Stopwatch clientRequestTimer;
5050
private final GoldenSignalsMetricsRecorder metricsRecorder;
51-
private final Map<String, String> attributes = new HashMap<>();
51+
private final Map<String, Object> attributes;
5252

53-
/**
54-
* Creates the following instruments for the following metrics:
55-
*
56-
* <ul>
57-
* <li>Client Request Duration: Histogram
58-
* </ul>
59-
*
60-
* @param metricsRecorder OpenTelemetry
61-
*/
62-
GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder) {
53+
GoldenSignalsMetricsTracer(
54+
GoldenSignalsMetricsRecorder metricsRecorder, ApiTracerContext apiTracerContext) {
6355
this.clientRequestTimer = Stopwatch.createStarted();
6456
this.metricsRecorder = metricsRecorder;
57+
this.attributes = apiTracerContext.getMetricsAttributes();
6558
}
6659

6760
@VisibleForTesting
68-
GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder, Ticker ticker) {
61+
GoldenSignalsMetricsTracer(
62+
GoldenSignalsMetricsRecorder metricsRecorder,
63+
ApiTracerContext apiTracerContext,
64+
Ticker ticker) {
6965
this.clientRequestTimer = Stopwatch.createStarted(ticker);
7066
this.metricsRecorder = metricsRecorder;
67+
this.attributes = new HashMap<>(apiTracerContext.getMetricsAttributes());
7168
}
7269

7370
/**

gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class GoldenSignalsMetricsTracerFactory implements ApiTracerFactory {
4848

4949
public GoldenSignalsMetricsTracerFactory(OpenTelemetry openTelemetry) {
5050
this.openTelemetry = openTelemetry;
51+
this.apiTracerContext = ApiTracerContext.empty();
5152
}
5253

5354
@Override
@@ -57,7 +58,7 @@ public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType op
5758
// regular requests.
5859
return new BaseApiTracer();
5960
}
60-
return new GoldenSignalsMetricsTracer(metricsRecorder);
61+
return new GoldenSignalsMetricsTracer(metricsRecorder, apiTracerContext);
6162
}
6263

6364
@Override

gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityAttributes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,13 @@ public class ObservabilityAttributes {
6464
* gRPC status code (e.g., "OK", "INTERNAL").
6565
*/
6666
public static final String RPC_RESPONSE_STATUS_ATTRIBUTE = "rpc.response.status_code";
67+
68+
/** The service name of a client (e.g. "bigtable", "spanner"). */
69+
public static final String GCP_CLIENT_SERVICE_ATTRIBUTE = "gcp.client.service";
70+
71+
/** The url domain of the request (e.g. "pubsub.googleapis.com"). */
72+
public static final String URL_DOMAIN_ATTRIBUTE = "url.domain";
73+
74+
/** The url template of the request (e.g. /v1/{name}:access). */
75+
public static final String URL_TEMPLATE_ATTRIBUTE = "url.template";
6776
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityUtils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import com.google.api.gax.rpc.ApiException;
3333
import com.google.api.gax.rpc.StatusCode;
34-
import com.google.common.base.Preconditions;
3534
import io.opentelemetry.api.common.Attributes;
3635
import io.opentelemetry.api.common.AttributesBuilder;
3736
import java.util.Map;
@@ -57,10 +56,19 @@ static String extractStatus(@Nullable Throwable error) {
5756
return statusString;
5857
}
5958

60-
static Attributes toOtelAttributes(Map<String, String> attributes) {
61-
Preconditions.checkNotNull(attributes, "Attributes map cannot be null");
59+
static Attributes toOtelAttributes(Map<String, Object> attributes) {
6260
AttributesBuilder attributesBuilder = Attributes.builder();
63-
attributes.forEach(attributesBuilder::put);
61+
if (attributes == null) {
62+
return attributesBuilder.build();
63+
}
64+
attributes.forEach(
65+
(k, v) -> {
66+
if (v instanceof String) {
67+
attributesBuilder.put(k, (String) v);
68+
} else if (v instanceof Integer) {
69+
attributesBuilder.put(k, (long) (Integer) v);
70+
}
71+
});
6472
return attributesBuilder.build();
6573
}
6674
}

0 commit comments

Comments
 (0)