@@ -200,8 +200,10 @@ def client_resposne_hook(span, future):
200200_HANDLER_CONTEXT_KEY = "_otel_trace_context_key"
201201_OTEL_PATCHED_KEY = "_otel_patched_key"
202202
203-
204203_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"
205207_SERVER_DURATION_HISTOGRAM = "http.server.duration"
206208_SERVER_REQUEST_SIZE_HISTOGRAM = "http.server.request.size"
207209_SERVER_RESPONSE_SIZE_HISTOGRAM = "http.server.response.size"
@@ -245,24 +247,9 @@ def _instrument(self, **kwargs):
245247 meter_provider = kwargs .get ("meter_provider" )
246248 meter = get_meter (__name__ , __version__ , meter_provider )
247249
250+ client_histograms = _create_client_histograms (meter )
248251 server_histograms = _create_server_histograms (meter )
249252
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-
266253 client_request_hook = kwargs .get ("client_request_hook" , None )
267254 client_response_hook = kwargs .get ("client_response_hook" , None )
268255 server_request_hook = kwargs .get ("server_request_hook" , None )
@@ -286,9 +273,9 @@ def handler_init(init, handler, args, kwargs):
286273 tracer ,
287274 client_request_hook ,
288275 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 ] ,
292279 ),
293280 )
294281
@@ -327,6 +314,28 @@ def _create_server_histograms(meter) -> Dict[str, Histogram]:
327314 return histograms
328315
329316
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+
330339def patch_handler_class (tracer , server_histograms , cls , request_hook = None ):
331340 if getattr (cls , _OTEL_PATCHED_KEY , False ):
332341 return False
@@ -550,7 +559,7 @@ def _finish_span(tracer, handler, error=None):
550559
551560
552561def _record_prepare_metrics (server_histograms , handler ):
553- request_size = len (handler .request .body )
562+ request_size = int (handler .request .headers . get ( "Content-Length" , 0 ) )
554563 metric_attributes = _create_metric_attributes (handler )
555564
556565 server_histograms [_SERVER_REQUEST_SIZE_HISTOGRAM ].record (
0 commit comments