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

Commit e2fa221

Browse files
authored
Update azure ctor, docs and samples (#640)
Updated samples and docs to reflect the default sampler change. Changed the AzureExporter constructor to take kwargs.
1 parent ae9243e commit e2fa221

9 files changed

Lines changed: 78 additions & 94 deletions

File tree

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

79
## 0.1.0

contrib/opencensus-ext-azure/README.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ This example shows how to send a span "hello" to Azure Monitor.
3333
.. code:: python
3434
3535
from opencensus.ext.azure.trace_exporter import AzureExporter
36-
from opencensus.trace import tracer as tracer_module
36+
from opencensus.trace.samplers import ProbabilitySampler
37+
from opencensus.trace.tracer import Tracer
3738
38-
tracer = tracer_module.Tracer(exporter=AzureExporter())
39+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
3940
4041
with tracer.span(name='hello'):
4142
print('Hello, World!')
@@ -46,20 +47,22 @@ You can also specify the instrumentation key explicitly in the code.
4647
4748
import requests
4849
49-
from opencensus.ext.azure.common import Options
5050
from opencensus.ext.azure.trace_exporter import AzureExporter
5151
from opencensus.trace import config_integration
52+
from opencensus.trace.samplers import ProbabilitySampler
5253
from opencensus.trace.tracer import Tracer
5354
54-
if __name__ == '__main__':
55-
config_integration.trace_integrations(['requests'])
56-
tracer = Tracer(exporter=AzureExporter(Options(
55+
config_integration.trace_integrations(['requests'])
56+
tracer = Tracer(
57+
exporter=AzureExporter(
5758
# TODO: replace this with your own instrumentation key.
5859
instrumentation_key='00000000-0000-0000-0000-000000000000',
5960
timeout=29.9,
60-
)))
61-
with tracer.span(name='parent'):
62-
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
61+
),
62+
sampler=ProbabilitySampler(1.0),
63+
)
64+
with tracer.span(name='parent'):
65+
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
6366
6467
References
6568
----------

contrib/opencensus-ext-azure/examples/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
from opencensus.ext.azure.trace_exporter import AzureExporter
1818
from opencensus.trace import config_integration
19+
from opencensus.trace.samplers import ProbabilitySampler
1920
from opencensus.trace.tracer import Tracer
2021

21-
if __name__ == '__main__':
22-
config_integration.trace_integrations(['requests'])
23-
# TODO: you need to specify the instrumentation key in the
24-
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
25-
tracer = Tracer(exporter=AzureExporter())
26-
with tracer.span(name='parent'):
27-
with tracer.span(name='child'):
28-
response = requests.get(url='http://localhost:8080/')
29-
print(response.status_code)
30-
print(response.text)
22+
config_integration.trace_integrations(['requests'])
23+
# TODO: you need to specify the instrumentation key in the
24+
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
25+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
26+
with tracer.span(name='parent'):
27+
with tracer.span(name='child'):
28+
response = requests.get(url='http://localhost:8080/')
29+
print(response.status_code)
30+
print(response.text)

contrib/opencensus-ext-azure/examples/config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from opencensus.ext.azure.common import Options
1615
from opencensus.ext.azure.trace_exporter import AzureExporter
17-
from opencensus.trace import tracer as tracer_module
16+
from opencensus.trace.samplers import ProbabilitySampler
17+
from opencensus.trace.tracer import Tracer
1818

19-
tracer = tracer_module.Tracer(exporter=AzureExporter(Options(
20-
# TODO: replace the all-zero GUID with your instrumentation key.
21-
instrumentation_key='00000000-0000-0000-0000-000000000000',
22-
)))
19+
tracer = Tracer(
20+
exporter=AzureExporter(
21+
# TODO: replace the all-zero GUID with your instrumentation key.
22+
instrumentation_key='00000000-0000-0000-0000-000000000000',
23+
),
24+
sampler=ProbabilitySampler(rate=1.0),
25+
)
2326

2427
with tracer.span(name='foo'):
2528
print('Hello, World!')

contrib/opencensus-ext-azure/examples/custom.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
# TODO: replace the all-zero GUID with your instrumentation key.
2020
app.config['OPENCENSUS'] = {
2121
'TRACE': {
22-
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)',
22+
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)',
2323
'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
24-
opencensus.ext.azure.common.Options(
25-
instrumentation_key='00000000-0000-0000-0000-000000000000',
26-
timeout=29.9,
27-
))''',
24+
instrumentation_key='00000000-0000-0000-0000-000000000000',
25+
timeout=29.9,
26+
)''',
2827
},
2928
}
3029
middleware = FlaskMiddleware(app)

contrib/opencensus-ext-azure/examples/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
from flask import Flask
1616
import requests
1717

18-
from opencensus.trace import config_integration
1918
from opencensus.ext.azure.trace_exporter import AzureExporter
2019
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
20+
from opencensus.trace import config_integration
21+
from opencensus.trace.samplers import ProbabilitySampler
2122

