1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from __future__ import annotations
16+
1517import os
1618import threading
1719from enum import Enum
20+ from typing import Container , Mapping , MutableMapping
1821
1922from opentelemetry .instrumentation .utils import http_status_to_status_code
2023from opentelemetry .semconv ._incubating .attributes .http_attributes import (
6366 USER_AGENT_ORIGINAL ,
6467)
6568from opentelemetry .semconv .schemas import Schemas
69+ from opentelemetry .trace import Span
6670from opentelemetry .trace .status import Status , StatusCode
71+ from opentelemetry .util .types import AttributeValue
6772
6873# Values defined in milliseconds
6974HTTP_DURATION_HISTOGRAM_BUCKETS_OLD = (
@@ -259,11 +264,11 @@ def _get_opentelemetry_stability_opt_in_mode(
259264
260265
261266def _filter_semconv_duration_attrs (
262- attrs ,
263- old_attrs ,
264- new_attrs ,
265- sem_conv_opt_in_mode = _StabilityMode .DEFAULT ,
266- ):
267+ attrs : Mapping [ str , AttributeValue ] ,
268+ old_attrs : Container [ AttributeValue ] ,
269+ new_attrs : Container [ AttributeValue ] ,
270+ sem_conv_opt_in_mode : _StabilityMode = _StabilityMode .DEFAULT ,
271+ ) -> dict [ str , AttributeValue ] :
267272 filtered_attrs = {}
268273 # duration is two different metrics depending on sem_conv_opt_in_mode, so no DUP attributes
269274 allowed_attributes = (
@@ -276,11 +281,11 @@ def _filter_semconv_duration_attrs(
276281
277282
278283def _filter_semconv_active_request_count_attr (
279- attrs ,
280- old_attrs ,
281- new_attrs ,
282- sem_conv_opt_in_mode = _StabilityMode .DEFAULT ,
283- ):
284+ attrs : Mapping [ str , AttributeValue ] ,
285+ old_attrs : Container [ AttributeValue ] ,
286+ new_attrs : Container [ AttributeValue ] ,
287+ sem_conv_opt_in_mode : _StabilityMode = _StabilityMode .DEFAULT ,
288+ ) -> dict [ str , AttributeValue ] :
284289 filtered_attrs = {}
285290 if _report_old (sem_conv_opt_in_mode ):
286291 for key , val in attrs .items ():
@@ -293,20 +298,33 @@ def _filter_semconv_active_request_count_attr(
293298 return filtered_attrs
294299
295300
296- def set_string_attribute (result , key , value ):
301+ def set_string_attribute (
302+ result : MutableMapping [str , AttributeValue ],
303+ key : str ,
304+ value : AttributeValue ,
305+ ) -> None :
297306 if value :
298307 result [key ] = value
299308
300309
301- def set_int_attribute (result , key , value ):
310+ def set_int_attribute (
311+ result : MutableMapping [str , AttributeValue ],
312+ key : str ,
313+ value : AttributeValue ,
314+ ) -> None :
302315 if value :
303316 try :
304317 result [key ] = int (value )
305318 except ValueError :
306319 return
307320
308321
309- def _set_http_method (result , original , normalized , sem_conv_opt_in_mode ):
322+ def _set_http_method (
323+ result : MutableMapping [str , AttributeValue ],
324+ original : str ,
325+ normalized : str ,
326+ sem_conv_opt_in_mode : _StabilityMode ,
327+ ) -> None :
310328 original = original .strip ()
311329 normalized = normalized .strip ()
312330 # See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#common-attributes
@@ -320,35 +338,55 @@ def _set_http_method(result, original, normalized, sem_conv_opt_in_mode):
320338 set_string_attribute (result , HTTP_REQUEST_METHOD , normalized )
321339
322340
323- def _set_http_status_code (result , code , sem_conv_opt_in_mode ):
341+ def _set_http_status_code (
342+ result : MutableMapping [str , AttributeValue ],
343+ code : str | int ,
344+ sem_conv_opt_in_mode : _StabilityMode ,
345+ ) -> None :
324346 if _report_old (sem_conv_opt_in_mode ):
325347 set_int_attribute (result , HTTP_STATUS_CODE , code )
326348 if _report_new (sem_conv_opt_in_mode ):
327349 set_int_attribute (result , HTTP_RESPONSE_STATUS_CODE , code )
328350
329351
330- def _set_http_url (result , url , sem_conv_opt_in_mode ):
352+ def _set_http_url (
353+ result : MutableMapping [str , AttributeValue ],
354+ url : str ,
355+ sem_conv_opt_in_mode : _StabilityMode ,
356+ ) -> None :
331357 if _report_old (sem_conv_opt_in_mode ):
332358 set_string_attribute (result , HTTP_URL , url )
333359 if _report_new (sem_conv_opt_in_mode ):
334360 set_string_attribute (result , URL_FULL , url )
335361
336362
337- def _set_http_scheme (result , scheme , sem_conv_opt_in_mode ):
363+ def _set_http_scheme (
364+ result : MutableMapping [str , AttributeValue ],
365+ scheme : str ,
366+ sem_conv_opt_in_mode : _StabilityMode ,
367+ ) -> None :
338368 if _report_old (sem_conv_opt_in_mode ):
339369 set_string_attribute (result , HTTP_SCHEME , scheme )
340370 if _report_new (sem_conv_opt_in_mode ):
341371 set_string_attribute (result , URL_SCHEME , scheme )
342372
343373
344- def _set_http_flavor_version (result , version , sem_conv_opt_in_mode ):
374+ def _set_http_flavor_version (
375+ result : MutableMapping [str , AttributeValue ],
376+ version : str ,
377+ sem_conv_opt_in_mode : _StabilityMode ,
378+ ) -> None :
345379 if _report_old (sem_conv_opt_in_mode ):
346380 set_string_attribute (result , HTTP_FLAVOR , version )
347381 if _report_new (sem_conv_opt_in_mode ):
348382 set_string_attribute (result , NETWORK_PROTOCOL_VERSION , version )
349383
350384
351- def _set_http_user_agent (result , user_agent , sem_conv_opt_in_mode ):
385+ def _set_http_user_agent (
386+ result : MutableMapping [str , AttributeValue ],
387+ user_agent : str ,
388+ sem_conv_opt_in_mode : _StabilityMode ,
389+ ) -> None :
352390 if _report_old (sem_conv_opt_in_mode ):
353391 set_string_attribute (result , HTTP_USER_AGENT , user_agent )
354392 if _report_new (sem_conv_opt_in_mode ):
@@ -358,28 +396,44 @@ def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
358396# Client
359397
360398
361- def _set_http_host_client (result , host , sem_conv_opt_in_mode ):
399+ def _set_http_host_client (
400+ result : MutableMapping [str , AttributeValue ],
401+ host : str ,
402+ sem_conv_opt_in_mode : _StabilityMode ,
403+ ) -> None :
362404 if _report_old (sem_conv_opt_in_mode ):
363405 set_string_attribute (result , HTTP_HOST , host )
364406 if _report_new (sem_conv_opt_in_mode ):
365407 set_string_attribute (result , SERVER_ADDRESS , host )
366408
367409
368- def _set_http_net_peer_name_client (result , peer_name , sem_conv_opt_in_mode ):
410+ def _set_http_net_peer_name_client (
411+ result : MutableMapping [str , AttributeValue ],
412+ peer_name : str ,
413+ sem_conv_opt_in_mode : _StabilityMode ,
414+ ) -> None :
369415 if _report_old (sem_conv_opt_in_mode ):
370416 set_string_attribute (result , NET_PEER_NAME , peer_name )
371417 if _report_new (sem_conv_opt_in_mode ):
372418 set_string_attribute (result , SERVER_ADDRESS , peer_name )
373419
374420
375- def _set_http_peer_port_client (result , port , sem_conv_opt_in_mode ):
421+ def _set_http_peer_port_client (
422+ result : MutableMapping [str , AttributeValue ],
423+ port : str | int ,
424+ sem_conv_opt_in_mode : _StabilityMode ,
425+ ) -> None :
376426 if _report_old (sem_conv_opt_in_mode ):
377427 set_int_attribute (result , NET_PEER_PORT , port )
378428 if _report_new (sem_conv_opt_in_mode ):
379429 set_int_attribute (result , SERVER_PORT , port )
380430
381431
382- def _set_http_network_protocol_version (result , version , sem_conv_opt_in_mode ):
432+ def _set_http_network_protocol_version (
433+ result : MutableMapping [str , AttributeValue ],
434+ version : str ,
435+ sem_conv_opt_in_mode : _StabilityMode ,
436+ ) -> None :
383437 if _report_old (sem_conv_opt_in_mode ):
384438 set_string_attribute (result , HTTP_FLAVOR , version )
385439 if _report_new (sem_conv_opt_in_mode ):
@@ -389,21 +443,35 @@ def _set_http_network_protocol_version(result, version, sem_conv_opt_in_mode):
389443# Server
390444
391445
392- def _set_http_net_host (result , host , sem_conv_opt_in_mode ):
446+ def _set_http_net_host (
447+ result : MutableMapping [str , AttributeValue ],
448+ host : str ,
449+ sem_conv_opt_in_mode : _StabilityMode ,
450+ ) -> None :
393451 if _report_old (sem_conv_opt_in_mode ):
394452 set_string_attribute (result , NET_HOST_NAME , host )
395453 if _report_new (sem_conv_opt_in_mode ):
396454 set_string_attribute (result , SERVER_ADDRESS , host )
397455
398456
399- def _set_http_net_host_port (result , port , sem_conv_opt_in_mode ):
457+ def _set_http_net_host_port (
458+ result : MutableMapping [str , AttributeValue ],
459+ port : str | int ,
460+ sem_conv_opt_in_mode : _StabilityMode ,
461+ ) -> None :
400462 if _report_old (sem_conv_opt_in_mode ):
401463 set_int_attribute (result , NET_HOST_PORT , port )
402464 if _report_new (sem_conv_opt_in_mode ):
403465 set_int_attribute (result , SERVER_PORT , port )
404466
405467
406- def _set_http_target (result , target , path , query , sem_conv_opt_in_mode ):
468+ def _set_http_target (
469+ result : MutableMapping [str , AttributeValue ],
470+ target : str ,
471+ path : str | None ,
472+ query : str | None ,
473+ sem_conv_opt_in_mode : _StabilityMode ,
474+ ) -> None :
407475 if _report_old (sem_conv_opt_in_mode ):
408476 set_string_attribute (result , HTTP_TARGET , target )
409477 if _report_new (sem_conv_opt_in_mode ):
@@ -413,7 +481,11 @@ def _set_http_target(result, target, path, query, sem_conv_opt_in_mode):
413481 set_string_attribute (result , URL_QUERY , query )
414482
415483
416- def _set_http_host_server (result , host , sem_conv_opt_in_mode ):
484+ def _set_http_host_server (
485+ result : MutableMapping [str , AttributeValue ],
486+ host : str ,
487+ sem_conv_opt_in_mode : _StabilityMode ,
488+ ) -> None :
417489 if _report_old (sem_conv_opt_in_mode ):
418490 set_string_attribute (result , HTTP_HOST , host )
419491 if _report_new (sem_conv_opt_in_mode ):
@@ -426,7 +498,11 @@ def _set_http_host_server(result, host, sem_conv_opt_in_mode):
426498# net.sock.peer.addr -> client.socket.address for server spans (TODO) AND client.address if missing
427499# https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/CHANGELOG.md#v1210-2023-07-13
428500# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/http-migration.md#common-attributes-across-http-client-and-server-spans
429- def _set_http_peer_ip_server (result , ip , sem_conv_opt_in_mode ):
501+ def _set_http_peer_ip_server (
502+ result : MutableMapping [str , AttributeValue ],
503+ ip : str ,
504+ sem_conv_opt_in_mode : _StabilityMode ,
505+ ) -> None :
430506 if _report_old (sem_conv_opt_in_mode ):
431507 set_string_attribute (result , NET_PEER_IP , ip )
432508 if _report_new (sem_conv_opt_in_mode ):
@@ -435,28 +511,36 @@ def _set_http_peer_ip_server(result, ip, sem_conv_opt_in_mode):
435511 set_string_attribute (result , CLIENT_ADDRESS , ip )
436512
437513
438- def _set_http_peer_port_server (result , port , sem_conv_opt_in_mode ):
514+ def _set_http_peer_port_server (
515+ result : MutableMapping [str , AttributeValue ],
516+ port : str | int ,
517+ sem_conv_opt_in_mode : _StabilityMode ,
518+ ) -> None :
439519 if _report_old (sem_conv_opt_in_mode ):
440520 set_int_attribute (result , NET_PEER_PORT , port )
441521 if _report_new (sem_conv_opt_in_mode ):
442522 set_int_attribute (result , CLIENT_PORT , port )
443523
444524
445- def _set_http_net_peer_name_server (result , name , sem_conv_opt_in_mode ):
525+ def _set_http_net_peer_name_server (
526+ result : MutableMapping [str , AttributeValue ],
527+ name : str ,
528+ sem_conv_opt_in_mode : _StabilityMode ,
529+ ) -> None :
446530 if _report_old (sem_conv_opt_in_mode ):
447531 set_string_attribute (result , NET_PEER_NAME , name )
448532 if _report_new (sem_conv_opt_in_mode ):
449533 set_string_attribute (result , CLIENT_ADDRESS , name )
450534
451535
452536def _set_status (
453- span ,
454- metrics_attributes : dict ,
537+ span : Span ,
538+ metrics_attributes : MutableMapping [ str , AttributeValue ] ,
455539 status_code : int ,
456540 status_code_str : str ,
457541 server_span : bool = True ,
458542 sem_conv_opt_in_mode : _StabilityMode = _StabilityMode .DEFAULT ,
459- ):
543+ ) -> None :
460544 if status_code < 0 :
461545 if _report_new (sem_conv_opt_in_mode ):
462546 metrics_attributes [ERROR_TYPE ] = status_code_str
0 commit comments