17
17
from enum import Enum
18
18
19
19
from opentelemetry .instrumentation .utils import http_status_to_status_code
20
+ from opentelemetry .semconv .attributes .client_attributes import (
21
+ CLIENT_ADDRESS ,
22
+ CLIENT_PORT ,
23
+ )
20
24
from opentelemetry .semconv .attributes .error_attributes import ERROR_TYPE
21
25
from opentelemetry .semconv .attributes .http_attributes import (
22
26
HTTP_REQUEST_METHOD ,
33
37
)
34
38
from opentelemetry .semconv .attributes .url_attributes import (
35
39
URL_FULL ,
40
+ URL_PATH ,
41
+ URL_QUERY ,
36
42
URL_SCHEME ,
37
43
)
44
+ from opentelemetry .semconv .attributes .user_agent_attributes import (
45
+ USER_AGENT_ORIGINAL ,
46
+ )
38
47
from opentelemetry .semconv .trace import SpanAttributes
39
48
from opentelemetry .trace .status import Status , StatusCode
40
49
50
+ # These lists represent attributes for metrics that are currently supported
51
+
41
52
_client_duration_attrs_old = [
42
53
SpanAttributes .HTTP_STATUS_CODE ,
43
54
SpanAttributes .HTTP_HOST ,
85
96
SpanAttributes .HTTP_SCHEME ,
86
97
SpanAttributes .HTTP_FLAVOR ,
87
98
SpanAttributes .HTTP_SERVER_NAME ,
88
- SpanAttributes .NET_HOST_NAME ,
89
- SpanAttributes .NET_HOST_PORT ,
90
99
]
91
100
92
101
_server_active_requests_count_attrs_new = [
93
102
HTTP_REQUEST_METHOD ,
94
103
URL_SCHEME ,
104
+ # TODO: Support SERVER_ADDRESS AND SERVER_PORT
95
105
]
96
106
97
107
OTEL_SEMCONV_STABILITY_OPT_IN = "OTEL_SEMCONV_STABILITY_OPT_IN"
@@ -280,38 +290,38 @@ def _set_http_net_host(result, host, sem_conv_opt_in_mode):
280
290
if _report_old (sem_conv_opt_in_mode ):
281
291
set_string_attribute (result , SpanAttributes .NET_HOST_NAME , host )
282
292
if _report_new (sem_conv_opt_in_mode ):
283
- set_string_attribute (result , SpanAttributes . SERVER_ADDRESS , host )
293
+ set_string_attribute (result , SERVER_ADDRESS , host )
284
294
285
295
286
296
def _set_http_net_host_port (result , port , sem_conv_opt_in_mode ):
287
297
if _report_old (sem_conv_opt_in_mode ):
288
298
set_int_attribute (result , SpanAttributes .NET_HOST_PORT , port )
289
299
if _report_new (sem_conv_opt_in_mode ):
290
- set_int_attribute (result , SpanAttributes . SERVER_PORT , port )
300
+ set_int_attribute (result , SERVER_PORT , port )
291
301
292
302
293
303
def _set_http_target (result , target , path , query , sem_conv_opt_in_mode ):
294
304
if _report_old (sem_conv_opt_in_mode ):
295
305
set_string_attribute (result , SpanAttributes .HTTP_TARGET , target )
296
306
if _report_new (sem_conv_opt_in_mode ):
297
307
if path :
298
- set_string_attribute (result , SpanAttributes . URL_PATH , path )
308
+ set_string_attribute (result , URL_PATH , path )
299
309
if query :
300
- set_string_attribute (result , SpanAttributes . URL_QUERY , query )
310
+ set_string_attribute (result , URL_QUERY , query )
301
311
302
312
303
313
def _set_http_peer_ip (result , ip , sem_conv_opt_in_mode ):
304
314
if _report_old (sem_conv_opt_in_mode ):
305
315
set_string_attribute (result , SpanAttributes .NET_PEER_IP , ip )
306
316
if _report_new (sem_conv_opt_in_mode ):
307
- set_string_attribute (result , SpanAttributes . CLIENT_ADDRESS , ip )
317
+ set_string_attribute (result , CLIENT_ADDRESS , ip )
308
318
309
319
310
320
def _set_http_peer_port_server (result , port , sem_conv_opt_in_mode ):
311
321
if _report_old (sem_conv_opt_in_mode ):
312
322
set_int_attribute (result , SpanAttributes .NET_PEER_PORT , port )
313
323
if _report_new (sem_conv_opt_in_mode ):
314
- set_int_attribute (result , SpanAttributes . CLIENT_PORT , port )
324
+ set_int_attribute (result , CLIENT_PORT , port )
315
325
316
326
317
327
def _set_http_user_agent (result , user_agent , sem_conv_opt_in_mode ):
@@ -320,32 +330,28 @@ def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
320
330
result , SpanAttributes .HTTP_USER_AGENT , user_agent
321
331
)
322
332
if _report_new (sem_conv_opt_in_mode ):
323
- set_string_attribute (
324
- result , SpanAttributes .USER_AGENT_ORIGINAL , user_agent
325
- )
333
+ set_string_attribute (result , USER_AGENT_ORIGINAL , user_agent )
326
334
327
335
328
336
def _set_http_net_peer_name_server (result , name , sem_conv_opt_in_mode ):
329
337
if _report_old (sem_conv_opt_in_mode ):
330
338
set_string_attribute (result , SpanAttributes .NET_PEER_NAME , name )
331
339
if _report_new (sem_conv_opt_in_mode ):
332
- set_string_attribute (result , SpanAttributes . CLIENT_ADDRESS , name )
340
+ set_string_attribute (result , CLIENT_ADDRESS , name )
333
341
334
342
335
343
def _set_http_flavor_version (result , version , sem_conv_opt_in_mode ):
336
344
if _report_old (sem_conv_opt_in_mode ):
337
345
set_string_attribute (result , SpanAttributes .HTTP_FLAVOR , version )
338
346
if _report_new (sem_conv_opt_in_mode ):
339
- set_string_attribute (
340
- result , SpanAttributes .NETWORK_PROTOCOL_VERSION , version
341
- )
347
+ set_string_attribute (result , NETWORK_PROTOCOL_VERSION , version )
342
348
343
349
344
350
def _set_status (
345
351
span ,
346
352
metrics_attributes ,
347
- status_code_str ,
348
353
status_code ,
354
+ status_code_str ,
349
355
sem_conv_opt_in_mode ,
350
356
):
351
357
if status_code < 0 :
@@ -366,12 +372,8 @@ def _set_status(
366
372
span .set_attribute (SpanAttributes .HTTP_STATUS_CODE , status_code )
367
373
metrics_attributes [SpanAttributes .HTTP_STATUS_CODE ] = status_code
368
374
if _report_new (sem_conv_opt_in_mode ):
369
- span .set_attribute (
370
- SpanAttributes .HTTP_RESPONSE_STATUS_CODE , status_code
371
- )
372
- metrics_attributes [SpanAttributes .HTTP_RESPONSE_STATUS_CODE ] = (
373
- status_code
374
- )
375
+ span .set_attribute (HTTP_RESPONSE_STATUS_CODE , status_code )
376
+ metrics_attributes [HTTP_RESPONSE_STATUS_CODE ] = status_code
375
377
if status == StatusCode .ERROR :
376
378
span .set_attribute (ERROR_TYPE , status_code_str )
377
379
metrics_attributes [ERROR_TYPE ] = status_code_str
0 commit comments