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

Allow customization of Counter, Histogram, Gauge #53

Open
extreme4all opened this issue Aug 31, 2024 · 0 comments
Open

Allow customization of Counter, Histogram, Gauge #53

extreme4all opened this issue Aug 31, 2024 · 0 comments

Comments

@extreme4all
Copy link

extreme4all commented Aug 31, 2024

Problem statement:

I want to add extra buckets to the Histogram.

Currently i don't think i can modify these variables because they are not initialized with the class

Proposed Solution

initialize the variables within the class, allow users to either add their own kwargs or add their own Counter, Histogram, Gauge

class PrometheusMiddleware(BaseHTTPMiddleware):
    def __init__(self, app: ASGIApp, filter_unhandled_paths: bool = False,
                 counter_kwargs: Optional[Dict[str, Any]] = None,
                 histogram_kwargs: Optional[Dict[str, Any]] = None,
                 gauge_kwargs: Optional[Dict[str, Any]] = None) -> None:
        super().__init__(app)
        self.filter_unhandled_paths = filter_unhandled_paths
        
        # Default values, can be overridden by kwargs
        counter_kwargs = counter_kwargs or {}
        histogram_kwargs = histogram_kwargs or {}
        gauge_kwargs = gauge_kwargs or {}
        
        self.REQUESTS = Counter(
            "starlette_requests_total",
            "Total count of requests by method and path.",
            ["method", "path_template"],
            **counter_kwargs
        )
        self.RESPONSES = Counter(
            "starlette_responses_total",
            "Total count of responses by method, path and status codes.",
            ["method", "path_template", "status_code"],
            **counter_kwargs
        )
        self.REQUESTS_PROCESSING_TIME = Histogram(
            "starlette_requests_processing_time_seconds",
            "Histogram of requests processing time by path (in seconds)",
            ["method", "path_template"],
            **histogram_kwargs
        )
        self.EXCEPTIONS = Counter(
            "starlette_exceptions_total",
            "Total count of exceptions raised by path and exception type",
            ["method", "path_template", "exception_type"],
            **counter_kwargs
        )
        self.REQUESTS_IN_PROGRESS = Gauge(
            "starlette_requests_in_progress",
            "Gauge of requests by method and path currently being processed",
            ["method", "path_template"],
            **gauge_kwargs
        )

or

class PrometheusMiddleware(BaseHTTPMiddleware):
    def __init__(self, app: ASGIApp, filter_unhandled_paths: bool = False,
                 requests_counter: Optional[Counter] = None,
                 responses_counter: Optional[Counter] = None,
                 processing_time_histogram: Optional[Histogram] = None,
                 exceptions_counter: Optional[Counter] = None,
                 requests_in_progress_gauge: Optional[Gauge] = None) -> None:
        super().__init__(app)
        self.filter_unhandled_paths = filter_unhandled_paths
        
        # Use provided metrics or default to the ones defined below
        self.REQUESTS = requests_counter or Counter(
            "starlette_requests_total",
            "Total count of requests by method and path.",
            ["method", "path_template"]
        )
        self.RESPONSES = responses_counter or Counter(
            "starlette_responses_total",
            "Total count of responses by method, path and status codes.",
            ["method", "path_template", "status_code"]
        )
        self.REQUESTS_PROCESSING_TIME = processing_time_histogram or Histogram(
            "starlette_requests_processing_time_seconds",
            "Histogram of requests processing time by path (in seconds)",
            ["method", "path_template"]
        )
        self.EXCEPTIONS = exceptions_counter or Counter(
            "starlette_exceptions_total",
            "Total count of exceptions raised by path and exception type",
            ["method", "path_template", "exception_type"]
        )
        self.REQUESTS_IN_PROGRESS = requests_in_progress_gauge or Gauge(
            "starlette_requests_in_progress",
            "Gauge of requests by method and path currently being processed",
            ["method", "path_template"]
        )
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

No branches or pull requests

1 participant