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

Implemented get_name in StatsLogger, updated Otel and StatsD #43340

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

Conversation

ArshiaZr
Copy link

  • Added get_name method in StatsLogger (base_stat_logger.py) to handle metric name preparation and tag validation.
  • Updated Otel and StatsD loggers to inherit from StatsLogger and utilize the new get_name method.
  • Refactored tag preparation and validation logic into get_name for a cleaner and more consistent implementation.

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

- Added get_name method in StatsLogger (base_stat_logger.py) to handle metric name preparation and tag validation.
- Updated Otel and StatsD loggers to inherit from StatsLogger and utilize the new get_name method.
- Refactored tag preparation and validation logic into get_name for a cleaner and more consistent implementation.
@ArshiaZr ArshiaZr closed this Oct 24, 2024
@ArshiaZr ArshiaZr reopened this Oct 24, 2024
@ArshiaZr ArshiaZr closed this Oct 24, 2024
@ArshiaZr ArshiaZr reopened this Oct 24, 2024
@ArshiaZr
Copy link
Author

@ferruzzi, @howardyoo, @o-nikolas Can you please take a look at it? If anything needs to change please let me know.

@howardyoo
Copy link
Contributor

howardyoo commented Oct 24, 2024

@ferruzzi, @howardyoo, @o-nikolas Can you please take a look at it? If anything needs to change please let me know.

@ArshiaZr, I am not a reviewer, and thus cannot review this PR, but I believe it would be nice if you can add unit tests for your changes as part of this PR, so that the changes can be properly checked to work properly. cc to @ferruzzi

@potiuk
Copy link
Member

potiuk commented Oct 25, 2024

@ArshiaZr, I am not a reviewer, and thus cannot review this PR, but I believe it would be nice if you can add unit tests for your changes as part of this PR, so that the changes can be properly checked to work properly. cc to @ferruzzi

Actually - anyone can review PRs - and we encourage it. You can even approve it @howardyoo and it might guide other maintainers with their approvals.

@howardyoo
Copy link
Contributor

howardyoo commented Oct 25, 2024 via email

@potiuk
Copy link
Member

potiuk commented Oct 25, 2024

No, as far as I know, I don’t think I have the ability to approve the PR

You can approve it (many contributors do that). But you need at least one approval from a commiter to merge it - but approvals can be done by absolutely anyone who has a GitHub account.

@howardyoo
Copy link
Contributor

howardyoo commented Oct 25, 2024 via email

Copy link
Contributor

@howardyoo howardyoo left a comment

Choose a reason for hiding this comment

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

Looks good to me, and ready. Thank you for pulling this up.

@howardyoo
Copy link
Contributor

howardyoo commented Oct 28, 2024 via email

@potiuk
Copy link
Member

potiuk commented Oct 28, 2024

Do we expect any incompatibilities in produced metrics @ArshiaZr ? I would love to understand what was the intention of "cleaner and more consistent implementation." before I get into details.

Copy link
Contributor

@ferruzzi ferruzzi left a comment

Choose a reason for hiding this comment

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

Left some comments and questions, overall looking pretty good but still needs a little work.

airflow/metrics/base_stats_logger.py Show resolved Hide resolved
airflow/metrics/base_stats_logger.py Outdated Show resolved Hide resolved
airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Outdated Show resolved Hide resolved
tests/core/test_otel_logger.py Outdated Show resolved Hide resolved
tests/core/test_otel_logger.py Outdated Show resolved Hide resolved
tests/core/test_otel_logger.py Outdated Show resolved Hide resolved
tests/core/test_otel_logger.py Show resolved Hide resolved
…tsdLogger for enhanced validation

- Modified StatsLogger base class to enforce stricter validation on metric names and prefixes, ensuring all names are strings and conform to required formats.
- Enhanced SafeOtelLogger with additional validation to support OpenTelemetry standards and added regression tests for metrics with and without tags.
- Updated SafeStatsdLogger to align with new validation standards and to handle edge cases more robustly.
- Added regression tests to catch issues with missing tags, empty names, and overly long names, ensuring consistency across changes.
@ArshiaZr
Copy link
Author

I just made the code more robust based on @ferruzzi reviews I'd appreciate it if you could look at it.

cc @howardyoo, @dannyl1u

Copy link
Contributor

@ferruzzi ferruzzi left a comment

Choose a reason for hiding this comment

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

Looking much better, left a few more comments. One big thing that has to be fixed, but the rest are little.

tests/core/test_otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Show resolved Hide resolved
tests/core/test_otel_logger.py Outdated Show resolved Hide resolved
@ArshiaZr
Copy link
Author

@ferruzzi Thanks for the reviews. Here's the fixed version. I had to add the instance back in StatsLogger as I noticed we're using it somewhere else.

airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/otel_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Show resolved Hide resolved
airflow/metrics/statsd_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Show resolved Hide resolved
@ArshiaZr
Copy link
Author

I raised another commit. I tried to address all the issues and reviewed it myself multiple times.
I'll be grateful if you can take another look at it @ferruzzi.

