|
59 | 59 | import concurrent.futures |
60 | 60 | import logging |
61 | 61 | import os |
| 62 | +import platform |
62 | 63 | import sys |
63 | 64 | import typing |
64 | 65 | from json import dumps |
|
119 | 120 | KUBERNETES_JOB_NAME = ResourceAttributes.K8S_JOB_NAME |
120 | 121 | KUBERNETES_CRON_JOB_UID = ResourceAttributes.K8S_CRONJOB_UID |
121 | 122 | KUBERNETES_CRON_JOB_NAME = ResourceAttributes.K8S_CRONJOB_NAME |
122 | | -OS_TYPE = ResourceAttributes.OS_TYPE |
123 | 123 | OS_DESCRIPTION = ResourceAttributes.OS_DESCRIPTION |
| 124 | +OS_TYPE = ResourceAttributes.OS_TYPE |
| 125 | +OS_VERSION = ResourceAttributes.OS_VERSION |
124 | 126 | PROCESS_PID = ResourceAttributes.PROCESS_PID |
125 | 127 | PROCESS_PARENT_PID = ResourceAttributes.PROCESS_PARENT_PID |
126 | 128 | PROCESS_EXECUTABLE_NAME = ResourceAttributes.PROCESS_EXECUTABLE_NAME |
@@ -371,6 +373,72 @@ def detect(self) -> "Resource": |
371 | 373 | return Resource(resource_info) |
372 | 374 |
|
373 | 375 |
|
| 376 | +class OsResourceDetector(ResourceDetector): |
| 377 | + """Detect os resources based on `Operating System conventions <https://opentelemetry.io/docs/specs/semconv/resource/os/`_.""" |
| 378 | + |
| 379 | + def detect(self) -> "Resource": |
| 380 | + """Returns a resource with with `os.type` and `os.version`. Example of |
| 381 | + return values for `platform` calls in different systems: |
| 382 | +
|
| 383 | + Linux: |
| 384 | + >>> platform.system() |
| 385 | + 'Linux' |
| 386 | + >>> platform.release() |
| 387 | + '6.5.0-35-generic' |
| 388 | + >>> platform.version() |
| 389 | + '#35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2' |
| 390 | +
|
| 391 | + MacOS: |
| 392 | + >>> platform.system() |
| 393 | + 'Darwin' |
| 394 | + >>> platform.release() |
| 395 | + '23.0.0' |
| 396 | + >>> platform.version() |
| 397 | + 'Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112' |
| 398 | +
|
| 399 | + Windows: |
| 400 | + >>> platform.system() |
| 401 | + 'Windows' |
| 402 | + >>> platform.release() |
| 403 | + '2022Server' |
| 404 | + >>> platform.version() |
| 405 | + '10.0.20348' |
| 406 | +
|
| 407 | + FreeBSD: |
| 408 | + >>> platform.system() |
| 409 | + 'FreeBSD' |
| 410 | + >>> platform.release() |
| 411 | + '14.1-RELEASE' |
| 412 | + >>> platform.version() |
| 413 | + 'FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC' |
| 414 | +
|
| 415 | + Solaris: |
| 416 | + >>> platform.system() |
| 417 | + 'SunOS' |
| 418 | + >>> platform.release() |
| 419 | + '5.11' |
| 420 | + >>> platform.version() |
| 421 | + '11.4.0.15.0' |
| 422 | + """ |
| 423 | + |
| 424 | + os_type = platform.system().lower() |
| 425 | + os_version = platform.release() |
| 426 | + |
| 427 | + # See docstring |
| 428 | + if os_type == "windows": |
| 429 | + os_version = platform.version() |
| 430 | + # Align SunOS with conventions |
| 431 | + if os_type == "sunos": |
| 432 | + os_type = "solaris" |
| 433 | + |
| 434 | + return Resource( |
| 435 | + { |
| 436 | + OS_TYPE: os_type, |
| 437 | + OS_VERSION: os_version, |
| 438 | + } |
| 439 | + ) |
| 440 | + |
| 441 | + |
374 | 442 | def get_aggregated_resources( |
375 | 443 | detectors: typing.List["ResourceDetector"], |
376 | 444 | initial_resource: typing.Optional[Resource] = None, |
|
0 commit comments