Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 11950af

Browse files
committed
fix(tracing): add body_size tracking to OpenTelemetry SpanTracer
1 parent 7bc8aa0 commit 11950af

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ public void attemptSucceeded() {
122122

123123
@Override
124124
public void responseHeadersReceived(java.util.Map<String, ?> headers) {
125-
if (attemptHandle != null) {
125+
if (attemptSpan != null) {
126126
long contentLength = extractContentLength(headers);
127127
if (contentLength >= 0) {
128-
attemptHandle.setAttribute(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE, contentLength);
128+
attemptSpan.setAttribute(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE, contentLength);
129129
}
130130
}
131131
}
@@ -136,24 +136,28 @@ private long extractContentLength(java.util.Map<String, ?> headers) {
136136
}
137137
for (Map.Entry<String, ?> entry : headers.entrySet()) {
138138
if ("Content-Length".equalsIgnoreCase(entry.getKey())) {
139-
Object value = entry.getValue();
140-
if (value != null) {
141-
try {
142-
String contentLengthStr =
143-
value instanceof java.util.List
144-
? ((java.util.List<?>) value).get(0).toString()
145-
: value.toString();
146-
return Long.parseLong(contentLengthStr);
147-
} catch (NumberFormatException | IndexOutOfBoundsException e) {
148-
// Ignore invalid Content-Length
149-
}
150-
}
151-
break;
139+
return parseContentLength(entry.getValue());
152140
}
153141
}
154142
return -1;
155143
}
156144

145+
private long parseContentLength(Object value) {
146+
if (value == null) {
147+
return -1;
148+
}
149+
try {
150+
String contentLengthStr =
151+
value instanceof java.util.List
152+
? ((java.util.List<?>) value).get(0).toString()
153+
: value.toString();
154+
return Long.parseLong(contentLengthStr);
155+
} catch (NumberFormatException | IndexOutOfBoundsException e) {
156+
// Ignore invalid Content-Length
157+
return -1;
158+
}
159+
}
160+
157161
private void endAttempt() {
158162
if (attemptSpan != null) {
159163
attemptSpan.end();

0 commit comments

Comments
 (0)