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 e557a9a313
Merge pull request #64 from lzchen/name
2020-03-26 08:23:25 -07:00
.github codeowners 2019-11-05 13:50:12 -08:00
azure_monitor Update azure_monitor/examples/metrics/simple.py 2020-03-26 08:17:42 -07:00
docs Refactor 2020-03-25 13:39:57 -07:00
scripts Update 2020-01-31 12:31:19 -08:00
.coveragerc add coverage 2020-03-13 12:51:43 -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
.travis.yml Add travis 2020-01-31 12:00:30 -08: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 Refactor 2020-03-25 13:39:57 -07:00
SECURITY.md Initial SECURITY.md commit 2019-10-17 15:34:54 -07:00
dev-requirements.txt CI up until lint 2020-01-31 11:37:26 -08:00
pyproject.toml lint 2020-01-31 11:53:09 -08:00
tox.ini Addressing comments 2020-03-24 12:28:02 -07:00

README.md

OpenTelemetry Azure Monitor SDKs and Exporters

Gitter chat Build status PyPI version

Installation

pip install opentelemetry-azure-monitor-exporter

Usage

Trace

The Azure Monitor Trace 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())

# We tell OpenTelemetry who it is that is creating spans. In this case, we have
# no real name (no setup.py), so we make one up. If we had a version, we would
# also specify it here.
tracer = trace.get_tracer(__name__)

exporter = AzureMonitorSpanExporter(
    connection_string='InstrumentationKey=<your-ikey-here>',
)

# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

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

Integrations

OpenTelemetry also supports several integrations which allows to integrate with third party libraries.

This example shows how to integrate 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 import http_requests
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)

http_requests.enable(tracer_provider)
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>'
)
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.
import time

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_label_set = meter.get_label_set({"environment": "testing"})

requests_counter.add(25, testing_label_set)
time.sleep(100)

References

Azure Monitor OpenTelemetry Project OpenTelemetry Python Client Azure Monitor Python Gitter