-
Notifications
You must be signed in to change notification settings - Fork 247
Skeleton for Azure Metrics Exporter #678
Changes from all commits
cb7f2cc
41e71a9
ec6f7ea
12a0da6
54f8945
93af575
7ef2ff1
79a0cf0
ab059f9
fb2f4cf
dab802f
8f04c07
a1e940f
d2b6c36
fda6204
5c7adae
ca6a0dc
6e38ff6
41a807f
89ea447
c1cbad6
bdd8141
2c2d45b
a0a98de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| ~~~ | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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`_. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
reyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ***************************** | ||
|
|
||
| .. 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()) | ||
|
|
||
reyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, this is something that we've been practicing, e.g. |
||
| 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") | ||
|
|
||
|
|
||
reyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| ---------- | ||
|
|
||
|
|
||
| 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() |
| 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() |
Uh oh!
There was an error while loading. Please reload this page.