Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit fe82b7e

Browse files
authored
Skeleton for Azure Log Exporter (#657)
added initial prototype on log exporter added skeleton azure log exporter
1 parent 04c111f commit fe82b7e

File tree

17 files changed

+267
-18
lines changed

17 files changed

+267
-18
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ Stats Exporter
237237
.. _threading: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-threading
238238
.. _Zipkin: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-zipkin
239239

240+
Log Exporter
241+
--------------
242+
243+
- `Azure`_
244+
240245
------------
241246
Versioning
242247
------------

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog
22

33
## Unreleased
4-
- Add persistent storage support
4+
- Added log exporter
5+
([#657](https://github.com/census-instrumentation/opencensus-python/pull/657))
6+
- Added persistent storage support
57
([#640](https://github.com/census-instrumentation/opencensus-python/pull/640))
68
- Changed AzureExporter constructor signature to use kwargs
79
([#632](https://github.com/census-instrumentation/opencensus-python/pull/632))

contrib/opencensus-ext-azure/README.rst

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OpenCensus Azure Monitor Trace Exporter
1+
OpenCensus Azure Monitor Exporters
22
============================================================================
33

44
|pypi|
@@ -21,15 +21,11 @@ Trace
2121

2222
The **Azure Monitor Trace Exporter** allows you to export `OpenCensus`_ traces to `Azure Monitor`_.
2323

24-
.. _Azure Monitor: https://docs.microsoft.com/azure/azure-monitor/
25-
.. _OpenCensus: https://github.com/census-instrumentation/opencensus-python/
26-
2724
This example shows how to send a span "hello" to Azure Monitor.
2825

2926
* 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>`_.
3027
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
3128

32-
3329
.. code:: python
3430
3531
from opencensus.ext.azure.trace_exporter import AzureExporter
@@ -43,6 +39,10 @@ This example shows how to send a span "hello" to Azure Monitor.
4339
4440
You can also specify the instrumentation key explicitly in the code.
4541

42+
* 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>`_.
43+
* Install the `requests integration package <../opencensus-ext-requests>`_ using ``pip install opencensus-ext-requests``.
44+
* Put the instrumentation key in the following code.
45+
4646
.. code:: python
4747
4848
import requests
@@ -57,16 +57,69 @@ You can also specify the instrumentation key explicitly in the code.
5757
exporter=AzureExporter(
5858
# TODO: replace this with your own instrumentation key.
5959
instrumentation_key='00000000-0000-0000-0000-000000000000',
60-
timeout=29.9,
6160
),
6261
sampler=ProbabilitySampler(1.0),
6362
)
6463
with tracer.span(name='parent'):
6564
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
6665
66+
Log
67+
~~~
68+
69+
The **Azure Monitor Log Handler** allows you to export Python logs to `Azure Monitor`_.
70+
71+
This example shows how to send a warning level log to Azure Monitor.
72+
73+
* 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>`_.
74+
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
75+
76+
.. code:: python
77+
78+
import logging
79+
80+
from opencensus.ext.azure.log_exporter import AzureLogHandler
81+
82+
logger = logging.getLogger(__name__)
83+
logger.addHandler(AzureLogHandler())
84+
logger.warning('Hello, World!')
85+
86+
You can enrich the logs with trace IDs and span IDs by using the `logging integration <../opencensus-ext-logging>`_.
87+
88+
* 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>`_.
89+
* Install the `logging integration package <../opencensus-ext-logging>`_ using ``pip install opencensus-ext-logging``.
90+
* Put the instrumentation key in ``APPINSIGHTS_INSTRUMENTATIONKEY`` environment variable.
91+
92+
.. code:: python
93+
94+
import logging
95+
96+
from opencensus.ext.azure.log_exporter import AzureLogHandler
97+
from opencensus.ext.azure.trace_exporter import AzureExporter
98+
from opencensus.trace import config_integration
99+
from opencensus.trace.samplers import ProbabilitySampler
100+
from opencensus.trace.tracer import Tracer
101+
102+
config_integration.trace_integrations(['logging'])
103+
104+
logger = logging.getLogger(__name__)
105+
106+
handler = AzureLogHandler()
107+
handler.setFormatter(logging.Formatter('%(traceId)s %(spanId)s %(message)s'))
108+
logger.addHandler(handler)
109+
110+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
111+
112+
logger.warning('Before the span')
113+
with tracer.span(name='test'):
114+
logger.warning('In the span')
115+
logger.warning('After the span')
116+
67117
References
68118
----------
69119

70120
* `Azure Monitor <https://docs.microsoft.com/azure/azure-monitor/>`_
71121
* `Examples <https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure/examples>`_
72122
* `OpenCensus Project <https://opencensus.io/>`_
123+
124+
.. _Azure Monitor: https://docs.microsoft.com/azure/azure-monitor/
125+
.. _OpenCensus: https://github.com/census-instrumentation/opencensus-python/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
from opencensus.ext.azure.log_exporter import AzureLogHandler
18+
from opencensus.ext.azure.trace_exporter import AzureExporter
19+
from opencensus.trace import config_integration
20+
from opencensus.trace.samplers import ProbabilitySampler
21+
from opencensus.trace.tracer import Tracer
22+
23+
config_integration.trace_integrations(['logging'])
24+
25+
logger = logging.getLogger(__name__)
26+
27+
# TODO: you need to specify the instrumentation key in the
28+
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
29+
handler = AzureLogHandler()
30+
handler.setFormatter(logging.Formatter('%(traceId)s %(spanId)s %(message)s'))
31+
logger.addHandler(handler)
32+
33+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
34+
35+
logger.warning('Before the span')
36+
with tracer.span(name='test'):
37+
logger.warning('In the span')
38+
logger.warning('After the span')
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
from opencensus.ext.azure.log_exporter import AzureLogHandler
18+
19+
logger = logging.getLogger(__name__)
20+
# TODO: you need to specify the instrumentation key in the
21+
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
22+
logger.addHandler(AzureLogHandler())
23+
logger.warning('Hello, World!')
File renamed without changes.
File renamed without changes.

contrib/opencensus-ext-azure/examples/custom.py renamed to contrib/opencensus-ext-azure/examples/traces/custom.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)',
2323
'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
2424
instrumentation_key='00000000-0000-0000-0000-000000000000',
25-
timeout=29.9,
2625
)''',
2726
},
2827
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)