Skip to content

Implement Azure Monitor Metrics Exporter #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 94 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
6ca4274
Create functions
lzchen Jul 29, 2019
b23cec1
fix lint
lzchen Jul 31, 2019
981eece
Fix lint
lzchen Jul 31, 2019
8ea9709
fix typing
lzchen Jul 31, 2019
00b4f11
Remove options, constructors, seperate labels
lzchen Aug 6, 2019
34c87ce
Consistent naming for float and int
lzchen Aug 6, 2019
df8ae34
Abstract time series
lzchen Aug 6, 2019
a2561ac
Use ABC
lzchen Aug 6, 2019
1ece493
Fix typo
lzchen Aug 6, 2019
ce9268a
Fix docs
lzchen Aug 6, 2019
f5f9f01
seperate measure classes
lzchen Aug 8, 2019
74a1815
Add examples
lzchen Aug 8, 2019
0a0b8ee
fix lint
lzchen Aug 8, 2019
555bf50
Update to RFC 0003
lzchen Aug 14, 2019
d6b1113
Add spancontext, measurebatch
lzchen Aug 14, 2019
c819109
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Aug 15, 2019
18cfc71
Fix docs
lzchen Aug 15, 2019
f646555
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Aug 19, 2019
a44cb47
Fix comments
lzchen Aug 22, 2019
94eaad9
fix lint
lzchen Aug 22, 2019
fc251b2
fix lint
lzchen Aug 22, 2019
262f312
fix lint
lzchen Aug 22, 2019
66c0a56
skip examples
lzchen Aug 22, 2019
e2c4a7e
white space
lzchen Aug 22, 2019
2fb7646
fix spacing
lzchen Aug 22, 2019
eb711cb
fix imports
lzchen Aug 22, 2019
baa3a32
fix imports
lzchen Aug 22, 2019
5c30a9c
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 3, 2019
211b20c
LabelValues to str
lzchen Sep 3, 2019
bffe040
Black formatting
lzchen Sep 3, 2019
0759b9a
fix isort
lzchen Sep 3, 2019
44d62f8
Remove aggregation
lzchen Sep 12, 2019
c5ab2df
Fix names
lzchen Sep 12, 2019
50d2de5
Remove aggregation from docs
lzchen Sep 12, 2019
d79bc7d
Fix lint
lzchen Sep 12, 2019
7fb7cdf
metric changes
lzchen Sep 13, 2019
79322f5
Typing
lzchen Sep 13, 2019
db242f0
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 13, 2019
f9c7cc3
Fix lint
lzchen Sep 13, 2019
4465929
Fix lint
lzchen Sep 13, 2019
695e596
Add space
lzchen Sep 13, 2019
ee43e39
Fix lint
lzchen Sep 13, 2019
e59f7c4
fix comments
lzchen Sep 16, 2019
9946028
handle, recordbatch
lzchen Sep 16, 2019
e1a3101
docs
lzchen Sep 16, 2019
47d5209
Update recordbatch
lzchen Sep 16, 2019
2553b89
black
lzchen Sep 16, 2019
15c2894
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 19, 2019
ebcc567
Fix typo
lzchen Sep 19, 2019
41ee12c
remove ValueType
lzchen Sep 19, 2019
62862a2
fix lint
lzchen Sep 19, 2019
b99dd3f
sdk
lzchen Sep 19, 2019
dda2966
metrics
lzchen Sep 20, 2019
38e5130
example
lzchen Sep 20, 2019
006bd89
counter
lzchen Sep 21, 2019
ae94dea
Tests
lzchen Sep 22, 2019
eed2cce
merge
lzchen Sep 22, 2019
a16556e
Address comments
lzchen Sep 23, 2019
3b0d251
ADd tests
lzchen Sep 23, 2019
8f2a216
Fix typing and examples
lzchen Sep 24, 2019
4f4e385
black
lzchen Sep 24, 2019
c0f1d10
fix lint
lzchen Sep 24, 2019
bbdc9df
remove override
lzchen Sep 24, 2019
bc617c0
Fix tests
lzchen Sep 24, 2019
081556b
mypy
lzchen Sep 24, 2019
77650be
fix lint
lzchen Sep 24, 2019
d1b8c32
fix type
lzchen Sep 24, 2019
1a8a037
fix typing
lzchen Sep 24, 2019
5c58f93
fix tests
lzchen Sep 24, 2019
1886a7e
isort
lzchen Sep 24, 2019
90fbbbb
isort
lzchen Sep 24, 2019
f602d70
isort
lzchen Sep 24, 2019
3d6654b
isort
lzchen Sep 24, 2019
6cd6ede
noop
lzchen Sep 25, 2019
d8e9a64
lint
lzchen Sep 25, 2019
311b1c4
lint
lzchen Sep 25, 2019
8ea1bd7
fix tuple typing
lzchen Sep 26, 2019
417231d
Merge branch 'metrics' of https://github.com/lzchen/opentelemetry-pyt…
lzchen Sep 26, 2019
1090a6c
fix type
lzchen Sep 26, 2019
18e6a11
black
lzchen Sep 26, 2019
4b03cb7
address comments
lzchen Sep 26, 2019
cedcd1e
fix type
lzchen Sep 26, 2019
77854c9
fix lint
lzchen Sep 26, 2019
605c947
remove imports
lzchen Sep 26, 2019
031bd1f
default tests
lzchen Sep 26, 2019
071b25b
fix lint
lzchen Sep 26, 2019
5ee98a7
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 26, 2019
c9040dc
Merge branch 'azure' of https://github.com/open-telemetry/opentelemet…
lzchen Sep 26, 2019
1b5845b
exporter
lzchen Sep 27, 2019
00eaea3
usse sequence
lzchen Sep 27, 2019
554b7b1
remove ellipses
lzchen Sep 27, 2019
7668de4
remove ellipses
lzchen Sep 27, 2019
61bef29
black
lzchen Sep 27, 2019
fa67bc7
exporter
lzchen Sep 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions docs/opentelemetry.metrics.handle.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/opentelemetry.metrics.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
opentelemetry.metrics package
=============================

