@@ -138,11 +138,12 @@ public void attemptSucceeded() {
138138
139139 @ Override
140140 public void responseHeadersReceived (java .util .Map <String , Object > headers ) {
141- if (attemptSpan != null ) {
142- long contentLength = extractContentLength (headers );
143- if (contentLength >= 0 ) {
144- attemptSpan .setAttribute (ObservabilityAttributes .HTTP_RESPONSE_BODY_SIZE , contentLength );
145- }
141+ if (attemptSpan == null ) {
142+ return ;
143+ }
144+ long contentLength = extractContentLength (headers );
145+ if (contentLength >= 0 ) {
146+ attemptSpan .setAttribute (ObservabilityAttributes .HTTP_RESPONSE_BODY_SIZE , contentLength );
146147 }
147148 }
148149
@@ -157,23 +158,22 @@ public void responseHeadersReceived(java.util.Map<String, Object> headers) {
157158 * @return the content length in bytes, or -1 if the header is missing or malformed.
158159 */
159160 private long extractContentLength (java .util .Map <String , Object > headers ) {
160- if (headers == null || headers .isEmpty ()) return -1 ;
161- // google-http-client HttpHeaders uses a case-insensitive map but we copy it for safety
162- // and to handle potential different implementations.
163- Object value =
164- headers .entrySet ().stream ()
165- .filter (e -> CONTENT_LENGTH_KEY .equalsIgnoreCase (e .getKey ()))
166- .map (Map .Entry ::getValue )
167- .findFirst ()
168- .orElse (null );
169-
170- if (value instanceof java .util .Collection ) {
171- value = ((java .util .Collection <?>) value ).stream ().findFirst ().orElse (null );
172- }
173-
174161 try {
162+ if (headers == null || headers .isEmpty ()) return -1 ;
163+ // google-http-client HttpHeaders uses a case-insensitive map but we copy it for safety
164+ // and to handle potential different implementations.
165+ Object value =
166+ headers .entrySet ().stream ()
167+ .filter (e -> CONTENT_LENGTH_KEY .equalsIgnoreCase (e .getKey ()))
168+ .map (Map .Entry ::getValue )
169+ .findFirst ()
170+ .orElse (null );
171+
172+ if (value instanceof java .util .Collection ) {
173+ value = ((java .util .Collection <?>) value ).stream ().findFirst ().orElse (null );
174+ }
175175 return Long .parseLong (value .toString ());
176- } catch (NumberFormatException | NullPointerException e ) {
176+ } catch (Exception e ) {
177177 return -1 ;
178178 }
179179 }
0 commit comments