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

fix: The metadata could be returned in trailer or header depends on i… #1337

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

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 @@ -104,11 +104,20 @@ public void onError(Throwable t) {
Long latency = Util.getGfeLatency(metadata);
tracer.recordGfeMetadata(latency, t);
try {
byte[] trailers =
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
} catch (NullPointerException | InvalidProtocolBufferException e) {
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());
}
} catch (InvalidProtocolBufferException e) {
}

outerObserver.onError(t);
Expand All @@ -120,11 +129,20 @@ public void onComplete() {
Long latency = Util.getGfeLatency(metadata);
tracer.recordGfeMetadata(latency, null);
try {
byte[] trailers =
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
} catch (NullPointerException | InvalidProtocolBufferException e) {
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());
}
} catch (InvalidProtocolBufferException e) {
}

outerObserver.onComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,20 @@ public void onFailure(Throwable throwable) {
Long latency = Util.getGfeLatency(metadata);
tracer.recordGfeMetadata(latency, throwable);
try {
byte[] trailers =
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
} catch (NullPointerException | InvalidProtocolBufferException e) {
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());
}
} catch (InvalidProtocolBufferException e) {
}
}

Expand All @@ -97,11 +106,20 @@ public void onSuccess(ResponseT response) {
Long latency = Util.getGfeLatency(metadata);
tracer.recordGfeMetadata(latency, null);
try {
byte[] trailers =
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
} catch (NullPointerException | InvalidProtocolBufferException e) {
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());
}
} catch (InvalidProtocolBufferException e) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class Util {
private static final Metadata.Key<String> SERVER_TIMING_HEADER_KEY =
Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER);
private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?<dur>\\d+)");

static final String RESPONSE_PRAMS_KEY = "x-goog-ext-425905942-bin";
static final Metadata.Key<byte[]> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down