Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
Merged
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
27 changes: 16 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,15 @@ OpenCensus supports integration with popular web frameworks, client libraries an
- `SQLAlchemy`_
- `threading`_

Trace Exporter
--------------
Log Exporter
------------

- `Azure`_

Metrics Exporter
----------------

- `Azure`_
- `Jaeger`_
- `OCAgent`_
- `Stackdriver`_
- `Zipkin`_

Stats Exporter
--------------
Expand All @@ -221,6 +222,15 @@ Stats Exporter
- `Prometheus`_
- `Stackdriver`_

Trace Exporter
--------------

- `Azure`_
- `Jaeger`_
- `OCAgent`_
- `Stackdriver`_
- `Zipkin`_

.. _Azure: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure
.. _Django: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django
.. _Flask: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-flask
Expand All @@ -243,11 +253,6 @@ Stats Exporter
.. _threading: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-threading
.. _Zipkin: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-zipkin

Log Exporter
--------------

- `Azure`_

------------
Versioning
------------
Expand Down
2 changes: 2 additions & 0 deletions contrib/opencensus-ext-azure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased
- Added metrics exporter
([#678](https://github.com/census-instrumentation/opencensus-python/pull/678)

## 0.2.1
Released 2019-06-13
Expand Down
156 changes: 109 additions & 47 deletions contrib/opencensus-ext-azure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,6 @@ Installation
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 <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.

.. code:: python

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 <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
* Put the instrumentation key in the following code.

.. code:: python

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
~~~

Expand All @@ -72,6 +25,7 @@ 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 <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.

.. code:: python

Expand All @@ -88,6 +42,7 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Install the `logging integration package <../opencensus-ext-logging>`_ using ``pip install opencensus-ext-logging``.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.

.. code:: python

Expand All @@ -114,6 +69,113 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
logger.warning('In the span')
logger.warning('After the span')

Metrics
~~~~~~~

The **OpenCensus Azure Monitor Metrics Exporter** allows you to export metrics to `Azure Monitor`_.
Copy link
Copy Markdown
Contributor

@reyang reyang Jun 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I feel that we don't need to mention "OpenCensus" here given the context we have in the doc, otherwise it is too long?
Up to you, if you decide to keep it, need to update trace/logs to make them consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. We should be consistent with the other two and it seems a bit redundant given the package we are in. I shall remove it.


* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.

Using the Metrics exporter
*****************************

.. code:: python

import time

from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module

stats = stats_module.stats
view_manager = stats.view_manager
stats_recorder = stats.stats_recorder

CARROTS_MEASURE = measure_module.MeasureInt("carrots",
"number of carrots",
"carrots")
CARROTS_VIEW = view_module.View("carrots_view",
"number of carrots",
[],
CARROTS_MEASURE,
aggregation_module.CountAggregation())


def main():
# Enable metrics
# Set the interval in seconds in which you want to send metrics
exporter = metrics_exporter.new_metrics_exporter(export_interval=2)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion, it is not clear if the interval should be an integer or it can be a float/decimal, consider either update the comment or put something like 2.0 (if we do support float).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We allow floats so I will put 2.0 in the sample to let users know implicitly this is possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, this is something that we've been practicing, e.g. ProbabilitySampler(1.0) and

aggregation_module.DistributionAggregation(
        [100.0, 200.0, 400.0, 1000.0, 2000.0, 4000.0]))

view_manager.register_exporter(exporter)

view_manager.register_view(CARROTS_VIEW)
mmap = stats_recorder.new_measurement_map()
tmap = tag_map_module.TagMap()

mmap.measure_int_put(CARROTS_MEASURE, 1000)
mmap.record(tmap)
time.sleep(10)

print("Done recording metrics")


if __name__ == "__main__":
main()

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 <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.

.. code:: python

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 <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_.
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
* You can also specify the instrumentation key explicitly in the code, which will take priority over a set environment variable.

.. code:: python

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')


References
----------

Expand Down
66 changes: 66 additions & 0 deletions contrib/opencensus-ext-azure/examples/metrics/distribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2019, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random
import time

from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module

stats = stats_module.stats
view_manager = stats.view_manager
stats_recorder = stats.stats_recorder

# Create the measures
# The latency in milliseconds
m_latency_ms = measure_module.MeasureFloat(
"task_latency", "The task latency in milliseconds", "ms")

# Create a view in which defines an aggregation and tag keys
latency_view = view_module.View(
"task_latency_distribution",
"The distribution of the task latencies",
[],
m_latency_ms,
# Latency in buckets: [>=0ms, >=100ms, >=200ms, >=400ms, >=1s, >=2s, >=4s]
aggregation_module.DistributionAggregation(
[100.0, 200.0, 400.0, 1000.0, 2000.0, 4000.0]))


def main():
# Enable metrics
# Set the interval in seconds in which you want to send metrics
exporter = metrics_exporter.new_metrics_exporter(export_interval=5)
view_manager.register_exporter(exporter)

view_manager.register_view(latency_view)
mmap = stats_recorder.new_measurement_map()
tmap = tag_map_module.TagMap()

for i in range(100):
ms = random.random() * 5 * 1000
print("Latency {0}:{1}".format(i, ms))
mmap.measure_float_put(m_latency_ms, ms)
mmap.record(tmap)
time.sleep(1)

print("Done recording metrics")


if __name__ == "__main__":
main()
56 changes: 56 additions & 0 deletions contrib/opencensus-ext-azure/examples/metrics/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2019, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module

stats = stats_module.stats
view_manager = stats.view_manager
stats_recorder = stats.stats_recorder

CARROTS_MEASURE = measure_module.MeasureInt("carrots",
"number of carrots",
"carrots")
CARROTS_VIEW = view_module.View("carrots_view",
"number of carrots",
[],
CARROTS_MEASURE,
aggregation_module.CountAggregation())


def main():
# Enable metrics
# Set the interval in seconds in which you want to send metrics
exporter = metrics_exporter.new_metrics_exporter(export_interval=2)
view_manager.register_exporter(exporter)

view_manager.register_view(CARROTS_VIEW)
mmap = stats_recorder.new_measurement_map()
tmap = tag_map_module.TagMap()

mmap.measure_int_put(CARROTS_MEASURE, 1000)
mmap.record(tmap)
time.sleep(10)

print("Done recording metrics")


if __name__ == "__main__":
main()
Loading