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

Feature: Prometheus Middleware #1791

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

Conversation

roma-frolov
Copy link
Contributor

@roma-frolov roma-frolov commented Sep 14, 2024

Description

Implemented Prometheus middleware for all brokers.

It seems to me that the getting-started documentation for prometheus middlewares (index.md) is not entirely complete. I would be glad to receive suggestions on what I would like to see in the documentation for this implementation.

Fixes #1665

Type of change

  • Documentation (typos, code examples, or any documentation updates)
  • Bug fix (a non-breaking change that resolves an issue)
  • New feature (a non-breaking change that adds functionality)
  • This change requires a documentation update

Checklist

  • My code adheres to the style guidelines of this project (scripts/lint.sh shows no errors)
  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • Both new and existing unit tests pass successfully on my local environment by running scripts/test-cov.sh
  • I have ensured that static analysis tests are passing by running scripts/static-analysis.sh
  • I have included code examples to illustrate the modifications

@Lancetnik Lancetnik added enhancement New feature or request Core Issues related to core FastStream functionality and affects to all brokers labels Sep 16, 2024
@roma-frolov roma-frolov marked this pull request as ready for review September 19, 2024 20:02
Copy link

@vignesh1507 vignesh1507 left a comment

Choose a reason for hiding this comment

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

I agree with the changes and really appreciate the effort @roma-frolov. Thanks for contributing to this mind-blowing project.

Copy link

@vignesh1507 vignesh1507 left a comment

Choose a reason for hiding this comment

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

Appreciate the effort @roma-frolov.


def __init__(self, registry: "CollectorRegistry"):
self.received_messages = Counter(
name="received_messages",
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should be faststream_received_messages_total

registry=registry,
)
self.received_messages_size = Histogram(
name="received_messages_size",
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should be faststream_received_messages_size_bytes

], # from 2^0 (1 byte) to 2^30 (1024 mb)
)
self.received_messages_processing_time = Histogram(
name="received_messages_processing_time",
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should be faststream_received_messages_processing_duration_seconds

registry=registry,
)
self.received_messages_in_process = Gauge(
name="received_messages_in_process",
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should be faststream_received_messages_in_process

registry=registry,
)
self.received_processed_messages = Counter(
name="received_processed_messages",
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should be faststream_received_processed_messages_total

status=status.value,
).inc()

if status == ProcessingStatus.error:
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be that status != ProcessingStatus.error, but err is not empty? In this case, the error will be lost.

)

except Exception as e:
err = e
Copy link
Contributor

Choose a reason for hiding this comment

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

Why _metrics.messages_publishing_exceptions inc in finally block?

*[(status, None) for status in AckStatus],
],
)
async def test_metrics(
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, the mock-based approach to metric tests does not check the metrics themselves in any way and does not show reference values. An instance of the metric has a collect() method that outputs the final data. The approach to metric tests can be viewed here: https://github.com/draincoder/asgi-monitor/blob/develop/tests/unit/metrics/test_metrics.py

labelnames=["broker", "handler"],
registry=registry,
buckets=[
pow(2, x) for x in range(31)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are there such buckets?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's worth merging some metrics into one, and specifying publish/consume/process kind in labels? For example, a metric for exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Issues related to core FastStream functionality and affects to all brokers enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Prometheus Middleware to collect metrics
4 participants