This repository is the home to Azure Monitor SDKs and exporters utilizing the OpenTelemetry Python Client https://github.com/open-telemetry/opentelemetry-python to send telemetry data to Azure Monitor written in Python.
Перейти к файлу
Leighton Chen 3bd8b514eb update 2021-03-02 10:51:21 -08:00
.github codeowners 2019-11-05 13:50:12 -08:00
azure_monitor update 2021-03-02 10:51:21 -08:00
docs update 2020-09-24 15:40:29 -04:00
scripts Update 2020-01-31 12:31:19 -08:00
.coveragerc Addressing comments 2020-04-28 15:11:03 -07:00
.flake8 Update 2020-01-31 12:31:19 -08:00
.gitignore WIP 2020-02-26 15:13:30 -08:00
.isort.cfg lint 2020-01-31 11:53:09 -08:00
.pylintrc Update 2020-01-31 12:31:19 -08:00
.readthedocs.yml Adding doc requirements 2020-03-26 11:38:59 -07:00
.travis.yml Update .travis.yml 2020-09-17 23:37:48 -04:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2019-10-17 15:34:51 -07:00
CONTRIBUTING.md Adding usage instructions 2020-02-28 16:16:41 -08:00
LICENSE changes 2019-11-05 13:44:34 -08:00
README.md update 2021-03-02 10:51:21 -08:00
SECURITY.md Initial SECURITY.md commit 2019-10-17 15:34:54 -07:00
dev-requirements.txt Addressing comments 2020-03-27 11:33:50 -07:00
docs-requirements.txt update 2020-09-24 15:40:29 -04:00
pyproject.toml lint 2020-01-31 11:53:09 -08:00
tox.ini remove 3.4 2020-09-17 23:33:18 -04:00

README.md

This repository has been moved to the Azure SDK for Python repository. In order to improve discoverability and share common dependencies/tests, the OpenTelemetry Azure Monitor exporters for Python has moved to a common location containing all Azure SDKs. Please submit all issues and inquiries in that repository.

OpenTelemetry Azure Monitor

Gitter chat Build status

Installation

pip install opentelemetry-azure-monitor

Documentation

The online documentation is available at https://opentelemetry-azure-monitor-python.readthedocs.io/.

Usage

Trace

The Azure Monitor Span Exporter allows you to export OpenTelemetry traces to Azure Monitor.

This example shows how to send a span "hello" to Azure Monitor.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
  • Place your instrumentation key in a connection string and directly into your code.
  • Alternatively, you can specify your connection string in an environment variable APPLICATIONINSIGHTS_CONNECTION_STRING.
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# SpanExporter receives the spans and send them to the target location
exporter = AzureMonitorSpanExporter(
    connection_string='InstrumentationKey=<your-ikey-here>',
)

span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span('hello'):
    print('Hello World!')

Instrumentations

OpenTelemetry also supports several instrumentations which allows to instrument with third party libraries.

This example shows how to instrument with the requests_ library.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
  • Install the requests integration package using pip install opentelemetry-ext-http-requests.
  • Place your instrumentation key in a connection string and directly into your code.
  • Alternatively, you can specify your connection string in an environment variable APPLICATIONINSIGHTS_CONNECTION_STRING.
import requests

from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.ext.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer_provider = trace.get_tracer_provider()

exporter = AzureMonitorSpanExporter(
    connection_string='InstrumentationKey=<your-ikey-here>',
)
span_processor = BatchExportSpanProcessor(exporter)
tracer_provider.add_span_processor(span_processor)

RequestsInstrumentor().instrument()

# This request will be traced
response = requests.get(url="https://azure.microsoft.com/")

Modifying Traces

  • You can pass a callback function to the exporter to process telemetry before it is exported.
  • Your callback function can return False if you do not want this envelope exported.
  • Your callback function must accept an envelope data type as its parameter.
  • You can see the schema for Azure Monitor data types in the envelopes here.
  • The AzureMonitorSpanExporter handles Data data types.
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

# Callback function to add os_type: linux to span properties
def callback_function(envelope):
    envelope.data.baseData.properties['os_type'] = 'linux'
    return True

exporter = AzureMonitorSpanExporter(
    connection_string='InstrumentationKey=<your-ikey-here>'
)
# This line will modify telemetry
exporter.add_telemetry_processor(callback_function)

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span('hello'):
    print('Hello World!')

Metrics

The Azure Monitor Metrics Exporter allows you to export metrics to Azure Monitor.

This example shows how to track a counter metric and send it as telemetry every export interval.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
  • Place your instrumentation key in a connection string and directly into your code.
  • Alternatively, you can specify your connection string in an environment variable APPLICATIONINSIGHTS_CONNECTION_STRING.
from azure_monitor import AzureMonitorMetricsExporter
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
exporter = AzureMonitorMetricsExporter(
    connection_string='InstrumentationKey=<your-ikey-here>'
)
controller = PushController(meter, exporter, 5)

requests_counter = meter.create_metric(
    name="requests",
    description="number of requests",
    unit="1",
    value_type=int,
    metric_type=Counter,
    label_keys=("environment",),
)

testing_labels = {"environment": "testing"}

requests_counter.add(25, testing_labels)
input("...")