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

Commit 3afd7bc

Browse files
authored
Release for Datadog exporters and some azure exporter changes (#818)
1 parent 0787052 commit 3afd7bc

File tree

25 files changed

+1135
-35
lines changed

25 files changed

+1135
-35
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,14 @@ Trace Exporter
226226
--------------
227227

228228
- `Azure`_
229+
- `Datadog`_
229230
- `Jaeger`_
230231
- `OCAgent`_
231232
- `Stackdriver`_
232233
- `Zipkin`_
233234

234235
.. _Azure: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure
236+
.. _Datadog: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-datadog
235237
.. _Django: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django
236238
.. _Flask: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-flask
237239
.. _gevent: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-gevent

contrib/opencensus-ext-azure/CHANGELOG.md

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

33
## Unreleased
44

5+
## 1.0.1
6+
Released 2019-11-18
7+
8+
- Validate instrumentation key in Azure Exporters
9+
([#789](https://github.com/census-instrumentation/opencensus-python/pull/789))
10+
511
## 1.0.0
612
Released 2019-09-30
713

contrib/opencensus-ext-azure/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ This example shows how to send a warning level log to Azure Monitor.
3737
logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
3838
logger.warning('Hello, World!')
3939
40-
* Alternatively, you can specify your `connection string` in an environment variable ``APPLICATIONINSIGHTS_CONNECTION_STRING``.
4140
4241
You can enrich the logs with trace IDs and span IDs by using the `logging integration <../opencensus-ext-logging>`_.
4342

4443
* 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>`_.
44+
* Install the `logging integration package <../opencensus-ext-logging>`_ using ``pip install opencensus-ext-logging``.
4545
* Place your instrumentation key in a `connection string` and directly into your code.
4646
* Alternatively, you can specify your `connection string` in an environment variable ``APPLICATIONINSIGHTS_CONNECTION_STRING``.
4747

@@ -71,7 +71,7 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
7171
logger.warning('Before the span')
7272
with tracer.span(name='test'):
7373
logger.warning('In the span')
74-
logger.warning('After the span')s
74+
logger.warning('After the span')
7575
7676
Metrics
7777
~~~~~~~

contrib/opencensus-ext-azure/opencensus/ext/azure/common/utils.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
import locale
1717
import os
1818
import platform
19+
import re
1920
import sys
2021

22+
from opencensus.common.utils import timestamp_to_microseconds, to_iso_str
23+
from opencensus.common.version import __version__ as opencensus_version
24+
from opencensus.ext.azure.common.version import __version__ as ext_version
25+
2126
try:
2227
from urllib.parse import urlparse
2328
except ImportError:
2429
from urlparse import urlparse
2530

26-
from opencensus.common.version import __version__ as opencensus_version
27-
from opencensus.common.utils import timestamp_to_microseconds
28-
from opencensus.common.utils import to_iso_str
29-
from opencensus.ext.azure.common.version import __version__ as ext_version
3031

3132
azure_monitor_context = {
3233
'ai.cloud.role': os.path.basename(sys.argv[0]) or 'Python Application',
@@ -65,3 +66,27 @@ def timestamp_to_iso_str(timestamp):
6566

6667
def url_to_dependency_name(url):
6768
return urlparse(url).netloc
69+
70+
71+
# Validate UUID format
72+
# Specs taken from https://tools.ietf.org/html/rfc4122
73+
uuid_regex_pattern = re.compile('^[0-9a-f]{8}-'
74+
'[0-9a-f]{4}-'
75+
'[1-5][0-9a-f]{3}-'
76+
'[89ab][0-9a-f]{3}-'
77+
'[0-9a-f]{12}$')
78+
79+
80+
def validate_instrumentation_key(instrumentation_key):
81+
"""Validates the instrumentation key used for Azure Monitor.
82+
83+
An instrumentation key cannot be null or empty. An instrumentation key
84+
is valid for Azure Monitor only if it is a valid UUID.
85+
86+
:param instrumentation_key: The instrumentation key to validate
87+
"""
88+
if not instrumentation_key:
89+
raise ValueError("Instrumentation key cannot be none or empty.")
90+
match = uuid_regex_pattern.match(instrumentation_key)
91+
if not match:
92+
raise ValueError("Invalid instrumentation key.")

contrib/opencensus-ext-azure/opencensus/ext/azure/common/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '1.0.0'
15+
__version__ = '1.0.1'

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
import time
1818
import traceback
1919

20-
from opencensus.common.schedule import Queue
21-
from opencensus.common.schedule import QueueExitEvent
22-
from opencensus.common.schedule import QueueEvent
23-
from opencensus.ext.azure.common import Options
24-
from opencensus.ext.azure.common import utils
25-
from opencensus.ext.azure.common.protocol import Data
26-
from opencensus.ext.azure.common.protocol import Envelope
27-
from opencensus.ext.azure.common.protocol import ExceptionData
28-
from opencensus.ext.azure.common.protocol import Message
20+
from opencensus.common.schedule import Queue, QueueEvent, QueueExitEvent
21+
from opencensus.ext.azure.common import Options, utils
22+
from opencensus.ext.azure.common.protocol import (
23+
Data,
24+
Envelope,
25+
ExceptionData,
26+
Message,
27+
)
2928
from opencensus.ext.azure.common.storage import LocalFileStorage
3029
from opencensus.ext.azure.common.transport import TransportMixin
3130
from opencensus.trace import execution_context
@@ -116,8 +115,7 @@ class AzureLogHandler(TransportMixin, BaseLogHandler):
116115

117116
def __init__(self, **options):
118117
self.options = Options(**options)
119-
if not self.options.instrumentation_key:
120-
raise ValueError('The instrumentation_key is not provided.')
118+
utils.validate_instrumentation_key(self.options.instrumentation_key)
121119
self.export_interval = self.options.export_interval
122120
self.max_batch_size = self.options.max_batch_size
123121
self.storage = LocalFileStorage(

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414

1515
import json
1616
import logging
17+
1718
import requests
1819

1920
from opencensus.common import utils as common_utils
20-
from opencensus.ext.azure.common import Options
21-
from opencensus.ext.azure.common import utils
22-
from opencensus.ext.azure.common.protocol import Data
23-
from opencensus.ext.azure.common.protocol import DataPoint
24-
from opencensus.ext.azure.common.protocol import Envelope
25-
from opencensus.ext.azure.common.protocol import MetricData
21+
from opencensus.ext.azure.common import Options, utils
22+
from opencensus.ext.azure.common.protocol import (
23+
Data,
24+
DataPoint,
25+
Envelope,
26+
MetricData,
27+
)
2628
from opencensus.ext.azure.metrics_exporter import standard_metrics
2729
from opencensus.metrics import transport
2830
from opencensus.metrics.export.metric_descriptor import MetricDescriptorType
@@ -40,8 +42,7 @@ def __init__(self, options=None):
4042
if options is None:
4143
options = Options()
4244
self.options = options
43-
if not self.options.instrumentation_key:
44-
raise ValueError('The instrumentation_key is not provided.')
45+
utils.validate_instrumentation_key(self.options.instrumentation_key)
4546
if self.options.max_batch_size <= 0:
4647
raise ValueError('Max batch size must be at least 1.')
4748
self.max_batch_size = self.options.max_batch_size

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import logging
1616

1717
from opencensus.common.schedule import QueueExitEvent
18-
from opencensus.ext.azure.common import Options
19-
from opencensus.ext.azure.common import utils
18+
from opencensus.ext.azure.common import Options, utils
2019
from opencensus.ext.azure.common.exporter import BaseExporter
21-
from opencensus.ext.azure.common.protocol import Data
22-
from opencensus.ext.azure.common.protocol import Envelope
23-
from opencensus.ext.azure.common.protocol import RemoteDependency
24-
from opencensus.ext.azure.common.protocol import Request
20+
from opencensus.ext.azure.common.protocol import (
21+
Data,
22+
Envelope,
23+
RemoteDependency,
24+
Request,
25+
)
2526
from opencensus.ext.azure.common.storage import LocalFileStorage
2627
from opencensus.ext.azure.common.transport import TransportMixin
2728
from opencensus.trace.span import SpanKind
@@ -39,8 +40,7 @@ class AzureExporter(TransportMixin, BaseExporter):
3940

4041
def __init__(self, **options):
4142
self.options = Options(**options)
42-
if not self.options.instrumentation_key:
43-
raise ValueError('The instrumentation_key is not provided.')
43+
utils.validate_instrumentation_key(self.options.instrumentation_key)
4444
self.storage = LocalFileStorage(
4545
path=self.options.storage_path,
4646
max_size=self.options.storage_max_size,

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
1617
from opencensus.ext.azure.common import utils
1718

1819

@@ -42,3 +43,97 @@ def test_url_to_dependency_name(self):
4243
'https://www.wikipedia.org/wiki/Rabbit'
4344
),
4445
'www.wikipedia.org')
46+
47+
def test_validate_instrumentation_key(self):
48+
key = '1234abcd-5678-4efa-8abc-1234567890ab'
49+
self.assertIsNone(utils.validate_instrumentation_key(key))
50+
51+
def test_invalid_key_none(self):
52+
key = None
53+
self.assertRaises(ValueError,
54+
lambda: utils.validate_instrumentation_key(key))
55+
56+
def test_invalid_key_empty(self):
57+
key = ''
58+
self.assertRaises(ValueError,
59+
lambda: utils.validate_instrumentation_key(key))
60+
61+
def test_invalid_key_prefix(self):
62+
key = 'test1234abcd-5678-4efa-8abc-1234567890ab'
63+
self.assertRaises(ValueError,
64+
lambda: utils.validate_instrumentation_key(key))
65+
66+
def test_invalid_key_suffix(self):
67+
key = '1234abcd-5678-4efa-8abc-1234567890abtest'
68+
self.assertRaises(ValueError,
69+
lambda: utils.validate_instrumentation_key(key))
70+
71+
def test_invalid_key_length(self):
72+
key = '1234abcd-5678-4efa-8abc-12234567890ab'
73+
self.assertRaises(ValueError,
74+
lambda: utils.validate_instrumentation_key(key))
75+
76+
def test_invalid_key_dashes(self):
77+
key = '1234abcda5678-4efa-8abc-1234567890ab'
78+
self.assertRaises(ValueError,
79+
lambda: utils.validate_instrumentation_key(key))
80+
81+
def test_invalid_key_section1_length(self):
82+
key = '1234abcda-678-4efa-8abc-1234567890ab'
83+
self.assertRaises(ValueError,
84+
lambda: utils.validate_instrumentation_key(key))
85+
86+
def test_invalid_key_section2_length(self):
87+
key = '1234abcd-678-a4efa-8abc-1234567890ab'
88+
self.assertRaises(ValueError,
89+
lambda: utils.validate_instrumentation_key(key))
90+
91+
def test_invalid_key_section3_length(self):
92+
key = '1234abcd-6789-4ef-8cabc-1234567890ab'
93+
self.assertRaises(ValueError,
94+
lambda: utils.validate_instrumentation_key(key))
95+
96+
def test_invalid_key_section4_length(self):
97+
key = '1234abcd-678-4efa-8bc-11234567890ab'
98+
self.assertRaises(ValueError,
99+
lambda: utils.validate_instrumentation_key(key))
100+
101+
def test_invalid_key_section5_length(self):
102+
key = '234abcd-678-4efa-8abc-11234567890ab'
103+
self.assertRaises(ValueError,
104+
lambda: utils.validate_instrumentation_key(key))
105+
106+
def test_invalid_key_section1_hex(self):
107+
key = 'x234abcd-5678-4efa-8abc-1234567890ab'
108+
self.assertRaises(ValueError,
109+
lambda: utils.validate_instrumentation_key(key))
110+
111+
def test_invalid_key_section2_hex(self):
112+
key = '1234abcd-x678-4efa-8abc-1234567890ab'
113+
self.assertRaises(ValueError,
114+
lambda: utils.validate_instrumentation_key(key))
115+
116+
def test_invalid_key_section3_hex(self):
117+
key = '1234abcd-5678-4xfa-8abc-1234567890ab'
118+
self.assertRaises(ValueError,
119+
lambda: utils.validate_instrumentation_key(key))
120+
121+
def test_invalid_key_section4_hex(self):
122+
key = '1234abcd-5678-4xfa-8abc-1234567890ab'
123+
self.assertRaises(ValueError,
124+
lambda: utils.validate_instrumentation_key(key))
125+
126+
def test_invalid_key_section5_hex(self):
127+
key = '1234abcd-5678-4xfa-8abc-1234567890ab'
128+
self.assertRaises(ValueError,
129+
lambda: utils.validate_instrumentation_key(key))
130+
131+
def test_invalid_key_version(self):
132+
key = '1234abcd-5678-6efa-8abc-1234567890ab'
133+
self.assertRaises(ValueError,
134+
lambda: utils.validate_instrumentation_key(key))
135+
136+
def test_invalid_key_variant(self):
137+
key = '1234abcd-5678-4efa-2abc-1234567890ab'
138+
self.assertRaises(ValueError,
139+
lambda: utils.validate_instrumentation_key(key))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
## 0.1.0
6+
Released 2019-11-18
7+
8+
- Initial version
9+
([#793](https://github.com/census-instrumentation/opencensus-python/pull/793))

0 commit comments

Comments
 (0)