Submodules
----------

.. toctree::

opentelemetry.metrics.handle

Module contents
---------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@
# 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.
#
"""
This module serves as an example for a simple application using metrics
"""

# pylint: skip-file
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Meter

METER = metrics.Meter()
MEASURE = metrics.create_measure(
"idle_cpu_percentage",
"cpu idle over time",
"percentage",
metrics.ValueType.FLOAT,
["environment"],
metrics.set_preferred_meter_implementation(lambda _: Meter())
meter = metrics.meter()
counter = meter.create_metric(
"available memory",
"available memory",
"bytes",
int,
metrics.MetricKind.COUNTER,
("environment"),
)

# Metrics sent to some exporter
MEASURE_METRIC_TESTING = MEASURE.get_handle("Testing")
MEASURE_METRIC_STAGING = MEASURE.get_handle("Staging")
label_values = "staging"

counter_handle = counter.get_handle(label_values)

counter_handle.add(100)
meter.record_batch(label_values, [(counter, 50)])

# record individual measures
STATISTIC = 100
MEASURE_METRIC_STAGING.record(STATISTIC)
print(counter_handle.data)

# Batch recording
METER.record_batch([tuple(MEASURE_METRIC_STAGING, STATISTIC)])
# TODO: exporters
42 changes: 42 additions & 0 deletions ext/opentelemetry-ext-azure-monitor/examples/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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 opentelemetry import metrics
from opentelemetry.ext.azure_monitor import AzureMonitorMetricsExporter
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter

logging.basicConfig(level=logging.INFO)

metrics.set_preferred_meter_implementation(lambda T: Meter())
meter = metrics.meter()
exporter = AzureMonitorMetricsExporter()
console_exporter = ConsoleMetricsExporter()

counter = meter.create_metric(
"available memory",
"available memory",
"bytes",
int,
Counter,
("environment",),
)
label_values = ("staging",)
counter_handle = counter.get_handle(label_values)
counter_handle.add(100)

console_exporter.export([(counter, label_values)])
exporter.export([(counter, label_values)])
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
The opentelemetry-ext-azure-monitor package provides integration with
Microsoft Azure Monitor.
"""

from opentelemetry.ext.azure_monitor.metrics import AzureMonitorMetricsExporter
from opentelemetry.ext.azure_monitor.trace import AzureMonitorSpanExporter
from opentelemetry.ext.azure_monitor.version import __version__ # noqa

__all__ = ["AzureMonitorSpanExporter"]
__all__ = ["AzureMonitorMetricsExporter", "AzureMonitorSpanExporter"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2019, OpenTelemetry 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 json
import logging

import requests

from opentelemetry.ext.azure_monitor import protocol, util
from opentelemetry.sdk.metrics.export import MetricsExporter,\
MetricsExportResult


logger = logging.getLogger(__name__)


class AzureMonitorMetricsExporter(MetricsExporter):
def __init__(self, **options):
self.options = util.Options(**options)
if not self.options.instrumentation_key:
raise ValueError("The instrumentation_key is not provided.")

def export(self, metric_tuples):
envelopes = tuple(map(self.metric_tuple_to_envelope, metric_tuples))

try:
response = requests.post(
url=self.options.endpoint,
data=json.dumps(envelopes),
headers={
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
},
timeout=self.options.timeout,
)
except requests.RequestException as ex:
logger.warning("Transient client side error %s.", ex)
return MetricsExportResult.FAILED_RETRYABLE

text = "N/A"
data = None # noqa pylint: disable=unused-variable
try:
text = response.text
except Exception as ex: # noqa pylint: disable=broad-except
logger.warning("Error while reading response body %s.", ex)
else:
try:
data = json.loads(text) # noqa pylint: disable=unused-variable
except Exception: # noqa pylint: disable=broad-except
pass

if response.status_code == 200:
logger.info("Transmission succeeded: %s.", text)
return MetricsExportResult.SUCCESS

if response.status_code in (
206, # Partial Content
429, # Too Many Requests
500, # Internal Server Error
503, # Service Unavailable
):
return MetricsExportResult.FAILED_RETRYABLE

return MetricsExportResult.FAILED_NOT_RETRYABLE

def metric_tuple_to_envelope(self, metric_tuple):
metric = metric_tuple[0]
label_values = metric_tuple[1]
handle = metric.get_handle(label_values)
envelope = protocol.Envelope(
iKey=self.options.instrumentation_key,
tags=dict(util.azure_monitor_context),
time=handle.time_stamp.isoformat(),
)
envelope.name = "Microsoft.ApplicationInsights.Metric"
# label_keys and label_values assumed to have the same length
properties = {metric.label_keys[idx]: label_values[idx]
for idx, value in enumerate(label_values, start=0)}
data_point = protocol.DataPoint(ns=metric.name,
name=metric.name,
value=handle.data)
data = protocol.MetricData(
metrics=[data_point],
properties=properties
)
envelope.data = protocol.Data(baseData=data, baseType="MetricData")
return envelope
Loading