|
38 | 38 | import io.opentelemetry.api.trace.Tracer; |
39 | 39 | import java.util.HashMap; |
40 | 40 | import java.util.Map; |
| 41 | +import java.util.TreeMap; |
| 42 | +import java.util.function.Supplier; |
41 | 43 |
|
42 | 44 | /** An implementation of {@link ApiTracer} that uses OpenTelemetry to record traces. */ |
43 | 45 | @BetaApi |
@@ -138,29 +140,14 @@ public void responseHeadersReceived(java.util.Map<String, Object> headers) { |
138 | 140 | * @param headers the map of response headers. |
139 | 141 | * @return the content length in bytes, or -1 if the header is missing or malformed. |
140 | 142 | */ |
141 | | - private long extractContentLength(java.util.Map<String, Object> headers) { |
142 | | - if (headers == null) { |
| 143 | + private long extractContentLength(final java.util.Map<String, Object> headers) { |
| 144 | + final Map<String, Object> iHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
| 145 | + iHeaders.putAll(headers); |
| 146 | + Supplier<Object> headerGetter = () -> iHeaders.get(CONTENT_LENGTH_KEY); |
| 147 | + if (headers == null || headerGetter.get() == null) { |
143 | 148 | return -1; |
144 | 149 | } |
145 | | - for (Map.Entry<String, Object> entry : headers.entrySet()) { |
146 | | - if (CONTENT_LENGTH_KEY.equalsIgnoreCase(entry.getKey())) { |
147 | | - return parseContentLength(entry.getValue()); |
148 | | - } |
149 | | - } |
150 | | - return -1; |
151 | | - } |
152 | | - |
153 | | - /** |
154 | | - * Safely parses the content length Object representation into a long integer. |
155 | | - * |
156 | | - * @param value the header value to parse. |
157 | | - * @return the parsed content length value, or -1 if it was null or failed to parse. |
158 | | - */ |
159 | | - private long parseContentLength(Object value) { |
160 | | - if (value == null) { |
161 | | - return -1; |
162 | | - } |
163 | | - return Long.parseLong(String.valueOf(value)); |
| 150 | + return Long.parseLong(String.valueOf(headerGetter.get())); |
164 | 151 | } |
165 | 152 |
|
166 | 153 | private void endAttempt() { |
|
0 commit comments