| 1 |
from typing import Callable, Optional, TypeVar |
| 2 |
|
| 3 |
from azure.core.settings import settings |
| 4 |
from azure.core.tracing.decorator import distributed_trace |
| 5 |
from azure.core.tracing.decorator_async import distributed_trace_async |
| 6 |
from azure.core.tracing import SpanKind |
| 7 |
|
| 8 |
from .client_request_properties import ClientRequestProperties |
| 9 |
|
| 10 |
|
| 11 |
class Span: |
| 12 |
""" |
| 13 |
Additional ADX attributes for telemetry spans |
| 14 |
""" |
| 15 |
|
| 16 |
_KUSTO_CLUSTER = "kusto_cluster" |
| 17 |
_DATABASE = "database" |
| 18 |
_TABLE = "table" |
| 19 |
|
| 20 |
_AUTH_METHOD = "authentication_method" |
| 21 |
_CLIENT_ACTIVITY_ID = "client_activity_id" |
| 22 |
|
| 23 |
_SPAN_COMPONENT = "component" |
| 24 |
_HTTP = "http" |
| 25 |
_HTTP_USER_AGENT = "http.user_agent" |
| 26 |
_HTTP_METHOD = "http.method" |
| 27 |
_HTTP_URL = "http.url" |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def add_attributes(cls, **kwargs) -> None: |
| 31 |
""" |
| 32 |
Add ADX attributes to the current span |
| 33 |
:key dict tracing_attributes: key, val ADX attributes to include in span of trace |
| 34 |
""" |
| 35 |
tracing_attributes: dict = kwargs.pop("tracing_attributes", {}) |
| 36 |
span_impl_type = settings.tracing_implementation() |
| 37 |
if span_impl_type is None: |
| 38 |
return |
| 39 |
current_span = span_impl_type.get_current_span() |
| 40 |
span = span_impl_type(span=current_span) |
| 41 |
for key, val in tracing_attributes.items(): |
| 42 |
span.add_attribute(key, val) |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def set_query_attributes(cls, cluster: str, database: str, properties: Optional[ClientRequestProperties] = None) -> None: |
| 46 |
query_attributes: dict = cls.create_query_attributes(cluster, database, properties) |
| 47 |
cls.add_attributes(tracing_attributes=query_attributes) |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def set_streaming_ingest_attributes(cls, cluster: str, database: str, table: str, properties: Optional[ClientRequestProperties] = None) -> None: |
| 51 |
ingest_attributes: dict = cls.create_streaming_ingest_attributes(cluster, database, table, properties) |
| 52 |
cls.add_attributes(tracing_attributes=ingest_attributes) |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def set_cloud_info_attributes(cls, url: str) -> None: |
| 56 |
cloud_info_attributes: dict = cls.create_cloud_info_attributes(url) |
| 57 |
cls.add_attributes(tracing_attributes=cloud_info_attributes) |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def create_query_attributes(cls, cluster: str, database: str, properties: Optional[ClientRequestProperties] = None) -> dict: |
| 61 |
query_attributes: dict = {cls._KUSTO_CLUSTER: cluster, cls._DATABASE: database} |
| 62 |
if properties: |
| 63 |
query_attributes.update(properties.get_tracing_attributes()) |
| 64 |
|
| 65 |
return query_attributes |
| 66 |
|
| 67 |
@classmethod |
| 68 |
def create_streaming_ingest_attributes(cls, cluster: str, database: str, table: str, properties: Optional[ClientRequestProperties] = None) -> dict: |
| 69 |
ingest_attributes: dict = {cls._KUSTO_CLUSTER: cluster, cls._DATABASE: database, cls._TABLE: table} |
| 70 |
if properties: |
| 71 |
ingest_attributes.update(properties.get_tracing_attributes()) |
| 72 |
|
| 73 |
return ingest_attributes |
| 74 |
|
| 75 |
@classmethod |
| 76 |
def create_http_attributes(cls, method: str, url: str, headers: dict = None) -> dict: |
| 77 |
if headers is None: |
| 78 |
headers = {} |
| 79 |
http_tracing_attributes: dict = { |
| 80 |
cls._SPAN_COMPONENT: cls._HTTP, |
| 81 |
cls._HTTP_METHOD: method, |
| 82 |
cls._HTTP_URL: url, |
| 83 |
} |
| 84 |
user_agent = headers.get("User-Agent") |
| 85 |
if user_agent: |
| 86 |
http_tracing_attributes[cls._HTTP_USER_AGENT] = user_agent |
| 87 |
return http_tracing_attributes |
| 88 |
|
| 89 |
@classmethod |
| 90 |
def create_cloud_info_attributes(cls, url: str) -> dict: |
| 91 |
ingest_attributes: dict = {cls._HTTP_URL: url} |
| 92 |
return ingest_attributes |
| 93 |
|
| 94 |
@classmethod |
| 95 |
def create_cluster_attributes(cls, cluster_uri: str) -> dict: |
| 96 |
cluster_attributes = {cls._KUSTO_CLUSTER: cluster_uri} |
| 97 |
return cluster_attributes |
| 98 |
|
| 99 |
|
| 100 |
class MonitoredActivity: |
| 101 |
""" |
| 102 |
Invoker class for telemetry |
| 103 |
""" |
| 104 |
|
| 105 |
T = TypeVar("T") |
| 106 |
|
| 107 |
@staticmethod |
| 108 |
def invoke(invoker: Callable[[], T], name_of_span: str = None, tracing_attributes=None, kind: str = SpanKind.INTERNAL) -> T: |
| 109 |
""" |
| 110 |
Runs the span on given function |
| 111 |
""" |
| 112 |
if tracing_attributes is None: |
| 113 |
tracing_attributes = {} |
| 114 |
span_shell: Callable = distributed_trace(name_of_span=name_of_span, tracing_attributes=tracing_attributes, kind=kind) |
| 115 |
span = span_shell(invoker) |
| 116 |
return span() |
| 117 |
|
| 118 |
@staticmethod |
| 119 |
async def invoke_async(invoker: Callable[[], T], name_of_span: str = None, tracing_attributes=None, kind: str = SpanKind.INTERNAL) -> T: |
| 120 |
""" |
| 121 |
Runs a span on given function |
| 122 |
""" |
| 123 |
if tracing_attributes is None: |
| 124 |
tracing_attributes = {} |
| 125 |
span_shell: Callable = distributed_trace_async(name_of_span=name_of_span, tracing_attributes=tracing_attributes, kind=kind) |
| 126 |
span = span_shell(invoker) |
| 127 |
return await span() |