@ferruzzi
Copy link
Contributor

ferruzzi commented Nov 1, 2024

I can def check whether the name validation works on python otel sdk side

Thanks @howardyoo

I agree we should have short names where possible, but lets not artificially limit it in the logger to 32 characters

@ashb I concur. Can we agree that the Otel names should continue to be the name without the embedded variables, as it is now, and (assuming Howard confirms the external verification is actually working) just remove our truncating and validating the name?

The datadog statsd format natively supports tags -- we should use those there/and it should be updated to inherit form StatsLogger too

I'm not very familiar with datadog. Looking at the existing code, its get_name implementation should be similar to OTel's and just pass the name through as provided since it also gets the tags added later, right?


@ArshiaZr - Please implement the datadog part and either you and/or Howard can sort out what is going on with the otel name validation after that.

@howardyoo
Copy link
Contributor

howardyoo commented Nov 1, 2024 via email

- Added prepare_stat_with_tags decorator to standardize tag processing across all metrics functions.
- Ensured tags default to an empty list [] when no valid tags are provided or self.metrics_tags is disabled, matching expected behavior in tests.
- Updated all metric functions (e.g., incr, decr, gauge, timing, timer) to use the new decorator for consistent tag validation and formatting.
@ArshiaZr
Copy link
Author

ArshiaZr commented Nov 3, 2024

Rationale for Using Decorators Instead of Inheritance with Datadog

Using get_name with the Datadog protocol is unnecessary since we don’t combine tags and the stat name when calling DogStatsd functions. The functions in DogStatsd (e.g., increment, decrement, gauge, timing) expect the metric name and tags separately, so there’s no need to modify or combine them beforehand.

Even if tags need to be prepared, we already apply the @prepare_metric_name_with_tags decorator before get_name, which impacts only the values passed to get_name and not the final metric name used in DogStatsd calls.

Therefore, making SafeDogStatsdLogger a subclass of StatsLogger doesn’t add any functional value here. Instead, adding @prepare_metric_name_with_tags to each function in SafeDogStatsdLogger keeps the code organized and consistent without requiring inheritance specifically for Datadog. This approach provides clarity while keeping SafeDogStatsdLogger focused solely on Datadog-specific logic.
Additionally, I added the @prepare_stat_with_tags decorator for a more consistent way of validating tags, allowing for cleaner code by centralizing tag preparation and validation across all metric functions.

@ashb @ferruzzi if you have any other suggestions just let me know

@ashb
Copy link
Member

ashb commented Nov 4, 2024

Not being a subclass sounds fine as long as it follows the same interface/protocol. I'll check out the changes when I can.

@ArshiaZr
Copy link
Author

ArshiaZr commented Nov 5, 2024

I have also added inheritance for consistency. Just let me know which approach is better. @ashb, @ferruzzi

@ferruzzi
Copy link
Contributor

ferruzzi commented Nov 5, 2024

We discussed on slack, but I'll also reply here for posterity: I think inheritance is the right way to go here. Should someone make a change tot he base class in the future, it would be confusing to troubleshoot why StatsD and OTel both work but Datadog doesn't. So it should inherit for the sake of consistency, if nothing else.

Copy link
Contributor

@howardyoo howardyoo left a comment

Choose a reason for hiding this comment

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

Thanks!

@ferruzzi
Copy link
Contributor

@ashb - Can we get this merged?

@ashb
Copy link
Member

ashb commented Nov 13, 2024

Looking at this right now, sorry for the delay

airflow/metrics/datadog_logger.py Show resolved Hide resolved
airflow/metrics/datadog_logger.py Outdated Show resolved Hide resolved
airflow/metrics/datadog_logger.py Outdated Show resolved Hide resolved
airflow/metrics/datadog_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Outdated Show resolved Hide resolved
airflow/metrics/statsd_logger.py Show resolved Hide resolved
tests/core/test_stats.py Outdated Show resolved Hide resolved
@ashb ashb dismissed their stale review November 13, 2024 12:34

This review actioned, new changes but less blocking now

@ferruzzi
Copy link
Contributor

@ArshiaZr - You can also remove the portion(s) of code that limit the OTel name length now. I think we've all agreed that we can keep the names short as Best Practice but should no longer be truncating them.

if len(proposed_stat_name) > OTEL_NAME_MAX_LENGTH:
# If the name is in the exceptions list, do not fail it for being too long.
# It may still be deemed invalid for other reasons below.
for exemption in BACK_COMPAT_METRIC_NAMES:
Copy link
Contributor

@ferruzzi ferruzzi Nov 14, 2024

Choose a reason for hiding this comment

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

My IDE is currently borked so I can't double check this easily; is BACK_COMPAT_METRIC_NAMES used anywhere else? It's possible that can also be pruned out now if it was only ever used as a work-around for the name length bug.

@ferruzzi
Copy link
Contributor

They increased the name length in September of last year 😅 open-telemetry/opentelemetry-python#3442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants