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 32 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
Fixes #3042

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
opentelemetry-api/src/opentelemetry/util/types.py Outdated Show resolved Hide resolved
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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no reason the user would pass None, I expected to just call it without passing the parameter as currently doing. But no big deal in keeping thing as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it looks like we have someone passing None:

    def __init__(self, boundaries: Sequence[float], **kwargs) -> None:
>       super().__init__(len(boundaries) + 1, **kwargs)
E       TypeError: object of type 'NoneType' has no len()

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py:296: TypeError

and

>       self._boundaries = tuple(boundaries)
E       TypeError: 'NoneType' object is not iterable

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py:488: TypeError

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with whatever you want to do here

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 Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@xrmx xrmx force-pushed the histogram-advisory branch from 22b40b0 to c148618 Compare January 15, 2025 16:36

@abstractmethod
def create_counter(
self,
name: str,
unit: str = "",
description: str = "",
advisory: None = None,
Copy link
Member

Choose a reason for hiding this comment

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

Wdyt of adding doing this?

Suggested change
advisory: None = None,
*,
advisory: None = None,

to force any new arguments to be passed by name explicitly

  • getting to having a lot of arguments here and IMO it is less error prone for users
  • that would allow us to reorder the argument list if needed in the future

if we also added a trailing **kwargs, we could even remove parameters in the future if they get deprecated. Maybe lets discuss in the SIG but wanted to get your thoughts.

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'm fine with that

Comment on lines 381 to 419
# pylint: disable=super-init-not-called
def __init__(
Copy link
Member

Choose a reason for hiding this comment

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

Is it because of multiple inheritance?

Copy link
Contributor Author

@xrmx xrmx Jan 20, 2025

Choose a reason for hiding this comment

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

I think this is because I haven't updated _ProxyInstrument and all the other instrument classes to get the advisory parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding them broke mypy ofc 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 0b6fc1f

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.

I'm fine with whatever you want to do here

@xrmx xrmx force-pushed the histogram-advisory branch from e8c6a4d to d3a345f Compare January 20, 2025 10:32
@xrmx xrmx force-pushed the histogram-advisory branch from d3a345f to 005295a Compare January 21, 2025 13:53
@xrmx xrmx force-pushed the histogram-advisory branch from 1372540 to 5cd650a Compare January 21, 2025 14:53
Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

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

💯

@@ -394,6 +451,7 @@ def create_gauge( # type: ignore # pylint: disable=no-self-use
name: str,
unit: str = "",
description: str = "",
advisory: Optional[MetricsCommonAdvisory] = None,
Copy link
Member

Choose a reason for hiding this comment

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

What if the spec adds specification adds a new advisory type for another instrument, say MetricsGaugeAdvisory? I think if we changed the type here, that would be a breaking change

Copy link
Contributor Author

@xrmx xrmx Jan 21, 2025

Choose a reason for hiding this comment

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

So are you suggesting to add preemptively a custom type for each instrumenting or having the specific dataclass inherit from the common be enough? In other words, is the issue about the name of the type or its structure?

Copy link
Member

Choose a reason for hiding this comment

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

I don't mind so much as long as we avoid introducing breaking changes. I looked at Go and Java, they took more of a "flat" approach where advisory parameters are flattened into the builder or options type.

Copy link
Contributor Author

@xrmx xrmx Jan 22, 2025

Choose a reason for hiding this comment

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

I guess the flat approach is for this very same reason of avoiding breaking compatibility by changing the layout of the advisory

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 [TC Review - Metrics] Duplicate registration logic
3 participants