Skip to content

Commit

Permalink
Metrics export pipeline + metrics stdout exporter (open-telemetry#341)
Browse files Browse the repository at this point in the history
Initial implementation of the end-to-end metrics pipeline.
  • Loading branch information
lzchen authored and toumorokoshi committed Feb 17, 2020
1 parent 9ca7b0d commit e4ccc2f
Show file tree
Hide file tree
Showing 23 changed files with 1,052 additions and 170 deletions.
7 changes: 7 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.aggregate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.metrics.export.aggregate
==========================================

.. automodule:: opentelemetry.sdk.metrics.export.aggregate
:members:
:undoc-members:
:show-inheritance:
11 changes: 11 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.batcher.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
opentelemetry.sdk.metrics.export.batcher
==========================================

.. toctree::

opentelemetry.sdk.metrics.export

.. automodule:: opentelemetry.sdk.metrics.export.batcher
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.metrics.export
==========================================

.. automodule:: opentelemetry.sdk.metrics.export
:members:
:undoc-members:
:show-inheritance:
8 changes: 8 additions & 0 deletions docs/opentelemetry.sdk.metrics.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
opentelemetry.sdk.metrics package
==========================================

Submodules
----------

.. toctree::

opentelemetry.sdk.metrics.export.aggregate
opentelemetry.sdk.metrics.export.batcher

.. automodule:: opentelemetry.sdk.metrics
:members:
:undoc-members:
Expand Down
69 changes: 69 additions & 0 deletions examples/metrics/record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.
#
"""
This module serves as an example for a simple application using metrics.
It demonstrates the different ways you can record metrics via the meter.
"""
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_implementation(lambda _: Meter())
meter = metrics.meter()
# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)

# Example to show how to record using the meter
counter = meter.create_metric(
"requests", "number of requests", 1, int, Counter, ("environment",)
)

counter2 = meter.create_metric(
"clicks", "number of clicks", 1, int, Counter, ("environment",)
)

# Labelsets are used to identify key-values that are associated with a specific
# metric that you want to record. These are useful for pre-aggregation and can
# be used to store custom dimensions pertaining to a metric

# The meter takes a dictionary of key value pairs
label_set = meter.get_label_set({"environment": "staging"})

# Handle usage
# You can record metrics with metric handles. Handles are created by passing in
# a labelset. A handle is essentially metric data that corresponds to a specific
# set of labels. Therefore, getting a handle using the same set of labels will
# yield the same metric handle.
counter_handle = counter.get_handle(label_set)
counter_handle.add(100)

# Direct metric usage
# You can record metrics directly using the metric instrument. You pass in a
# labelset that you would like to record for.
counter.add(25, label_set)

# Record batch usage
# You can record metrics in a batch by passing in a labelset and a sequence of
# (metric, value) pairs. The value would be recorded for each metric using the
# specified labelset for each.
meter.record_batch(label_set, [(counter, 50), (counter2, 70)])
time.sleep(100)
72 changes: 72 additions & 0 deletions examples/metrics/stateful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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.
#
"""
This module serves as an example for a simple application using metrics
Examples show how to recording affects the collection of metrics to be exported
"""
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Batcher used to collect all created metrics from meter ready for exporting
# Pass in true/false to indicate whether the batcher is stateful. True
# indicates the batcher computes checkpoints from over the process lifetime.
# False indicates the batcher computes checkpoints which describe the updates
# of a single collection period (deltas)
batcher = UngroupedBatcher(True)
# If a batcher is not provded, a default batcher is used
# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
meter = metrics.meter()
# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)

counter = meter.create_metric(
"requests", "number of requests", 1, int, Counter, ("environment",)
)

counter2 = meter.create_metric(
"clicks", "number of clicks", 1, int, Counter, ("environment",)
)

# Labelsets are used to identify key-values that are associated with a specific
# metric that you want to record. These are useful for pre-aggregation and can
# be used to store custom dimensions pertaining to a metric
label_set = meter.get_label_set({"environment": "staging"})
label_set2 = meter.get_label_set({"environment": "testing"})

counter.add(25, label_set)
# We sleep for 5 seconds, exported value should be 25
time.sleep(5)

counter.add(50, label_set)
# exported value should be 75
time.sleep(5)

counter.add(35, label_set2)
# should be two exported values 75 and 35, one for each labelset
time.sleep(5)

counter2.add(5, label_set)
# should be three exported values, labelsets can be reused for different
# metrics but will be recorded seperately, 75, 35 and 5
time.sleep(5)
57 changes: 57 additions & 0 deletions examples/metrics/stateless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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.
#
"""
This module serves as an example for a simple application using metrics
Examples show how to recording affects the collection of metrics to be exported
"""
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Batcher used to collect all created metrics from meter ready for exporting
# Pass in false for non-stateful batcher. Indicates the batcher computes
# checkpoints which describe the updates of a single collection period (deltas)
batcher = UngroupedBatcher(False)
# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
meter = metrics.meter()
# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)

counter = meter.create_metric(
"requests", "number of requests", 1, int, Counter, ("environment",)
)

# Labelsets are used to identify key-values that are associated with a specific
# metric that you want to record. These are useful for pre-aggregation and can
# be used to store custom dimensions pertaining to a metric
label_set = meter.get_label_set({"environment": "staging"})

counter.add(25, label_set)
# We sleep for 5 seconds, exported value should be 25
time.sleep(5)

counter.add(50, label_set)
# exported value should be 50 due to non-stateful batcher
time.sleep(20)

# Following exported values would be 0
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

metrics.set_preferred_meter_implementation(lambda _: Meter())
batcher = UngroupedBatcher(True)
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
meter = metrics.meter()
counter = meter.create_metric(
"available memory",
Expand All @@ -33,14 +37,14 @@
label_set = meter.get_label_set({"environment": "staging"})

# Direct metric usage
counter.add(label_set, 25)
counter.add(25, label_set)

# Handle usage
counter_handle = counter.get_handle(label_set)
counter_handle.add(100)

# Record batch usage
meter.record_batch(label_set, [(counter, 50)])
print(counter_handle.data)

# TODO: exporters
exporter = ConsoleMetricsExporter()
controller = PushController(meter, exporter, 5)
4 changes: 2 additions & 2 deletions ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_translate_to_jaeger(self):
vLong=StatusCanonicalCode.OK.value,
),
jaeger.Tag(
key="status.message", vType=jaeger.TagType.STRING, vStr=None,
key="status.message", vType=jaeger.TagType.STRING, vStr=None
),
jaeger.Tag(
key="span.kind",
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_translate_to_jaeger(self):
vStr=trace_api.SpanKind.CLIENT.name,
),
jaeger.Tag(
key="error", vType=jaeger.TagType.BOOL, vBool=True,
key="error", vType=jaeger.TagType.BOOL, vBool=True
),
],
references=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ def export(self, spans: Sequence[Span]) -> SpanExportResult:

def _translate_to_zipkin(self, spans: Sequence[Span]):

local_endpoint = {
"serviceName": self.service_name,
"port": self.port,
}
local_endpoint = {"serviceName": self.service_name, "port": self.port}

if self.ipv4 is not None:
local_endpoint["ipv4"] = self.ipv4
Expand Down
9 changes: 2 additions & 7 deletions ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def test_export(self):
)

span_context = trace_api.SpanContext(
trace_id,
span_id,
trace_options=TraceOptions(TraceOptions.SAMPLED),
trace_id, span_id, trace_options=TraceOptions(TraceOptions.SAMPLED)
)
parent_context = trace_api.SpanContext(trace_id, parent_id)
other_context = trace_api.SpanContext(trace_id, other_id)
Expand Down Expand Up @@ -168,10 +166,7 @@ def test_export(self):
otel_spans[2].end(end_time=end_times[2])

service_name = "test-service"
local_endpoint = {
"serviceName": service_name,
"port": 9411,
}
local_endpoint = {"serviceName": service_name, "port": 9411}

exporter = ZipkinSpanExporter(service_name)
expected = [
Expand Down
Loading

0 comments on commit e4ccc2f

Please sign in to comment.