-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sid Mohan
authored and
Sid Mohan
committed
May 28, 2024
1 parent
4aeb2f0
commit 2facea8
Showing
438 changed files
with
24,467 additions
and
30,991 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=00bea047-1836-46fa-9652-26d43d63a3fa;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=959cc365-c112-491b-af69-b196d0943ca4" | ||
|
||
|
||
# note this is an Azure specific implementation of the OpenTelemetry distro. for more information please see https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,72 @@ | ||
import asyncio | ||
import json | ||
import logging | ||
import os | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
from azure.monitor.opentelemetry import configure_azure_monitor | ||
import platform | ||
from typing import Any | ||
from opentelemetry.trace import Status, StatusCode | ||
|
||
import pkg_resources | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
# Use environment variable if available, otherwise fall back to hardcoded value | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.resources import SERVICE_NAME, Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
from opentelemetry.trace import Status, StatusCode | ||
from logging import INFO, getLogger | ||
from dotenv import load_dotenv | ||
from azure.monitor.opentelemetry import configure_azure_monitor | ||
load_dotenv() | ||
|
||
APPLICATIONINSIGHTS_CONNECTION_STRING = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") | ||
|
||
class Telemetry: | ||
"""A class to handle anonymous telemetry for the DataFog package.""" | ||
|
||
def __init__(self, instrumentation_key: str = os.getenv("INSTRUMENTATION_KEY")): | ||
def __init__(self): | ||
self.ready = False | ||
self.trace_set = False | ||
try: | ||
self.resource = Resource(attributes={SERVICE_NAME: "datafog-python"}) | ||
self.provider = TracerProvider(resource=self.resource) | ||
# Create a new TracerProvider and set it as the global trace provider | ||
tracer_provider = TracerProvider() | ||
trace.set_tracer_provider(tracer_provider) | ||
|
||
# Configure Azure Monitor with the connection string from environment variables | ||
configure_azure_monitor(connection_string=APPLICATIONINSIGHTS_CONNECTION_STRING, logger_name="datafog_logger") | ||
|
||
# Create an exporter that sends data to Application Insights | ||
exporter = AzureMonitorTraceExporter( | ||
connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
connection_string=APPLICATIONINSIGHTS_CONNECTION_STRING | ||
) | ||
processor = BatchSpanProcessor(exporter) | ||
self.provider.add_span_processor(processor) | ||
|
||
# Create a span processor and add it to the tracer provider | ||
span_processor = BatchSpanProcessor(exporter) | ||
tracer_provider.add_span_processor(span_processor) | ||
|
||
# Get a tracer | ||
self.tracer = trace.get_tracer(__name__) | ||
|
||
self.ready = True | ||
except BaseException as e: | ||
if isinstance( | ||
e, | ||
(SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError), | ||
): | ||
raise | ||
self.ready = False | ||
self.trace_set = True | ||
|
||
def set_tracer(self): | ||
"""Sets the tracer for telemetry.""" | ||
if self.ready: | ||
try: | ||
trace.set_tracer_provider(self.provider) | ||
self.trace_set = True | ||
except Exception: | ||
self.trace_set = False | ||
except Exception as e: | ||
print(f"Error setting up Azure Monitor: {e}") | ||
|
||
def log_system_info(self): | ||
"""Logs system information.""" | ||
if self.ready: | ||
try: | ||
tracer = trace.get_tracer("datafog.telemetry") | ||
with tracer.start_as_current_span("System Info") as span: | ||
self._add_attribute( | ||
span, | ||
"datafog_version", | ||
pkg_resources.get_distribution("datafog").version, | ||
) | ||
self._add_attribute( | ||
span, "python_version", platform.python_version() | ||
) | ||
self._add_attribute(span, "os", platform.system()) | ||
self._add_attribute(span, "platform_version", platform.version()) | ||
self._add_attribute(span, "cpus", os.cpu_count()) | ||
span.set_status(Status(StatusCode.OK)) | ||
except Exception: | ||
pass | ||
|
||
def pipeline_execution(self, datafog, input_data, output_data): | ||
"""Records the execution of a DataFog pipeline.""" | ||
if self.ready: | ||
try: | ||
tracer = trace.get_tracer("datafog.telemetry") | ||
with tracer.start_as_current_span("Pipeline Execution") as span: | ||
self._add_attribute( | ||
span, | ||
"datafog_version", | ||
pkg_resources.get_distribution("datafog").version, | ||
) | ||
self._add_attribute( | ||
span, "pipeline_type", datafog.__class__.__name__ | ||
) | ||
self._add_attribute(span, "input_data", input_data) | ||
self._add_attribute(span, "output_data", output_data) | ||
span.set_status(Status(StatusCode.OK)) | ||
except Exception: | ||
pass | ||
|
||
def end_pipeline(self, datafog, output): | ||
def datafog_creation(self, name: str): | ||
if self.ready: | ||
try: | ||
tracer = trace.get_tracer("datafog.telemetry") | ||
with tracer.start_as_current_span("Pipeline Ended") as span: | ||
self._add_attribute( | ||
span, | ||
"datafog_version", | ||
pkg_resources.get_distribution("datafog").version, | ||
) | ||
self._add_attribute( | ||
span, "pipeline_type", datafog.__class__.__name__ | ||
) | ||
self._add_attribute(span, "output", output) | ||
span.set_status(Status(StatusCode.OK)) | ||
except Exception: | ||
pass | ||
tracer = trace.get_tracer(__name__) | ||
span = tracer.start_span("datafog object created") | ||
self._add_attribute(span, "datafog_name", name) | ||
self._add_attribute(span, "datafog_version", platform.python_version()) | ||
span.set_status(Status(StatusCode.OK)) | ||
span.end() | ||
except Exception as e: | ||
print(f"Error starting span: {e}") | ||
return None | ||
|
||
def _add_attribute(self, span, key, value): | ||
"""Add an attribute to a span.""" | ||
try: | ||
span.set_attribute(key, value) | ||
return span.set_attribute(key, value) | ||
except Exception: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
APPLICATIONINSIGHTS_CONNECTION_STRING="" # note this is an Azure specific implementation of the OpenTelemetry distro. for more information please see https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.