Skip to main content

OpenCensus Azure Monitor Exporter

Project description

pypi

Installation

pip install opencensus-ext-azure

Usage

Trace

The Azure Monitor Trace Exporter allows you to export OpenCensus 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.

  • Put the instrumentation key in APPINSIGHTS_INSTRUMENTATIONKEY environment variable.

from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer

tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))

with tracer.span(name='hello'):
    print('Hello, World!')

You can also specify the instrumentation key explicitly in the code.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.

  • Install the requests integration package using pip install opencensus-ext-requests.

  • Put the instrumentation key in the following code.

import requests

from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer

config_integration.trace_integrations(['requests'])
tracer = Tracer(
    exporter=AzureExporter(
        # TODO: replace this with your own instrumentation key.
        instrumentation_key='00000000-0000-0000-0000-000000000000',
    ),
    sampler=ProbabilitySampler(1.0),
)
with tracer.span(name='parent'):
    response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')

Log

The Azure Monitor Log Handler allows you to export Python logs to Azure Monitor.

This example shows how to send a warning level log to Azure Monitor.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.

  • Put the instrumentation key in APPINSIGHTS_INSTRUMENTATIONKEY environment variable.

import logging

from opencensus.ext.azure.log_exporter import AzureLogHandler

logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler())
logger.warning('Hello, World!')

You can enrich the logs with trace IDs and span IDs by using the logging integration.

  • Create an Azure Monitor resource and get the instrumentation key, more information can be found here.

  • Install the logging integration package using pip install opencensus-ext-logging.

  • Put the instrumentation key in APPINSIGHTS_INSTRUMENTATIONKEY environment variable.

import logging

from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer

config_integration.trace_integrations(['logging'])

logger = logging.getLogger(__name__)

handler = AzureLogHandler()
handler.setFormatter(logging.Formatter('%(traceId)s %(spanId)s %(message)s'))
logger.addHandler(handler)

tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))

logger.warning('Before the span')
with tracer.span(name='test'):
    logger.warning('In the span')
logger.warning('After the span')

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opencensus_ext_azure-0.2.1-py2.py3-none-any.whl (16.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file opencensus_ext_azure-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: opencensus_ext_azure-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for opencensus_ext_azure-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a01b7fe623e1cf972753b66f6445fae3cecd6f9d660eed774b5193dd56469f65
MD5 ab735392e828dddefa8a8ab479761d45
BLAKE2b-256 b0840fcfe4bfa0ee82fe8d19e13c0c6e0973b97f17ae1e6f93b3b00f78538035

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page