2223
# TODO: you need to specify the instrumentation key in the
2324
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
2425
app = Flask(__name__)
25-
middleware = FlaskMiddleware(app, exporter=AzureExporter())
26+
middleware = FlaskMiddleware(
27+
app,
28+
exporter=AzureExporter(),
29+
sampler=ProbabilitySampler(rate=1.0),
30+
)
2631

2732

2833
@app.route('/')

contrib/opencensus-ext-azure/examples/simple.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# limitations under the License.
1414

1515
from opencensus.ext.azure.trace_exporter import AzureExporter
16-
from opencensus.trace import tracer as tracer_module
16+
from opencensus.trace.samplers import ProbabilitySampler
17+
from opencensus.trace.tracer import Tracer
1718

1819
# TODO: you need to specify the instrumentation key in the
1920
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
20-
tracer = tracer_module.Tracer(exporter=AzureExporter())
21+
tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))
2122

2223
with tracer.span(name='foo'):
2324
print('Hello, World!')

contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class AzureExporter(base_exporter.Exporter):
4141
:param options: Options for the exporter. Defaults to None.
4242
"""
4343

44-
def __init__(self, options=None):
45-
self.options = options or Options()
44+
def __init__(self, **options):
45+
self.options = Options(**options)
4646
if not self.options.instrumentation_key:
4747
raise ValueError('The instrumentation_key is not provided.')
4848
self.storage = LocalFileStorage(

contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import unittest
2020

2121
from opencensus.ext.azure import trace_exporter
22-
from opencensus.ext.azure.common import Options
2322

2423
TEST_FOLDER = os.path.abspath('.test.exporter')
2524

@@ -40,17 +39,16 @@ def func(*_args, **_kwargs):
4039

4140
class TestAzureExporter(unittest.TestCase):
4241
def test_ctor(self):
42+
from opencensus.ext.azure.common import Options
4343
instrumentation_key = Options.prototype.instrumentation_key
4444
Options.prototype.instrumentation_key = None
4545
self.assertRaises(ValueError, lambda: trace_exporter.AzureExporter())
4646
Options.prototype.instrumentation_key = instrumentation_key
4747

4848
def test_export(self):
4949
exporter = trace_exporter.AzureExporter(
50-
Options(
51-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
52-
storage_path=os.path.join(TEST_FOLDER, 'foo'),
53-
),
50+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
51+
storage_path=os.path.join(TEST_FOLDER, 'foo'),
5452
)
5553
exporter.transport = MockTransport()
5654
exporter.export(None)
@@ -59,10 +57,8 @@ def test_export(self):
5957
@mock.patch('requests.post', return_value=mock.Mock())
6058
def test_emit(self, request_mock):
6159
exporter = trace_exporter.AzureExporter(
62-
Options(
63-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
64-
storage_path=os.path.join(TEST_FOLDER, 'foo'),
65-
),
60+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
61+
storage_path=os.path.join(TEST_FOLDER, 'foo'),
6662
)
6763
exporter.transport = MockTransport()
6864
exporter.emit([])
@@ -74,18 +70,15 @@ def test_emit(self, request_mock):
7470
self.assertIsNone(exporter.storage.get())
7571

7672
def test_span_data_to_envelope(self):
77-
from opencensus.ext.azure.common import Options
7873
from opencensus.trace.span import SpanKind
7974
from opencensus.trace.span_context import SpanContext
8075
from opencensus.trace.span_data import SpanData
8176
from opencensus.trace.trace_options import TraceOptions
8277
from opencensus.trace.tracestate import Tracestate
8378

8479
exporter = trace_exporter.AzureExporter(
85-
Options(
86-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
87-
storage_path=os.path.join(TEST_FOLDER, 'bar'),
88-
),
80+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
81+
storage_path=os.path.join(TEST_FOLDER, 'bar'),
8982
)
9083

9184
# SpanKind.CLIENT HTTP
@@ -368,10 +361,8 @@ def test_span_data_to_envelope(self):
368361

369362
def test_transmission_nothing(self):
370363
exporter = trace_exporter.AzureExporter(
371-
Options(
372-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
373-
storage_path=os.path.join(TEST_FOLDER, 'baz'),
374-
),
364+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
365+
storage_path=os.path.join(TEST_FOLDER, 'baz'),
375366
)
376367

377368
with mock.patch('requests.post') as post:
@@ -380,10 +371,8 @@ def test_transmission_nothing(self):
380371

381372
def test_transmission_request_exception(self):
382373
exporter = trace_exporter.AzureExporter(
383-
Options(
384-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
385-
storage_path=os.path.join(TEST_FOLDER, 'request.exception'),
386-
),
374+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
375+
storage_path=os.path.join(TEST_FOLDER, 'request.exception'),
387376
)
388377
exporter.storage.put([1, 2, 3])
389378
with mock.patch('requests.post', throw(Exception)):
@@ -393,10 +382,8 @@ def test_transmission_request_exception(self):
393382

394383
def test_transmission_lease_failure(self):
395384
exporter = trace_exporter.AzureExporter(
396-
Options(
397-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
398-
storage_path=os.path.join(TEST_FOLDER, 'lease.failure'),
399-
),
385+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
386+
storage_path=os.path.join(TEST_FOLDER, 'lease.failure'),
400387
)
401388
exporter.storage.put([1, 2, 3])
402389
with mock.patch('opencensus.ext.azure.common.storage.LocalFileBlob.lease') as lease: # noqa: E501
@@ -406,10 +393,8 @@ def test_transmission_lease_failure(self):
406393

407394
def test_transmission_response_exception(self):
408395
exporter = trace_exporter.AzureExporter(
409-
Options(
410-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
411-
storage_path=os.path.join(TEST_FOLDER, 'response.exception'),
412-
),
396+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
397+
storage_path=os.path.join(TEST_FOLDER, 'response.exception'),
413398
)
414399
exporter.storage.put([1, 2, 3])
415400
with mock.patch('requests.post') as post:
@@ -421,10 +406,8 @@ def test_transmission_response_exception(self):
421406

422407
def test_transmission_200(self):
423408
exporter = trace_exporter.AzureExporter(
424-
Options(
425-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
426-
storage_path=os.path.join(TEST_FOLDER, '200'),
427-
),
409+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
410+
storage_path=os.path.join(TEST_FOLDER, '200'),
428411
)
429412
exporter.storage.put([1, 2, 3])
430413
exporter.storage.put([1, 2, 3])
@@ -436,10 +419,8 @@ def test_transmission_200(self):
436419

437420
def test_transmission_206(self):
438421
exporter = trace_exporter.AzureExporter(
439-
Options(
440-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
441-
storage_path=os.path.join(TEST_FOLDER, '206'),
442-
),
422+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
423+
storage_path=os.path.join(TEST_FOLDER, '206'),
443424
)
444425
exporter.storage.put([1, 2, 3])
445426
with mock.patch('requests.post') as post:
@@ -450,10 +431,8 @@ def test_transmission_206(self):
450431

451432
def test_transmission_206_500(self):
452433
exporter = trace_exporter.AzureExporter(
453-
Options(
454-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
455-
storage_path=os.path.join(TEST_FOLDER, '206.500'),
456-
),
434+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
435+
storage_path=os.path.join(TEST_FOLDER, '206.500'),
457436
)
458437
exporter.storage.put([1, 2, 3, 4, 5])
459438
with mock.patch('requests.post') as post:
@@ -479,10 +458,8 @@ def test_transmission_206_500(self):
479458

480459
def test_transmission_206_nothing_to_retry(self):
481460
exporter = trace_exporter.AzureExporter(
482-
Options(
483-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
484-
storage_path=os.path.join(TEST_FOLDER, 'nothing.to.retry'),
485-
),
461+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
462+
storage_path=os.path.join(TEST_FOLDER, 'nothing.to.retry'),
486463
)
487464
exporter.storage.put([1, 2, 3])
488465
with mock.patch('requests.post') as post:
@@ -502,10 +479,8 @@ def test_transmission_206_nothing_to_retry(self):
502479

503480
def test_transmission_206_bogus(self):
504481
exporter = trace_exporter.AzureExporter(
505-
Options(
506-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
507-
storage_path=os.path.join(TEST_FOLDER, '206.bogus'),
508-
),
482+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
483+
storage_path=os.path.join(TEST_FOLDER, '206.bogus'),
509484
)
510485
exporter.storage.put([1, 2, 3, 4, 5])
511486
with mock.patch('requests.post') as post:
@@ -525,10 +500,8 @@ def test_transmission_206_bogus(self):
525500

526501
def test_transmission_400(self):
527502
exporter = trace_exporter.AzureExporter(
528-
Options(
529-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
530-
storage_path=os.path.join(TEST_FOLDER, '400'),
531-
),
503+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
504+
storage_path=os.path.join(TEST_FOLDER, '400'),
532505
)
533506
exporter.storage.put([1, 2, 3])
534507
with mock.patch('requests.post') as post:
@@ -538,10 +511,8 @@ def test_transmission_400(self):
538511

539512
def test_transmission_500(self):
540513
exporter = trace_exporter.AzureExporter(
541-
Options(
542-
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
543-
storage_path=os.path.join(TEST_FOLDER, '500'),
544-
),
514+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
515+
storage_path=os.path.join(TEST_FOLDER, '500'),
545516
)
546517
exporter.storage.put([1, 2, 3])
547518
with mock.patch('requests.post') as post:

0 commit comments

Comments
 (0)