Skip to content

Commit

Permalink
Add Exponential Histogram (#2964)
Browse files Browse the repository at this point in the history
* Bump werkzeug in /docs/examples/fork-process-model/flask-gunicorn (#3179)

Bumps [werkzeug](https://github.com/pallets/werkzeug) from 1.0.1 to 2.2.3.
- [Release notes](https://github.com/pallets/werkzeug/releases)
- [Changelog](https://github.com/pallets/werkzeug/blob/main/CHANGES.rst)
- [Commits](pallets/werkzeug@1.0.1...2.2.3)

---
updated-dependencies:
- dependency-name: werkzeug
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* Refactor buckets interface

* Add missing test case

* Remove wrong return

* Add comments

* Refactor object resetting

* More fixes

* Refactor exporting of buckets

* Fix bug

* Fix spelling

* Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

Co-authored-by: Aaron Abbott <aaronabbott@google.com>

* Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

Co-authored-by: Aaron Abbott <aaronabbott@google.com>

* Fix exception messages

* Remove scaling check

* Fix typo

* Remove wrong instrument

* Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

Co-authored-by: Aaron Abbott <aaronabbott@google.com>

* Add attribute setting lo lock scope

* Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/buckets.py

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* Fix collect

* Remove unreachable code

* Fix exporter test cases

* Add comment

* Expand lock scope

* Revert "Expand lock scope"

This reverts commit fd9c47b.

* Expand lock scope

* Add min_max_size test case

* Fix lint

* Use properties for buckets attributes

* Fix bug

* Add comment

* WIP

* Reset values at the beginning of collect

* Merging WIP

* Add merge submethods

* Refactor to use auxiliary methods

* Add downscale method

* WIP

* Finish merge

* Refactor storage of positive and negative WIP

* WIP

* Add test_merge_simple_event_test

* WIP

* WIP

* WIP

* WIP

* Fix lint

* Fix lint

* Add delta test case

* Avoid using current_point attributes

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
Co-authored-by: Aaron Abbott <aaronabbott@google.com>
  • Loading branch information
5 people committed Mar 13, 2023
1 parent f05fa77 commit f0a0cc4
Show file tree
Hide file tree
Showing 9 changed files with 1,950 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix formatting of ConsoleMetricExporter.
([#3197](https://github.com/open-telemetry/opentelemetry-python/pull/3197))

## Version 1.16.0/0.37b0 (2023-02-17)
- Add exponential histogram
([#2964](https://github.com/open-telemetry/opentelemetry-python/pull/2964))

## Version 1.16.0/0.37b0 (2023-02-15)

- Change ``__all__`` to be statically defined.
([#3143](https://github.com/open-telemetry/opentelemetry-python/pull/3143))
- Remove the ability to set a global metric prefix for Prometheus exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
ResourceMetrics,
ScopeMetrics,
Sum,
ExponentialHistogram as ExponentialHistogramType,
)

_logger = getLogger(__name__)
Expand Down Expand Up @@ -266,6 +267,51 @@ def _translate_data(
metric.data.is_monotonic
)
pb2_metric.sum.data_points.append(pt)

elif isinstance(metric.data, ExponentialHistogramType):
for data_point in metric.data.data_points:

if data_point.positive.bucket_counts:
positive = pb2.ExponentialHistogramDataPoint.Buckets(
offset=data_point.positive.offset,
bucket_counts=data_point.positive.bucket_counts,
)
else:
positive = None

if data_point.negative.bucket_counts:
negative = pb2.ExponentialHistogramDataPoint.Buckets(
offset=data_point.negative.offset,
bucket_counts=data_point.negative.bucket_counts,
)
else:
negative = None

pt = pb2.ExponentialHistogramDataPoint(
attributes=self._translate_attributes(
data_point.attributes
),
time_unix_nano=data_point.time_unix_nano,
start_time_unix_nano=(
data_point.start_time_unix_nano
),
count=data_point.count,
sum=data_point.sum,
scale=data_point.scale,
zero_count=data_point.zero_count,
positive=positive,
negative=negative,
flags=data_point.flags,
max=data_point.max,
min=data_point.min,
)
pb2_metric.exponential_histogram.aggregation_temporality = (
metric.data.aggregation_temporality
)
pb2_metric.exponential_histogram.data_points.append(
pt
)

else:
_logger.warning(
"unsupported data type %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@
ObservableUpDownCounter,
UpDownCounter,
)
from opentelemetry.sdk.metrics.export import AggregationTemporality, Gauge
from opentelemetry.sdk.metrics.export import AggregationTemporality, Buckets
from opentelemetry.sdk.metrics.export import (
ExponentialHistogram as ExponentialHistogramType,
)
from opentelemetry.sdk.metrics.export import (
ExponentialHistogramDataPoint,
Gauge,
)
from opentelemetry.sdk.metrics.export import Histogram as HistogramType
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
Expand Down Expand Up @@ -163,6 +170,31 @@ def setUp(self):
),
)

exponential_histogram = Metric(
name="exponential_histogram",
description="description",
unit="unit",
data=ExponentialHistogramType(
data_points=[
ExponentialHistogramDataPoint(
attributes={"a": 1, "b": True},
start_time_unix_nano=0,
time_unix_nano=1,
count=2,
sum=3,
scale=4,
zero_count=5,
positive=Buckets(offset=6, bucket_counts=[7, 8]),
negative=Buckets(offset=9, bucket_counts=[10, 11]),
flags=12,
min=13.0,
max=14.0,
)
],
aggregation_temporality=AggregationTemporality.DELTA,
),
)

self.metrics = {
"sum_int": MetricsData(
resource_metrics=[
Expand Down Expand Up @@ -276,6 +308,28 @@ def setUp(self):
)
]
),
"exponential_histogram": MetricsData(
resource_metrics=[
ResourceMetrics(
resource=Resource(
attributes={"a": 1, "b": False},
schema_url="resource_schema_url",
),
scope_metrics=[
ScopeMetrics(
scope=SDKInstrumentationScope(
name="first_name",
version="first_version",
schema_url="insrumentation_scope_schema_url",
),
metrics=[exponential_histogram],
schema_url="instrumentation_scope_schema_url",
)
],
schema_url="resource_schema_url",
)
]
),
}

self.multiple_scope_histogram = MetricsData(
Expand Down Expand Up @@ -896,6 +950,80 @@ def test_translate_histogram(self):
actual = self.exporter._translate_data(self.metrics["histogram"])
self.assertEqual(expected, actual)

def test_translate_exponential_histogram(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
pb2.ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
scope_metrics=[
pb2.ScopeMetrics(
scope=InstrumentationScope(
name="first_name", version="first_version"
),
metrics=[
pb2.Metric(
name="exponential_histogram",
unit="unit",
description="description",
exponential_histogram=pb2.ExponentialHistogram(
data_points=[
pb2.ExponentialHistogramDataPoint(
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=True
),
),
],
start_time_unix_nano=0,
time_unix_nano=1,
count=2,
sum=3,
scale=4,
zero_count=5,
positive=pb2.ExponentialHistogramDataPoint.Buckets(
offset=6,
bucket_counts=[7, 8],
),
negative=pb2.ExponentialHistogramDataPoint.Buckets(
offset=9,
bucket_counts=[10, 11],
),
flags=12,
exemplars=[],
min=13.0,
max=14.0,
)
],
aggregation_temporality=AggregationTemporality.DELTA,
),
)
],
)
],
)
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data(
self.metrics["exponential_histogram"]
)
self.assertEqual(expected, actual)

def test_translate_multiple_scope_histogram(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
Expand Down

0 comments on commit f0a0cc4

Please sign in to comment.