Skip to content
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

Implement ExplicitBucketBoundaries advisory for Histograms #4361

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Dec 17, 2024

Description

This adds basic support for the advisory attribute of Instruments and implements ExplicitBucketBoundaries advisory for Histograms.

Fixes #4140

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@xrmx xrmx requested a review from a team as a code owner December 17, 2024 10:38
Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Any clue on the docs CI error? I would say to add to nitpick_ignore.

@xrmx
Copy link
Contributor Author

xrmx commented Dec 19, 2024

Thanks. Any clue on the docs CI error? I would say to add to nitpick_ignore.

Nope, it's really hard for me to understand where this is coming from. Tried using "AnyValue" in util/types.py but does not change anything.

@xrmx
Copy link
Contributor Author

xrmx commented Dec 19, 2024

Appear to work fine with the flask implementation after updating the create_histogram calls for HTTP_SERVER_REQUEST_DURATION:

                        {
                            "name": "http.server.request.duration",
                            "description": "Duration of HTTP server requests.",
                            "unit": "s",
                            "data": {
                                "data_points": [
                                    {
                                        "attributes": {
                                            "http.request.method": "GET",
                                            "url.scheme": "http",
                                            "network.protocol.version": "1.1",
                                            "http.response.status_code": 200,
                                            "http.route": "/rolldice"
                                        },
                                        "start_time_unix_nano": 1734623881505049269,
                                        "time_unix_nano": 1734624051781113265,
                                        "count": 9,
                                        "sum": 0.00882425531744957,
                                        "bucket_counts": [
                                            9,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0
                                        ],
                                        "explicit_bounds": [
                                            0.005,
                                            0.01,
                                            0.025,
                                            0.05,
                                            0.075,
                                            0.1,
                                            0.25,
                                            0.5,
                                            0.75,
                                            1,
                                            2.5,
                                            5,
                                            7.5,
                                            10
                                        ],
                                        "min": 0.0004944326356053352,
                                        "max": 0.001611161045730114,
                                        "exemplars": []
                                    }
                                ],
                                "aggregation_temporality": 2
                            }
                        }

@emdneto
Copy link
Member

emdneto commented Dec 19, 2024

Thanks. Any clue on the docs CI error? I would say to add to nitpick_ignore.

Nope, it's really hard for me to understand where this is coming from. Tried using "AnyValue" in util/types.py but does not change anything.

We can probably use a TypedDict or just add ("py:class", "AnyValue"), to nitpick_ignore and see if it works:

diff --git a/docs/conf.py b/docs/conf.py
index 965a806d..997b5784 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -96,6 +96,7 @@ nitpicky = True
 # Container supposedly were fixed, but does not work
 # https://github.com/sphinx-doc/sphinx/pull/3744
 nitpick_ignore = [
+    ("py:class", "AnyValue"),
     ("py:class", "ValueT"),
     ("py:class", "CarrierT"),
     ("py:obj", "opentelemetry.propagators.textmap.CarrierT"),

@xrmx
Copy link
Contributor Author

xrmx commented Dec 19, 2024

Thanks. Any clue on the docs CI error? I would say to add to nitpick_ignore.

Nope, it's really hard for me to understand where this is coming from. Tried using "AnyValue" in util/types.py but does not change anything.

We can probably use a TypedDict or just add ("py:class", "AnyValue"), to nitpick_ignore and see if it works:

Moved to TypedDict, thanks for the hint!

@emdneto emdneto added the Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary label Dec 19, 2024
@xrmx xrmx force-pushed the histogram-advisory branch from 3c8b98e to 181596b Compare December 23, 2024 14:25
@xrmx xrmx requested a review from emdneto December 23, 2024 14:29
@@ -56,3 +55,7 @@
],
...,
]


class MetricsInstrumentAdvisory(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using a dataclass instead of typed dict? IMO dataclass is a little cleaner and has validation for users not using typing, but maybe there are some tradeoffs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I'll fix the other comments and then look at this

Comment on lines 218 to 222
if raise_error:
raise ValueError(
"Advisory must be a dict with explicit_bucket_boundaries key containing a sequence of numbers"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from the spec, emphasis mine

When a Meter creates an instrument, it SHOULD validate the instrument advisory parameters. If an advisory parameter is not valid, the Meter SHOULD emit an error notifying the user and proceed as if the parameter was not provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected, thanks!

7500.0,
10000.0,
),
boundaries: Optional[Sequence[float]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not leave the default as _DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES? I'm wondering because this allows explicitly passing None now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aabmass Yeah, now you can pass None boundaries but then the defaults boundaries are used. Also this is more pythonic I guess?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason user would pass None instead of allowing the default?

instrumentation_scope=instrumentation_scope,
measurement_consumer=measurement_consumer,
)
self.advisory = advisory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new part of the public API right? Should we make it protected or do users have any reason to read it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it private

opentelemetry-sdk/tests/metrics/test_aggregation.py Outdated Show resolved Hide resolved
opentelemetry-api/src/opentelemetry/util/types.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this covered already?

If multiple identical Instruments are created with different advisory parameters, the Meter MUST return an instrument using the first-seen advisory parameters and log an appropriate error as described in duplicate instrument registrations.

Copy link
Contributor Author

@xrmx xrmx Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our duplicate instrument check is already wrong and printing the warning when two identical instruments are created instead of conflicting ones. I've a local branch updating it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see tests are red there, will fix and open a PR tomorrow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this requirement covered when using Default aggregation?

Explicit Bucket Histogram Aggregation, with the ExplicitBucketBoundaries advisory parameter if provided

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use it already

CHANGELOG.md Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we are missing tests for the new behavior. Would be nice to see integration tests for the cross product of

  • views/no views
  • advisory/no advisory

Copy link
Contributor Author

@xrmx xrmx Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add tests for interaction with views for sure. What kind of integration tests are you thinking for advisory? I've added a test for create_histogram method that is what instrumentations are using for creating them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something high level like this test https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/tests/metrics/integration_test/test_disable_default_views.py. I guess just to capture the possible use cases and make sure we don't have unexpected behavior e.g.

  • User does nothing -> advisory buckets are used
  • User passes default aggregation to histogram -> advisory buckets are used
  • User passes ExplicitBucketHistogramAggregation -> view buckets are used

@xrmx xrmx force-pushed the histogram-advisory branch from 22b40b0 to c148618 Compare January 15, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement the histogram bucket advise API
3 participants