Skip to content

Commit

Permalink
Add first test focusing on exemplar
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Aug 14, 2024
1 parent 2b7793a commit 6a25608
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
10 changes: 10 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
from opentelemetry.sdk.metrics._internal import Meter, MeterProvider
from opentelemetry.sdk.metrics._internal.exceptions import MetricsTimeoutError
from opentelemetry.sdk.metrics._internal.exemplar import (
AlignedHistogramBucketExemplarReservoir,
AlwaysOnExemplarFilter,
AlwaysOffExemplarFilter,
ExemplarFilter,
ExemplarReservoir,
SimpleFixedSizeExemplarReservoir,
TraceBasedExemplarFilter,
)
from opentelemetry.sdk.metrics._internal.instrument import Counter
from opentelemetry.sdk.metrics._internal.instrument import Gauge as _Gauge
Expand All @@ -30,6 +35,9 @@
)

__all__ = [
"AlignedHistogramBucketExemplarReservoir",
"AlwaysOnExemplarFilter",
"AlwaysOffExemplarFilter",
"ExemplarFilter",
"ExemplarReservoir",
"Meter",
Expand All @@ -41,5 +49,7 @@
"ObservableCounter",
"ObservableGauge",
"ObservableUpDownCounter",
"SimpleFixedSizeExemplarReservoir",
"UpDownCounter",
"TraceBasedExemplarFilter",
]
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def __init__(
metric_readers: Sequence[
"opentelemetry.sdk.metrics.export.MetricReader"
] = (),
resource: Resource = None,
resource: Optional[Resource] = None,
exemplar_filter: Optional[ExemplarFilter] = None,
shutdown_on_exit: bool = True,
views: Sequence["opentelemetry.sdk.metrics.view.View"] = (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
from io import StringIO
from json import loads
from unittest import TestCase
from unittest.mock import Mock, patch

from opentelemetry.context import Context
from opentelemetry.metrics import get_meter, set_meter_provider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics import AlwaysOnExemplarFilter, MeterProvider
from opentelemetry.sdk.metrics.export import (
ConsoleMetricExporter,
PeriodicExportingMetricReader,
)
from opentelemetry.test.globals_test import reset_metrics_globals


TEST_TIMESTAMP = 1_234_567_890


class TestConsoleExporter(TestCase):
def setUp(self):
reset_metrics_globals()
Expand Down Expand Up @@ -88,3 +93,39 @@ def test_console_exporter_no_export(self):
expected = ""

self.assertEqual(actual, expected)

@patch(
"opentelemetry.sdk.metrics._internal.instrument.time_ns",
Mock(return_value=TEST_TIMESTAMP),
)
def test_console_exporter_with_exemplars(self):
ctx = Context()

output = StringIO()
exporter = ConsoleMetricExporter(out=output)
reader = PeriodicExportingMetricReader(
exporter, export_interval_millis=100
)
provider = MeterProvider(metric_readers=[reader], exemplar_filter=AlwaysOnExemplarFilter())
set_meter_provider(provider)
meter = get_meter(__name__)
counter = meter.create_counter(
"name", description="description", unit="unit"
)
counter.add(1, attributes={"a": "b"}, context=ctx)
provider.shutdown()

output.seek(0)
result_0 = loads("".join(output.readlines()))

self.assertGreater(len(result_0), 0)

metrics = result_0["resource_metrics"][0]["scope_metrics"][0]

self.assertEqual(metrics["scope"]["name"], "test_console_exporter")

point = metrics["metrics"][0]["data"]["data_points"][0]

self.assertEqual(point["attributes"], {"a": "b"})
self.assertEqual(point["value"], 1)
self.assertEqual(point["exemplars"], [{"filtered_attributes": {}, "value": 1, "time_unix_nano": TEST_TIMESTAMP, "span_id": None, "trace_id": None}])

0 comments on commit 6a25608

Please sign in to comment.