aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/exporter/_utils.py
blob: 588fe6e3cf7495c80adcb71c8fea0521da5db4bb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import datetime
import locale
from os import environ
from os.path import isdir
import platform
import threading
import time
import warnings
from typing import Callable, Dict, Any

from opentelemetry.semconv.attributes.service_attributes import SERVICE_NAME
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.util import ns_to_iso_str
from opentelemetry.util.types import Attributes

from azure.core.pipeline.policies import BearerTokenCredentialPolicy
from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys, TelemetryItem
from azure.monitor.opentelemetry.exporter._version import VERSION as ext_version
from azure.monitor.opentelemetry.exporter._constants import (
    _AKS_ARM_NAMESPACE_ID,
    _DEFAULT_AAD_SCOPE,
    _INSTRUMENTATIONS_BIT_MAP,
    _FUNCTIONS_WORKER_RUNTIME,
    _PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
    _WEBSITE_SITE_NAME,
)


opentelemetry_version = ""

# Workaround for missing version file
try:
    from importlib.metadata import version

    opentelemetry_version = version("opentelemetry-sdk")
except ImportError:
    # Temporary workaround for <Py3.8
    # importlib-metadata causing issues in CI
    import pkg_resources  # type: ignore

    opentelemetry_version = pkg_resources.get_distribution("opentelemetry-sdk").version


# Azure App Service


def _is_on_app_service():
    return environ.get(_WEBSITE_SITE_NAME) is not None


# Functions


def _is_on_functions():
    return environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None


# AKS


def _is_on_aks():
    return _AKS_ARM_NAMESPACE_ID in environ


# Attach


def _is_attach_enabled():
    if _is_on_app_service():
        return isdir("/agents/python/")
    if _is_on_functions():
        return environ.get(_PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY) == "true"
    return False


def _get_sdk_version_prefix():
    sdk_version_prefix = ""
    rp = "u"
    if _is_on_functions():
        rp = "f"
    elif _is_on_app_service():
        rp = "a"
    # TODO: Add VM scenario outside statsbeat
    # elif _is_on_vm():
    #     rp = 'v'
    elif _is_on_aks():
        rp = "k"

    os = "u"
    system = platform.system()
    if system == "Linux":
        os = "l"
    elif system == "Windows":
        os = "w"

    attach_type = "m"
    if _is_attach_enabled():
        attach_type = "i"
    sdk_version_prefix = "{}{}{}_".format(rp, os, attach_type)

    return sdk_version_prefix


def _get_sdk_version():
    return "{}py{}:otel{}:ext{}".format(
        _get_sdk_version_prefix(), platform.python_version(), opentelemetry_version, ext_version
    )


def _getlocale():
    try:
        with warnings.catch_warnings():
            # temporary work-around for https://github.com/python/cpython/issues/82986
            # by continuing to use getdefaultlocale() even though it has been deprecated.
            # we ignore the deprecation warnings to reduce noise
            warnings.simplefilter("ignore", category=DeprecationWarning)
            return locale.getdefaultlocale()[0]
    except AttributeError:
        # locale.getlocal() has issues on Windows: https://github.com/python/cpython/issues/82986
        # Use this as a fallback if locale.getdefaultlocale() doesn't exist (>Py3.13)
        return locale.getlocale()[0]


azure_monitor_context = {
    ContextTagKeys.AI_DEVICE_ID: platform.node(),
    ContextTagKeys.AI_DEVICE_LOCALE: _getlocale(),
    ContextTagKeys.AI_DEVICE_OS_VERSION: platform.version(),
    ContextTagKeys.AI_DEVICE_TYPE: "Other",
    ContextTagKeys.AI_INTERNAL_SDK_VERSION: _get_sdk_version(),
}


def ns_to_duration(nanoseconds: int) -> str:
    value = (nanoseconds + 500000) // 1000000  # duration in milliseconds
    value, milliseconds = divmod(value, 1000)
    value, seconds = divmod(value, 60)
    value, minutes = divmod(value, 60)
    days, hours = divmod(value, 24)
    return "{:d}.{:02d}:{:02d}:{:02d}.{:03d}".format(days, hours, minutes, seconds, milliseconds)


# Replicate .netDateTime.Ticks(), which is the UTC time, expressed as the number
# of 100-nanosecond intervals that have elapsed since 12:00:00 midnight on
# January 1, 0001.
def _ticks_since_dot_net_epoch():
    # Since time.time() is the elapsed time since UTC January 1, 1970, we have
    # to shift this start time, and  then multiply by 10^7 to get the number of
    # 100-nanosecond intervals
    shift_time = int((datetime.datetime(1970, 1, 1, 0, 0, 0) - datetime.datetime(1, 1, 1, 0, 0, 0)).total_seconds()) * (
        10**7
    )
    # Add shift time to 100-ns intervals since time.time()
    return int(time.time() * (10**7)) + shift_time


_INSTRUMENTATIONS_BIT_MASK = 0
_INSTRUMENTATIONS_BIT_MASK_LOCK = threading.Lock()


def get_instrumentations():
    return _INSTRUMENTATIONS_BIT_MASK


