Skip to content

Commit

Permalink
Release for v0.7.10 (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jun 29, 2020
1 parent 8561909 commit a51cdb9
Show file tree
Hide file tree
Showing 24 changed files with 2,693 additions and 2,749 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

## 0.7.10
Released 2020-06-29

- Updated `azure` module
([#903](https://github.com/census-instrumentation/opencensus-python/pull/903),
[#925](https://github.com/census-instrumentation/opencensus-python/pull/925))

- Updated `stackdriver` module
([#919](https://github.com/census-instrumentation/opencensus-python/pull/919))

## 0.7.9
Released 2020-06-17

Expand Down
5 changes: 5 additions & 0 deletions context/opencensus-context/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

## 0.1.2
Released 2020-06-29

- Release source distribution

## 0.1.1
Released 2019-05-31

Expand Down
2 changes: 1 addition & 1 deletion context/opencensus-context/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = '0.1.1'
__version__ = '0.1.2'
8 changes: 8 additions & 0 deletions contrib/opencensus-ext-azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

## 1.0.4
Released 2020-06-29

- Remove dependency rate from standard metrics
([#903](https://github.com/census-instrumentation/opencensus-python/pull/903))
- Implement customEvents using AzureEventHandler
([#925](https://github.com/census-instrumentation/opencensus-python/pull/925))

## 1.0.3
Released 2020-06-17

Expand Down
26 changes: 20 additions & 6 deletions contrib/opencensus-ext-azure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ Modifying Logs
logger.addHandler(handler)
logger.warning('Hello, World!')
Events
######

You can send `customEvent` telemetry in exactly the same way you would send `trace` telemetry except using the `AzureEventHandler` instead.

.. code:: python
import logging
from opencensus.ext.azure.log_exporter import AzureEventHandler
logger = logging.getLogger(__name__)
logger.addHandler(AzureEventHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
logger.setLevel(logging.INFO)
logger.info('Hello, World!')
Metrics
~~~~~~~
Expand Down Expand Up @@ -178,10 +193,10 @@ The **Azure Monitor Metrics Exporter** allows you to export metrics to `Azure Mo
if __name__ == "__main__":
main()
Standard Metrics
################
Performance counters
####################

The exporter also includes a set of standard metrics that are exported to Azure Monitor by default.
The exporter also includes a set of performance counters that are exported to Azure Monitor by default.

.. code:: python
Expand All @@ -191,7 +206,7 @@ The exporter also includes a set of standard metrics that are exported to Azure
from opencensus.ext.azure import metrics_exporter
def main():
# All you need is the next line. You can disable standard metrics by
# All you need is the next line. You can disable performance counters by
# passing in enable_standard_metrics=False into the constructor of
# new_metrics_exporter()
_exporter = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey=<your-instrumentation-key-here>')
Expand All @@ -205,13 +220,12 @@ The exporter also includes a set of standard metrics that are exported to Azure
if __name__ == "__main__":
main()
Below is a list of standard metrics that are currently available:
Below is a list of performance counters that are currently available:

- Available Memory (bytes)
- CPU Processor Time (percentage)
- Incoming Request Rate (per second)
- Incoming Request Average Execution Time (milliseconds)
- Outgoing Request Rate (per second)
- Process CPU Usage (percentage)
- Process Private Bytes (bytes)

Expand Down
2 changes: 2 additions & 0 deletions contrib/opencensus-ext-azure/examples/logs/correlated.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
with tracer.span(name='test'):
logger.warning('In the span')
logger.warning('After the span')

input("...")
1 change: 1 addition & 0 deletions contrib/opencensus-ext-azure/examples/logs/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ def main():

if __name__ == '__main__':
main()
input("...")
27 changes: 27 additions & 0 deletions contrib/opencensus-ext-azure/examples/logs/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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 logging

from opencensus.ext.azure.log_exporter import AzureEventHandler

logger = logging.getLogger(__name__)
# TODO: you need to specify the instrumentation key in a connection string
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
# environment variable.
logger.addHandler(AzureEventHandler())
logger.setLevel(logging.INFO)
logger.info('Hello, World!')

input("...")
2 changes: 2 additions & 0 deletions contrib/opencensus-ext-azure/examples/logs/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
result = 1 / 0 # generate a ZeroDivisionError
except Exception:
logger.exception('Captured an exception.', extra=properties)

input("...")
2 changes: 2 additions & 0 deletions contrib/opencensus-ext-azure/examples/logs/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
# environment variable.
logger.addHandler(AzureLogHandler())
logger.warning('Hello, World!')

input("...")
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = '1.0.3'
__version__ = '1.0.4'
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from opencensus.ext.azure.common.protocol import (
Data,
Envelope,
Event,
ExceptionData,
Message,
)
Expand All @@ -33,17 +34,51 @@

logger = logging.getLogger(__name__)

__all__ = ['AzureLogHandler']
__all__ = ['AzureEventHandler', 'AzureLogHandler']


class BaseLogHandler(logging.Handler):
def __init__(self):

def __init__(self, **options):
super(BaseLogHandler, self).__init__()
self.options = Options(**options)
utils.validate_instrumentation_key(self.options.instrumentation_key)
if not 0 <= self.options.logging_sampling_rate <= 1:
raise ValueError('Sampling must be in the range: [0,1]')
self.export_interval = self.options.export_interval
self.max_batch_size = self.options.max_batch_size
self.storage = LocalFileStorage(
path=self.options.storage_path,
max_size=self.options.storage_max_size,
maintenance_period=self.options.storage_maintenance_period,
retention_period=self.options.storage_retention_period,
)
self._telemetry_processors = []
self.addFilter(SamplingFilter(self.options.logging_sampling_rate))
self._queue = Queue(capacity=8192) # TODO: make this configurable
self._worker = Worker(self._queue, self)
self._worker.start()

def _export(self, batch, event=None): # pragma: NO COVER
try:
if batch:
envelopes = [self.log_record_to_envelope(x) for x in batch]
envelopes = self.apply_telemetry_processors(envelopes)
result = self._transmit(envelopes)
if result > 0:
self.storage.put(envelopes, result)
if event:
if isinstance(event, QueueExitEvent):
self._transmit_from_storage() # send files before exit
return
if len(batch) < self.options.max_batch_size:
self._transmit_from_storage()
finally:
if event:
event.set()

def close(self):
self.storage.close()
self._worker.stop()

def createLock(self):
Expand All @@ -52,14 +87,7 @@ def createLock(self):
def emit(self, record):
self._queue.put(record, block=False)

def _export(self, batch, event=None):
try:
return self.export(batch)
finally:
if event:
event.set()

def export(self, batch):
def log_record_to_envelope(self, record):
raise NotImplementedError # pragma: NO COVER

def flush(self, timeout=None):
Expand All @@ -79,7 +107,7 @@ def __init__(self, src, dst):

def run(self):
# Indicate that this thread is an exporter thread.
# Used to suppress tracking of requests in this thread.
# Used to suppress tracking of requests in this thread
execution_context.set_is_exporter(True)
src = self._src
dst = self._dst
Expand Down Expand Up @@ -121,74 +149,18 @@ def filter(self, record):


class AzureLogHandler(TransportMixin, ProcessorMixin, BaseLogHandler):
"""Handler for logging to Microsoft Azure Monitor.
:param options: Options for the log handler.
"""

def __init__(self, **options):
self.options = Options(**options)
utils.validate_instrumentation_key(self.options.instrumentation_key)
if not 0 <= self.options.logging_sampling_rate <= 1:
raise ValueError('Sampling must be in the range: [0,1]')
self.export_interval = self.options.export_interval
self.max_batch_size = self.options.max_batch_size
self.storage = LocalFileStorage(
path=self.options.storage_path,
max_size=self.options.storage_max_size,
maintenance_period=self.options.storage_maintenance_period,
retention_period=self.options.storage_retention_period,
)
self._telemetry_processors = []
super(AzureLogHandler, self).__init__()
self.addFilter(SamplingFilter(self.options.logging_sampling_rate))

def close(self):
self.storage.close()
super(AzureLogHandler, self).close()

def _export(self, batch, event=None): # pragma: NO COVER
try:
if batch:
envelopes = [self.log_record_to_envelope(x) for x in batch]
envelopes = self.apply_telemetry_processors(envelopes)
result = self._transmit(envelopes)
if result > 0:
self.storage.put(envelopes, result)
if event:
if isinstance(event, QueueExitEvent):
self._transmit_from_storage() # send files before exit
return
if len(batch) < self.options.max_batch_size:
self._transmit_from_storage()
finally:
if event:
event.set()
"""Handler for logging to Microsoft Azure Monitor."""

def log_record_to_envelope(self, record):
envelope = Envelope(
iKey=self.options.instrumentation_key,
tags=dict(utils.azure_monitor_context),
time=utils.timestamp_to_iso_str(record.created),
)
envelope = create_envelope(self.options.instrumentation_key, record)

envelope.tags['ai.operation.id'] = getattr(
record,
'traceId',
'00000000000000000000000000000000',
)
envelope.tags['ai.operation.parentId'] = '|{}.{}.'.format(
envelope.tags['ai.operation.id'],
getattr(record, 'spanId', '0000000000000000'),
)
properties = {
'process': record.processName,
'module': record.module,
'fileName': record.pathname,
'lineNumber': record.lineno,
'level': record.levelname,
}

if (hasattr(record, 'custom_dimensions') and
isinstance(record.custom_dimensions, dict)):
properties.update(record.custom_dimensions)
Expand Down Expand Up @@ -230,3 +202,43 @@ def log_record_to_envelope(self, record):
)
envelope.data = Data(baseData=data, baseType='MessageData')
return envelope


class AzureEventHandler(TransportMixin, ProcessorMixin, BaseLogHandler):
"""Handler for sending custom events to Microsoft Azure Monitor."""

def log_record_to_envelope(self, record):
envelope = create_envelope(self.options.instrumentation_key, record)

properties = {}
if (hasattr(record, 'custom_dimensions') and
isinstance(record.custom_dimensions, dict)):
properties.update(record.custom_dimensions)

envelope.name = 'Microsoft.ApplicationInsights.Event'
data = Event(
name=self.format(record),
properties=properties,
)
envelope.data = Data(baseData=data, baseType='EventData')

return envelope


def create_envelope(instrumentation_key, record):
envelope = Envelope(
iKey=instrumentation_key,
tags=dict(utils.azure_monitor_context),
time=utils.timestamp_to_iso_str(record.created),
)
envelope.tags['ai.operation.id'] = getattr(
record,
'traceId',
'00000000000000000000000000000000',
)
envelope.tags['ai.operation.parentId'] = '|{}.{}.'.format(
envelope.tags['ai.operation.id'],
getattr(record, 'spanId', '0000000000000000'),
)

return envelope
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
from opencensus.ext.azure.metrics_exporter.standard_metrics.cpu import (
ProcessorTimeMetric,
)
from opencensus.ext.azure.metrics_exporter.standard_metrics.http_dependency import (
DependencyRateMetric,
)
from opencensus.ext.azure.metrics_exporter.standard_metrics.http_requests import (
from opencensus.ext.azure.metrics_exporter.standard_metrics.http_requests import ( # noqa E501
RequestsAvgExecutionMetric,
RequestsRateMetric,
)
Expand All @@ -34,7 +31,6 @@

# List of standard metrics to track
STANDARD_METRICS = [AvailableMemoryMetric,
DependencyRateMetric,
ProcessCPUMetric,
ProcessMemoryMetric,
ProcessorTimeMetric,
Expand Down
Loading

0 comments on commit a51cdb9

Please sign in to comment.