From 19db687a09f7a2a8b12099065dff5390af494905 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Tue, 2 Aug 2022 11:40:46 -0400 Subject: [PATCH 1/4] fix: The metadata could be returned in trailer or header depends on if sidecar is enabled. Check both for now. --- .../stub/metrics/BigtableTracerStreamingCallable.java | 11 +++++++++-- .../v2/stub/metrics/BigtableTracerUnaryCallable.java | 11 +++++++++-- .../cloud/bigtable/data/v2/stub/metrics/Util.java | 4 ++-- .../v2/stub/metrics/BuiltinMetricsTracerTest.java | 4 +--- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java index 6f636bf55d..beba2ffc29 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java @@ -22,6 +22,7 @@ import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.api.gax.rpc.StreamController; import com.google.bigtable.v2.ResponseParams; +import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.base.Stopwatch; import com.google.protobuf.InvalidProtocolBufferException; @@ -104,8 +105,11 @@ public void onError(Throwable t) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, t); try { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + (byte[]) + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { @@ -120,8 +124,11 @@ public void onComplete() { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, null); try { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + (byte[]) + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java index 0efc99fe4d..e5faf392b4 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java @@ -23,6 +23,7 @@ import com.google.api.gax.rpc.ApiCallContext; import com.google.api.gax.rpc.UnaryCallable; import com.google.bigtable.v2.ResponseParams; +import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.InvalidProtocolBufferException; @@ -83,8 +84,11 @@ public void onFailure(Throwable throwable) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, throwable); try { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + (byte[]) + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { @@ -97,8 +101,11 @@ public void onSuccess(ResponseT response) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, null); try { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + (byte[]) + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java index 7487703fc0..9477c7da71 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java @@ -57,8 +57,8 @@ public class Util { private static final Metadata.Key SERVER_TIMING_HEADER_KEY = Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER); private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?\\d+)"); - - static final String RESPONSE_PRAMS_KEY = "x-goog-ext-425905942-bin"; + static final Metadata.Key METADATA_KEY = + Metadata.Key.of("x-goog-ext-425905942-bin", Metadata.BINARY_BYTE_MARSHALLER); /** Convert an exception into a value that can be used to create an OpenCensus tag value. */ static String extractStatus(@Nullable Throwable error) { diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java index b9bd2a926c..9ea222d012 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java @@ -127,9 +127,7 @@ public void sendHeaders(Metadata headers) { ResponseParams params = ResponseParams.newBuilder().setZoneId(ZONE).setClusterId(CLUSTER).build(); byte[] byteArray = params.toByteArray(); - headers.put( - Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER), - byteArray); + headers.put(Util.METADATA_KEY, byteArray); super.sendHeaders(headers); } From 31bee69f0d3d2c6cf62404818aa8ff104e374213 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Tue, 2 Aug 2022 16:16:45 -0400 Subject: [PATCH 2/4] fix --- .../BigtableTracerStreamingCallable.java | 30 +++++++++++-------- .../metrics/BigtableTracerUnaryCallable.java | 30 +++++++++++-------- .../bigtable/data/v2/stub/metrics/Util.java | 2 +- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java index beba2ffc29..9ed4e93779 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java @@ -107,12 +107,15 @@ public void onError(Throwable t) { try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - (byte[]) - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); - } catch (NullPointerException | InvalidProtocolBufferException e) { + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } + } catch (InvalidProtocolBufferException e) { } outerObserver.onError(t); @@ -126,12 +129,15 @@ public void onComplete() { try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - (byte[]) - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); - } catch (NullPointerException | InvalidProtocolBufferException e) { + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } + } catch (InvalidProtocolBufferException e) { } outerObserver.onComplete(); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java index e5faf392b4..c8b35a68a3 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java @@ -86,12 +86,15 @@ public void onFailure(Throwable throwable) { try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - (byte[]) - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); - } catch (NullPointerException | InvalidProtocolBufferException e) { + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } + } catch (InvalidProtocolBufferException e) { } } @@ -103,12 +106,15 @@ public void onSuccess(ResponseT response) { try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); byte[] trailers = - (byte[]) - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); - } catch (NullPointerException | InvalidProtocolBufferException e) { + MoreObjects.firstNonNull( + metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } + } catch (InvalidProtocolBufferException e) { } } } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java index 9477c7da71..0e356ebaf9 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java @@ -57,7 +57,7 @@ public class Util { private static final Metadata.Key SERVER_TIMING_HEADER_KEY = Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER); private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?\\d+)"); - static final Metadata.Key METADATA_KEY = + static final Metadata.Key METADATA_KEY = Metadata.Key.of("x-goog-ext-425905942-bin", Metadata.BINARY_BYTE_MARSHALLER); /** Convert an exception into a value that can be used to create an OpenCensus tag value. */ From 5d26166107784152562594c2b1bfefc6e54641f2 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Tue, 2 Aug 2022 16:42:12 -0400 Subject: [PATCH 3/4] fix npe --- .../BigtableTracerStreamingCallable.java | 19 ++++++++++++------- .../metrics/BigtableTracerUnaryCallable.java | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java index 9ed4e93779..dee44d8a77 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java @@ -22,7 +22,6 @@ import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.api.gax.rpc.StreamController; import com.google.bigtable.v2.ResponseParams; -import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.base.Stopwatch; import com.google.protobuf.InvalidProtocolBufferException; @@ -106,9 +105,12 @@ public void onError(Throwable t) { tracer.recordGfeMetadata(latency, t); try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); - byte[] trailers = - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // Check both headers and trailers because in different environments the metadata + // could be returned in headers or trailers + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } // If the response is terminated abnormally and we didn't get location information in // trailers or headers, skip setting the locations if (trailers != null) { @@ -128,9 +130,12 @@ public void onComplete() { tracer.recordGfeMetadata(latency, null); try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); - byte[] trailers = - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // Check both headers and trailers because in different environments the metadata + // could be returned in headers or trailers + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } // If the response is terminated abnormally and we didn't get location information in // trailers or headers, skip setting the locations if (trailers != null) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java index c8b35a68a3..9df6e8f4e1 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java @@ -23,7 +23,6 @@ import com.google.api.gax.rpc.ApiCallContext; import com.google.api.gax.rpc.UnaryCallable; import com.google.bigtable.v2.ResponseParams; -import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.InvalidProtocolBufferException; @@ -85,9 +84,12 @@ public void onFailure(Throwable throwable) { tracer.recordGfeMetadata(latency, throwable); try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); - byte[] trailers = - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // Check both headers and trailers because in different environments the metadata + // could be returned in headers or trailers + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } // If the response is terminated abnormally and we didn't get location information in // trailers or headers, skip setting the locations if (trailers != null) { @@ -105,9 +107,12 @@ public void onSuccess(ResponseT response) { tracer.recordGfeMetadata(latency, null); try { Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); - byte[] trailers = - MoreObjects.firstNonNull( - metadata.get(Util.METADATA_KEY), trailingMetadata.get(Util.METADATA_KEY)); + // Check both headers and trailers because in different environments the metadata + // could be returned in headers or trailers + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } // If the response is terminated abnormally and we didn't get location information in // trailers or headers, skip setting the locations if (trailers != null) { From ee51d24d235b0997eb84e69f9de51046b6d743d6 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Wed, 3 Aug 2022 11:16:30 -0400 Subject: [PATCH 4/4] fix NPE when metadata is null --- .../BigtableTracerStreamingCallable.java | 48 +++++++++++-------- .../metrics/BigtableTracerUnaryCallable.java | 48 +++++++++++-------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java index dee44d8a77..c7f09c4db1 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java @@ -104,18 +104,22 @@ public void onError(Throwable t) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, t); try { - Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); // Check both headers and trailers because in different environments the metadata // could be returned in headers or trailers - byte[] trailers = metadata.get(Util.METADATA_KEY); - if (trailers == null) { - trailers = trailingMetadata.get(Util.METADATA_KEY); - } - // If the response is terminated abnormally and we didn't get location information in - // trailers or headers, skip setting the locations - if (trailers != null) { - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + if (metadata != null) { + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); + if (trailingMetadata != null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } + } + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } } } catch (InvalidProtocolBufferException e) { } @@ -129,18 +133,22 @@ public void onComplete() { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, null); try { - Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); // Check both headers and trailers because in different environments the metadata // could be returned in headers or trailers - byte[] trailers = metadata.get(Util.METADATA_KEY); - if (trailers == null) { - trailers = trailingMetadata.get(Util.METADATA_KEY); - } - // If the response is terminated abnormally and we didn't get location information in - // trailers or headers, skip setting the locations - if (trailers != null) { - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + if (metadata != null) { + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); + if (trailingMetadata != null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } + } + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } } } catch (InvalidProtocolBufferException e) { } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java index 9df6e8f4e1..50d24ecbaf 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java @@ -83,18 +83,22 @@ public void onFailure(Throwable throwable) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, throwable); try { - Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); // Check both headers and trailers because in different environments the metadata // could be returned in headers or trailers - byte[] trailers = metadata.get(Util.METADATA_KEY); - if (trailers == null) { - trailers = trailingMetadata.get(Util.METADATA_KEY); - } - // If the response is terminated abnormally and we didn't get location information in - // trailers or headers, skip setting the locations - if (trailers != null) { - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + if (metadata != null) { + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); + if (trailingMetadata != null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } + } + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } } } catch (InvalidProtocolBufferException e) { } @@ -106,18 +110,22 @@ public void onSuccess(ResponseT response) { Long latency = Util.getGfeLatency(metadata); tracer.recordGfeMetadata(latency, null); try { - Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); // Check both headers and trailers because in different environments the metadata // could be returned in headers or trailers - byte[] trailers = metadata.get(Util.METADATA_KEY); - if (trailers == null) { - trailers = trailingMetadata.get(Util.METADATA_KEY); - } - // If the response is terminated abnormally and we didn't get location information in - // trailers or headers, skip setting the locations - if (trailers != null) { - ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); - tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + if (metadata != null) { + byte[] trailers = metadata.get(Util.METADATA_KEY); + if (trailers == null) { + Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); + if (trailingMetadata != null) { + trailers = trailingMetadata.get(Util.METADATA_KEY); + } + } + // If the response is terminated abnormally and we didn't get location information in + // trailers or headers, skip setting the locations + if (trailers != null) { + ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); + tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); + } } } catch (InvalidProtocolBufferException e) { }