def add_instrumentation(instrumentation_name: str):
    with _INSTRUMENTATIONS_BIT_MASK_LOCK:
        global _INSTRUMENTATIONS_BIT_MASK  # pylint: disable=global-statement
        instrumentation_bits = _INSTRUMENTATIONS_BIT_MAP.get(instrumentation_name, 0)
        _INSTRUMENTATIONS_BIT_MASK |= instrumentation_bits


def remove_instrumentation(instrumentation_name: str):
    with _INSTRUMENTATIONS_BIT_MASK_LOCK:
        global _INSTRUMENTATIONS_BIT_MASK  # pylint: disable=global-statement
        instrumentation_bits = _INSTRUMENTATIONS_BIT_MAP.get(instrumentation_name, 0)
        _INSTRUMENTATIONS_BIT_MASK &= ~instrumentation_bits


class PeriodicTask(threading.Thread):
    """Thread that periodically calls a given function.

    :type interval: int or float
    :param interval: Seconds between calls to the function.

    :type function: function
    :param function: The function to call.

    :type args: list
    :param args: The args passed in while calling `function`.

    :type kwargs: dict
    :param args: The kwargs passed in while calling `function`.
    """

    def __init__(self, interval: int, function: Callable, *args: Any, **kwargs: Any):
        super().__init__(name=kwargs.pop("name", None))
        self.interval = interval
        self.function = function
        self.args = args or []  # type: ignore
        self.kwargs = kwargs or {}
        self.finished = threading.Event()

    def run(self):
        wait_time = self.interval
        while not self.finished.wait(wait_time):
            start_time = time.time()
            self.function(*self.args, **self.kwargs)
            elapsed_time = time.time() - start_time
            wait_time = max(self.interval - elapsed_time, 0)

    def cancel(self):
        self.finished.set()


def _create_telemetry_item(timestamp: int) -> TelemetryItem:
    return TelemetryItem(
        name="",
        instrumentation_key="",
        tags=dict(azure_monitor_context),  # type: ignore
        time=ns_to_iso_str(timestamp),  # type: ignore
    )


def _populate_part_a_fields(resource: Resource):
    tags = {}
    if resource and resource.attributes:
        service_name = resource.attributes.get(SERVICE_NAME)
        service_namespace = resource.attributes.get(ResourceAttributes.SERVICE_NAMESPACE)
        service_instance_id = resource.attributes.get(ResourceAttributes.SERVICE_INSTANCE_ID)
        device_id = resource.attributes.get(ResourceAttributes.DEVICE_ID)
        device_model = resource.attributes.get(ResourceAttributes.DEVICE_MODEL_NAME)
        device_make = resource.attributes.get(ResourceAttributes.DEVICE_MANUFACTURER)
        app_version = resource.attributes.get(ResourceAttributes.SERVICE_VERSION)
        if service_name:
            if service_namespace:
                tags[ContextTagKeys.AI_CLOUD_ROLE] = str(service_namespace) + "." + str(service_name)
            else:
                tags[ContextTagKeys.AI_CLOUD_ROLE] = service_name  # type: ignore
        if service_instance_id:
            tags[ContextTagKeys.AI_CLOUD_ROLE_INSTANCE] = service_instance_id  # type: ignore
        else:
            tags[ContextTagKeys.AI_CLOUD_ROLE_INSTANCE] = platform.node()  # hostname default
        tags[ContextTagKeys.AI_INTERNAL_NODE_NAME] = tags[ContextTagKeys.AI_CLOUD_ROLE_INSTANCE]
        if device_id:
            tags[ContextTagKeys.AI_DEVICE_ID] = device_id  # type: ignore
        if device_model:
            tags[ContextTagKeys.AI_DEVICE_MODEL] = device_model  # type: ignore
        if device_make:
            tags[ContextTagKeys.AI_DEVICE_OEM_NAME] = device_make  # type: ignore
        if app_version:
            tags[ContextTagKeys.AI_APPLICATION_VER] = app_version  # type: ignore

    return tags


# pylint: disable=W0622
def _filter_custom_properties(properties: Attributes, filter=None) -> Dict[str, str]:
    truncated_properties: Dict[str, str] = {}
    if not properties:
        return truncated_properties
    for key, val in properties.items():
        # Apply filter function
        if filter is not None:
            if not filter(key, val):
                continue
        # Apply truncation rules
        # Max key length is 150, value is 8192
        if not key or len(key) > 150 or val is None:
            continue
        truncated_properties[key] = str(val)[:8192]
    return truncated_properties


def _get_auth_policy(credential, default_auth_policy, aad_audience=None):
    if credential:
        if hasattr(credential, "get_token"):
            return BearerTokenCredentialPolicy(
                credential,
                _get_scope(aad_audience),
            )
        raise ValueError("Must pass in valid TokenCredential.")
    return default_auth_policy


def _get_scope(aad_audience=None):
    # The AUDIENCE is a url that identifies Azure Monitor in a specific cloud
    # (For example: "https://monitor.azure.com/").
    # The SCOPE is the audience + the permission
    # (For example: "https://monitor.azure.com//.default").
    return _DEFAULT_AAD_SCOPE if not aad_audience else "{}/.default".format(aad_audience)


class Singleton(type):
    _instance = None

    def __call__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instance