Skip to content

Commit

Permalink
Move exponential histogram tests from integration to unit
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtz-openai committed Mar 22, 2024
1 parent 4aecf46 commit 61ebe89
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
from json import loads
from unittest import TestCase

from opentelemetry.metrics import Histogram, get_meter, set_meter_provider
from opentelemetry.metrics import get_meter, set_meter_provider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
ConsoleMetricExporter,
PeriodicExportingMetricReader,
)
from opentelemetry.sdk.metrics.view import (
ExponentialBucketHistogramAggregation,
View,
)
from opentelemetry.test.globals_test import reset_metrics_globals


Expand Down Expand Up @@ -77,56 +73,6 @@ def test_console_exporter(self):
self.assertEqual(metrics["attributes"], {"a": "b"})
self.assertEqual(metrics["value"], 1)

def test_exp_histogram_exporter(self):
output = StringIO()
exporter = ConsoleMetricExporter(out=output)
reader = PeriodicExportingMetricReader(
exporter, export_interval_millis=100
)
provider = MeterProvider(
metric_readers=[reader],
views=[
View(
instrument_type=Histogram,
aggregation=ExponentialBucketHistogramAggregation(),
),
],
)
set_meter_provider(provider)
meter = get_meter(__name__)
hist = meter.create_histogram(
"name", description="description", unit="unit"
)
hist.record(1, attributes={"a": "b"})
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")

metrics = metrics["metrics"][0]

self.assertEqual(metrics["name"], "name")
self.assertEqual(metrics["description"], "description")
self.assertEqual(metrics["unit"], "unit")

metrics = metrics["data"]

self.assertEqual(metrics["aggregation_temporality"], 2)
self.assertEqual(len(metrics["data_points"]), 1)

metrics = metrics["data_points"][0]

self.assertEqual(metrics["attributes"], {"a": "b"})
self.assertEqual(metrics["count"], 1)
self.assertEqual(metrics["sum"], 1)
self.assertEqual(metrics["zero_count"], 0)

def test_console_exporter_no_export(self):

output = StringIO()
Expand Down
46 changes: 46 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
Buckets,
ExponentialHistogram,
ExponentialHistogramDataPoint,
Gauge,
Histogram,
HistogramDataPoint,
Expand Down Expand Up @@ -100,6 +103,23 @@ def setUpClass(cls):
)
cls.histogram_data_point_1_str = f'{{"attributes": {cls.attributes_1_str}, "start_time_unix_nano": 2, "time_unix_nano": 3, "count": 4, "sum": 4.4, "bucket_counts": [2, 1, 1], "explicit_bounds": [1.2, 2.3, 3.4, 4.5], "min": 0.3, "max": 4.4}}'

cls.exp_histogram_data_point_0 = ExponentialHistogramDataPoint(
attributes=cls.attributes_0,
start_time_unix_nano=1,
time_unix_nano=2,
count=1,
sum=10,
scale=1,
zero_count=0,
positive=Buckets(offset=0, bucket_counts=[1]),
negative=Buckets(offset=0, bucket_counts=[0]),
flags=0,
min=10,
max=10,
)
cls.exp_histogram_data_point_0_str = f'{{"attributes": {cls.attributes_0_str}, "start_time_unix_nano": 1, "time_unix_nano": 2, "count": 1, "sum": 10, "scale": 1, "zero_count": 0, "positive": {{"offset": 0, "bucket_counts": [1]}}, "negative": {{"offset": 0, "bucket_counts": [0]}}, "flags": 0, "min": 10, "max": 10}}'


cls.sum_0 = Sum(
data_points=[cls.number_data_point_0, cls.number_data_point_1],
aggregation_temporality=AggregationTemporality.DELTA,
Expand All @@ -121,6 +141,15 @@ def setUpClass(cls):
)
cls.histogram_0_str = f'{{"data_points": [{cls.histogram_data_point_0_str}, {cls.histogram_data_point_1_str}], "aggregation_temporality": 1}}'

cls.exp_histogram_0 = ExponentialHistogram(
data_points=[
cls.exp_histogram_data_point_0,
],
aggregation_temporality=AggregationTemporality.CUMULATIVE,
)
cls.exp_histogram_0_str = f'{{"data_points": [{cls.exp_histogram_data_point_0_str}], "aggregation_temporality": 2}}'


cls.metric_0 = Metric(
name="metric_0",
description="description_0",
Expand Down Expand Up @@ -209,6 +238,15 @@ def test_histogram_data_point(self):
self.histogram_data_point_1_str,
)

def test_exp_histogram_data_point(self):

self.maxDiff = None

self.assertEqual(
self.exp_histogram_data_point_0.to_json(indent=None),
self.exp_histogram_data_point_0_str,
)

def test_sum(self):

self.assertEqual(self.sum_0.to_json(indent=None), self.sum_0_str)
Expand All @@ -225,6 +263,14 @@ def test_histogram(self):
self.histogram_0.to_json(indent=None), self.histogram_0_str
)

def test_exp_histogram(self):

self.maxDiff = None

self.assertEqual(
self.exp_histogram_0.to_json(indent=None), self.exp_histogram_0_str
)

def test_metric(self):

self.assertEqual(self.metric_0.to_json(indent=None), self.metric_0_str)
Expand Down

0 comments on commit 61ebe89

Please sign in to comment.