Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def _initialize(cls):
_diagnostic_file_logger.addHandler(f_handler)
cls._initialized = True
except Exception as e: # pylint: disable=broad-except
_logger.error("Failed to initialize Azure Monitor diagnostic logging: %s", e)
_logger.error(
"Failed to initialize Azure Monitor diagnostic logging: %s", e
) # pylint: disable=do-not-log-exceptions-if-not-debug
cls._initialized = False

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_dependency_conflicts(
try:
req = Requirement(dep)
except InvalidRequirement as exc:
logger.warning(
logger.warning( # pylint: disable=do-not-log-exceptions-if-not-debug
'error parsing dependency, reporting as a conflict: "%s" - %s',
dep,
exc,
Expand Down Expand Up @@ -127,7 +127,7 @@ def _get_dependency_conflicts_any(
try:
req = Requirement(dep)
except InvalidRequirement as exc:
logger.warning(
logger.warning( # pylint: disable=do-not-log-exceptions-if-not-debug
'error parsing dependency, reporting as a conflict: "%s" - %s',
dep,
exc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
An example to show an application using Opentelemetry tracing api and sdk with a Azure Managed Identity
Credential. Credentials are used for Azure Active Directory/EntraId Authentication.
"""
from opentelemetry import trace

# You will need to install azure-identity
from azure.identity import ManagedIdentityCredential
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace


credential = ManagedIdentityCredential(client_id="<client_id>")
configure_azure_monitor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
An example to show an application using Opentelemetry tracing api and sdk with a Azure Client Secret
Credential. Credentials are used for Azure Active Directory/EntraId Authentication.
"""
from opentelemetry import trace

# You will need to install azure-identity
from azure.identity import ClientSecretCredential
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

credential = ClientSecretCredential(
tenant_id="<tenant_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from logging import getLogger

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor(
logger_name=__name__,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from logging import getLogger

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

configure_azure_monitor(
logger_name=__name__,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
val = 1 / 0
print(val)
except ZeroDivisionError:
logger.exception("Error: Division by zero")
logger.exception("Error: Division by zero") # pylint: disable=do-not-use-logging-exception

try:
val = 1 / 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import logging
from logging import getLogger
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys
from opentelemetry import trace
from opentelemetry.sdk._logs import LogRecordProcessor, ReadableLogRecord
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys

logger = getLogger(__name__)
logger.setLevel(logging.INFO)
Expand All @@ -18,12 +18,12 @@
class LogRecordEnrichingProcessor(LogRecordProcessor):
"""Enriches log records with operation name from the current span context."""

def on_emit(self, readable_log_record: ReadableLogRecord) -> None:
def on_emit(self, log_record: ReadableLogRecord) -> None:
current_span = trace.get_current_span()
if current_span and getattr(current_span, "name", None):
if readable_log_record.log_record.attributes is None:
readable_log_record.log_record.attributes = {}
readable_log_record.log_record.attributes[ContextTagKeys.AI_OPERATION_NAME] = current_span.name
if log_record.log_record.attributes is None:
log_record.log_record.attributes = {}
log_record.log_record.attributes[ContextTagKeys.AI_OPERATION_NAME] = current_span.name

def shutdown(self) -> None:
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# license information.
# --------------------------------------------------------------------------

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import metrics
from azure.monitor.opentelemetry import configure_azure_monitor

# Configure Azure monitor collection telemetry pipeline
configure_azure_monitor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@

from typing import Iterable

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import metrics
from opentelemetry.metrics import CallbackOptions, Observation
from opentelemetry.sdk.resources import Resource, ResourceAttributes
from azure.monitor.opentelemetry import configure_azure_monitor

# Configure Azure monitor collection telemetry pipeline
configure_azure_monitor()


# Callback functions for observable instruments
def observable_counter_func(options: CallbackOptions) -> Iterable[Observation]:
def observable_counter_func(_options: CallbackOptions) -> Iterable[Observation]:
yield Observation(1, {})


def observable_up_down_counter_func(
options: CallbackOptions,
_options: CallbackOptions,
) -> Iterable[Observation]:
yield Observation(-10, {})


def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:
def observable_gauge_func(_options: CallbackOptions) -> Iterable[Observation]:
yield Observation(9, {})


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Live metrics is enabled by default, it can be disabled by setting `enable_live_metrics` to `False`
"""
import logging
import requests # type: ignore[import-untyped]
import time
import requests # type: ignore[import-untyped] # pylint: disable=networking-import-outside-azure-core-transport

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor(
resource=Resource.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

from time import sleep

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import metrics
from opentelemetry.sdk.metrics.export import (
MetricExportResult,
MetricExporter,
MetricsData,
PeriodicExportingMetricReader,
)
from azure.monitor.opentelemetry import configure_azure_monitor


class PrintMetricExporter(MetricExporter):
Expand All @@ -27,7 +27,7 @@ def export(self, metrics_data: MetricsData, **kwargs) -> MetricExportResult: #
def shutdown(self, timeout_millis: float = 30000, **kwargs) -> None: # type: ignore[override]
return None

def force_flush(self, timeout_millis: float = 30000, **kwargs) -> bool: # type: ignore[override]
def force_flush(self, timeout_millis: float = 30000, **kwargs) -> bool: # type: ignore[override] # pylint: disable=unused-argument
return True


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# license information.
# --------------------------------------------------------------------------

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics.view import View
from azure.monitor.opentelemetry import configure_azure_monitor

# Create a view matching the counter instrument `my.counter`
# and configure the new name `my.counter.total` for the result metrics stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# license information.
# --------------------------------------------------------------------------

from os import environ
import os
import sys

from opentelemetry import trace
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage, CompletionsFinishReason
from azure.ai.inference.models import UserMessage
from azure.core.credentials import AzureKeyCredential

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

# Set up exporting to Azure Monitor
configure_azure_monitor()
Expand All @@ -25,12 +25,12 @@
except KeyError:
print("Missing environment variable 'AZURE_AI_CHAT_ENDPOINT' or 'AZURE_AI_CHAT_KEY'")
print("Set them before running this sample.")
exit()
sys.exit()

is_content_tracing_enabled = os.environ["AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED"]
if not is_content_tracing_enabled:
print(
f"Content tracing is disabled. Set 'AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED' to 'true' to record prompts and completions."
"Content tracing is disabled. Set 'AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED' to 'true' to record prompts and completions." # pylint: disable=line-too-long
)

tracer = trace.get_tracer(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

from os import environ

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.storage.blob import BlobServiceClient

# Set up exporting to Azure Monitor
configure_azure_monitor()

# Example with Storage SDKs

from azure.storage.blob import BlobServiceClient

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(name="MyApplication"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# license information.
# --------------------------------------------------------------------------

from django.contrib import admin
from django.contrib import admin # pylint: disable=unused-import

# Register your models here.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# license information.
# --------------------------------------------------------------------------

from django.db import models
from django.db import models # pylint: disable=unused-import

# Create your models here.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# license information.
# --------------------------------------------------------------------------

from django.test import TestCase
from django.test import TestCase # pylint: disable=unused-import

# Create your tests here.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


# Requests sent to the django application will be automatically captured
def index(request):
def index(_request):
return HttpResponse("Hello, world.")


# Exceptions that are raised within the request are automatically captured
def exception(request):
raise Exception("Exception was raised.")
def exception(_request):
raise Exception("Exception was raised.") # pylint: disable=broad-exception-raised
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def main():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample.settings")

# Configure Azure monitor collection telemetry pipeline
# configure_azure_monitor should only be called once in either asgi.py, wsgi.py, or manage.py, depending on startup method.
# configure_azure_monitor should only be called once in either asgi.py, wsgi.py, or manage.py,
# depending on startup method.
# If using manage.py, please remove configure_azure_monitor from asgi.py and wsgi.py
configure_azure_monitor()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

import os

from azure.monitor.opentelemetry import configure_azure_monitor
from django.core.asgi import get_asgi_application
from azure.monitor.opentelemetry import configure_azure_monitor


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample.settings")

# Configure Azure monitor collection telemetry pipeline
# configure_azure_monitor should only be called once in etiher asgi.py, wsgi.py, or manage.py, depending on startup method.
# configure_azure_monitor should only be called once in etiher asgi.py, wsgi.py, or manage.py,
# depending on startup method.
# If using asgi, please remove configure_azure_monitor from wsgi.py and manage.py
configure_azure_monitor()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

import os

from azure.monitor.opentelemetry import configure_azure_monitor
from django.core.wsgi import get_wsgi_application
from azure.monitor.opentelemetry import configure_azure_monitor


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample.settings")

# Configure Azure monitor collection telemetry pipeline
# configure_azure_monitor should only be called once in either asgi.py, wsgi.py, or manage.py, depending on startup method.
# configure_azure_monitor should only be called once in either asgi.py, wsgi.py, or manage.py,
# depending on startup method.
# If using wsgi, please remove configure_azure_monitor from asgi.py and manage.py
configure_azure_monitor()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# license information.
# --------------------------------------------------------------------------

import requests # type: ignore[import-untyped]
from azure.monitor.opentelemetry import configure_azure_monitor
import requests # type: ignore[import-untyped] # pylint: disable=networking-import-outside-azure-core-transport
from opentelemetry.sdk.trace import SpanProcessor
from opentelemetry.trace import get_tracer, SpanContext, SpanKind, TraceFlags
from azure.monitor.opentelemetry import configure_azure_monitor


# Define a custom processor to filter your spans
# pylint: disable=protected-access
class SpanFilteringProcessor(SpanProcessor):
# Prevents exporting spans that are of kind INTERNAL
def on_start(self, span, parent_context): # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test():
# Exceptions that are raised within the request are automatically captured
@app.get("/exception")
async def exception():
raise Exception("Hit an exception")
raise Exception("Hit an exception") # pylint: disable=broad-exception-raised


# Set the OTEL_PYTHON_EXCLUDED_URLS environment variable to "http://127.0.0.1:8000/exclude"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
configure_azure_monitor()

# Import Flask after running configure_azure_monitor()
import flask
import flask # pylint: disable=wrong-import-position, wrong-import-order

app = flask.Flask(__name__)

Expand All @@ -24,7 +24,7 @@ def test():
# Exceptions that are raised within the request are automatically captured
@app.route("/exception")
def exception():
raise Exception("Hit an exception")
raise Exception("Hit an exception") # pylint: disable=broad-exception-raised


# Requests sent to this endpoint will not be tracked due to
Expand Down
Loading
Loading