|
1 |
| -import asyncio |
2 |
| -import json |
3 |
| -import logging |
4 | 1 | import os
|
| 2 | +from opentelemetry import trace |
| 3 | +from opentelemetry.sdk.trace import TracerProvider |
| 4 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 5 | +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter |
| 6 | +from azure.monitor.opentelemetry import configure_azure_monitor |
5 | 7 | import platform
|
6 |
| -from typing import Any |
| 8 | +from opentelemetry.trace import Status, StatusCode |
7 | 9 |
|
8 |
| -import pkg_resources |
9 |
| -from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter |
| 10 | +# Use environment variable if available, otherwise fall back to hardcoded value |
10 | 11 | from opentelemetry import trace
|
11 |
| -from opentelemetry.sdk.resources import SERVICE_NAME, Resource |
12 | 12 | from opentelemetry.sdk.trace import TracerProvider
|
13 | 13 | from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
14 |
| -from opentelemetry.trace import Status, StatusCode |
| 14 | +from logging import INFO, getLogger |
| 15 | +from dotenv import load_dotenv |
| 16 | +from azure.monitor.opentelemetry import configure_azure_monitor |
| 17 | +load_dotenv() |
15 | 18 |
|
| 19 | +APPLICATIONINSIGHTS_CONNECTION_STRING = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") |
16 | 20 |
|
17 | 21 | class Telemetry:
|
18 |
| - """A class to handle anonymous telemetry for the DataFog package.""" |
19 |
| - |
20 |
| - def __init__(self, instrumentation_key: str = os.getenv("INSTRUMENTATION_KEY")): |
| 22 | + def __init__(self): |
21 | 23 | self.ready = False
|
22 | 24 | self.trace_set = False
|
23 | 25 | try:
|
24 |
| - self.resource = Resource(attributes={SERVICE_NAME: "datafog-python"}) |
25 |
| - self.provider = TracerProvider(resource=self.resource) |
| 26 | + # Create a new TracerProvider and set it as the global trace provider |
| 27 | + tracer_provider = TracerProvider() |
| 28 | + trace.set_tracer_provider(tracer_provider) |
| 29 | + |
| 30 | + # Configure Azure Monitor with the connection string from environment variables |
| 31 | + configure_azure_monitor(connection_string=APPLICATIONINSIGHTS_CONNECTION_STRING, logger_name="datafog_logger") |
| 32 | + |
| 33 | + # Create an exporter that sends data to Application Insights |
26 | 34 | exporter = AzureMonitorTraceExporter(
|
27 |
| - connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] |
| 35 | + connection_string=APPLICATIONINSIGHTS_CONNECTION_STRING |
28 | 36 | )
|
29 |
| - processor = BatchSpanProcessor(exporter) |
30 |
| - self.provider.add_span_processor(processor) |
| 37 | + |
| 38 | + # Create a span processor and add it to the tracer provider |
| 39 | + span_processor = BatchSpanProcessor(exporter) |
| 40 | + tracer_provider.add_span_processor(span_processor) |
| 41 | + |
| 42 | + # Get a tracer |
| 43 | + self.tracer = trace.get_tracer(__name__) |
| 44 | + |
31 | 45 | self.ready = True
|
32 |
| - except BaseException as e: |
33 |
| - if isinstance( |
34 |
| - e, |
35 |
| - (SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError), |
36 |
| - ): |
37 |
| - raise |
38 |
| - self.ready = False |
| 46 | + self.trace_set = True |
39 | 47 |
|
40 |
| - def set_tracer(self): |
41 |
| - """Sets the tracer for telemetry.""" |
42 |
| - if self.ready: |
43 |
| - try: |
44 |
| - trace.set_tracer_provider(self.provider) |
45 |
| - self.trace_set = True |
46 |
| - except Exception: |
47 |
| - self.trace_set = False |
| 48 | + except Exception as e: |
| 49 | + print(f"Error setting up Azure Monitor: {e}") |
48 | 50 |
|
49 |
| - def log_system_info(self): |
50 |
| - """Logs system information.""" |
51 |
| - if self.ready: |
52 |
| - try: |
53 |
| - tracer = trace.get_tracer("datafog.telemetry") |
54 |
| - with tracer.start_as_current_span("System Info") as span: |
55 |
| - self._add_attribute( |
56 |
| - span, |
57 |
| - "datafog_version", |
58 |
| - pkg_resources.get_distribution("datafog").version, |
59 |
| - ) |
60 |
| - self._add_attribute( |
61 |
| - span, "python_version", platform.python_version() |
62 |
| - ) |
63 |
| - self._add_attribute(span, "os", platform.system()) |
64 |
| - self._add_attribute(span, "platform_version", platform.version()) |
65 |
| - self._add_attribute(span, "cpus", os.cpu_count()) |
66 |
| - span.set_status(Status(StatusCode.OK)) |
67 |
| - except Exception: |
68 |
| - pass |
69 | 51 |
|
70 |
| - def pipeline_execution(self, datafog, input_data, output_data): |
71 |
| - """Records the execution of a DataFog pipeline.""" |
72 |
| - if self.ready: |
73 |
| - try: |
74 |
| - tracer = trace.get_tracer("datafog.telemetry") |
75 |
| - with tracer.start_as_current_span("Pipeline Execution") as span: |
76 |
| - self._add_attribute( |
77 |
| - span, |
78 |
| - "datafog_version", |
79 |
| - pkg_resources.get_distribution("datafog").version, |
80 |
| - ) |
81 |
| - self._add_attribute( |
82 |
| - span, "pipeline_type", datafog.__class__.__name__ |
83 |
| - ) |
84 |
| - self._add_attribute(span, "input_data", input_data) |
85 |
| - self._add_attribute(span, "output_data", output_data) |
86 |
| - span.set_status(Status(StatusCode.OK)) |
87 |
| - except Exception: |
88 |
| - pass |
89 | 52 |
|
90 |
| - def end_pipeline(self, datafog, output): |
| 53 | + def datafog_creation(self, name: str): |
91 | 54 | if self.ready:
|
92 | 55 | try:
|
93 |
| - tracer = trace.get_tracer("datafog.telemetry") |
94 |
| - with tracer.start_as_current_span("Pipeline Ended") as span: |
95 |
| - self._add_attribute( |
96 |
| - span, |
97 |
| - "datafog_version", |
98 |
| - pkg_resources.get_distribution("datafog").version, |
99 |
| - ) |
100 |
| - self._add_attribute( |
101 |
| - span, "pipeline_type", datafog.__class__.__name__ |
102 |
| - ) |
103 |
| - self._add_attribute(span, "output", output) |
104 |
| - span.set_status(Status(StatusCode.OK)) |
105 |
| - except Exception: |
106 |
| - pass |
| 56 | + tracer = trace.get_tracer(__name__) |
| 57 | + span = tracer.start_span("datafog object created") |
| 58 | + self._add_attribute(span, "datafog_name", name) |
| 59 | + self._add_attribute(span, "datafog_version", platform.python_version()) |
| 60 | + span.set_status(Status(StatusCode.OK)) |
| 61 | + span.end() |
| 62 | + except Exception as e: |
| 63 | + print(f"Error starting span: {e}") |
| 64 | + return None |
107 | 65 |
|
108 | 66 | def _add_attribute(self, span, key, value):
|
109 | 67 | """Add an attribute to a span."""
|
110 | 68 | try:
|
111 |
| - span.set_attribute(key, value) |
| 69 | + return span.set_attribute(key, value) |
112 | 70 | except Exception:
|
113 | 71 | pass
|
| 72 | + |
0 commit comments