@@ -200,8 +200,10 @@ def client_resposne_hook(span, future):
200
200
_HANDLER_CONTEXT_KEY = "_otel_trace_context_key"
201
201
_OTEL_PATCHED_KEY = "_otel_patched_key"
202
202
203
-
204
203
_START_TIME = "start_time"
204
+ _CLIENT_DURATION_HISTOGRAM = "http.client.duration"
205
+ _CLIENT_REQUEST_SIZE_HISTOGRAM = "http.client.request.size"
206
+ _CLIENT_RESPONSE_SIZE_HISTOGRAM = "http.client.response.size"
205
207
_SERVER_DURATION_HISTOGRAM = "http.server.duration"
206
208
_SERVER_REQUEST_SIZE_HISTOGRAM = "http.server.request.size"
207
209
_SERVER_RESPONSE_SIZE_HISTOGRAM = "http.server.response.size"
@@ -245,24 +247,9 @@ def _instrument(self, **kwargs):
245
247
meter_provider = kwargs .get ("meter_provider" )
246
248
meter = get_meter (__name__ , __version__ , meter_provider )
247
249
250
+ client_histograms = _create_client_histograms (meter )
248
251
server_histograms = _create_server_histograms (meter )
249
252
250
- client_duration_histogram = meter .create_histogram (
251
- name = "http.client.duration" ,
252
- unit = "ms" ,
253
- description = "measures the duration outbound HTTP requests" ,
254
- )
255
- client_request_size_histogram = meter .create_histogram (
256
- name = "http.client.request.size" ,
257
- unit = "By" ,
258
- description = "measures the size of HTTP request messages (compressed)" ,
259
- )
260
- client_response_size_histogram = meter .create_histogram (
261
- name = "http.client.response.size" ,
262
- unit = "By" ,
263
- description = "measures the size of HTTP response messages (compressed)" ,
264
- )
265
-
266
253
client_request_hook = kwargs .get ("client_request_hook" , None )
267
254
client_response_hook = kwargs .get ("client_response_hook" , None )
268
255
server_request_hook = kwargs .get ("server_request_hook" , None )
@@ -286,9 +273,9 @@ def handler_init(init, handler, args, kwargs):
286
273
tracer ,
287
274
client_request_hook ,
288
275
client_response_hook ,
289
- client_duration_histogram ,
290
- client_request_size_histogram ,
291
- client_response_size_histogram ,
276
+ client_histograms [ _CLIENT_DURATION_HISTOGRAM ] ,
277
+ client_histograms [ _CLIENT_REQUEST_SIZE_HISTOGRAM ] ,
278
+ client_histograms [ _CLIENT_RESPONSE_SIZE_HISTOGRAM ] ,
292
279
),
293
280
)
294
281
@@ -327,6 +314,28 @@ def _create_server_histograms(meter) -> Dict[str, Histogram]:
327
314
return histograms
328
315
329
316
317
+ def _create_client_histograms (meter ) -> Dict [str , Histogram ]:
318
+ histograms = {
319
+ _CLIENT_DURATION_HISTOGRAM : meter .create_histogram (
320
+ name = "http.client.duration" ,
321
+ unit = "ms" ,
322
+ description = "measures the duration outbound HTTP requests" ,
323
+ ),
324
+ _CLIENT_REQUEST_SIZE_HISTOGRAM : meter .create_histogram (
325
+ name = "http.client.request.size" ,
326
+ unit = "By" ,
327
+ description = "measures the size of HTTP request messages (compressed)" ,
328
+ ),
329
+ _CLIENT_RESPONSE_SIZE_HISTOGRAM : meter .create_histogram (
330
+ name = "http.client.response.size" ,
331
+ unit = "By" ,
332
+ description = "measures the size of HTTP response messages (compressed)" ,
333
+ ),
334
+ }
335
+
336
+ return histograms
337
+
338
+
330
339
def patch_handler_class (tracer , server_histograms , cls , request_hook = None ):
331
340
if getattr (cls , _OTEL_PATCHED_KEY , False ):
332
341
return False
@@ -550,7 +559,7 @@ def _finish_span(tracer, handler, error=None):
550
559
551
560
552
561
def _record_prepare_metrics (server_histograms , handler ):
553
- request_size = len (handler .request .body )
562
+ request_size = int (handler .request .headers . get ( "Content-Length" , 0 ) )
554
563
metric_attributes = _create_metric_attributes (handler )
555
564
556
565
server_histograms [_SERVER_REQUEST_SIZE_HISTOGRAM ].record (
0 commit comments