-
Notifications
You must be signed in to change notification settings - Fork 71
Demo: Prometheus plugin example #315
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
Open
savathoon
wants to merge
5
commits into
main
Choose a base branch
from
prometheus-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
09eaa30
initial demo prometheus plugin example
savathoon 2c9981a
Merge branch 'main' into prometheus-plugin
savathoon dbed317
import cleanup
savathoon cafdd06
mypy resolve errors
savathoon 4fe052a
Update examples/plugins/prometheus_metrics_reporter/README.md
savathoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| # Prometheus Metrics Reporter Plugin | ||
|
|
||
| A Prometheus-compatible metrics reporter plugin for the Access Management System. This plugin integrates with the `prometheus_client` library to expose metrics in Prometheus format. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Counter Metrics**: Track cumulative values (e.g., request counts, errors) | ||
| - **Gauge Metrics**: Track current values (e.g., active connections, queue size) | ||
| - **Histogram Metrics**: Track distributions of values (e.g., response times, request sizes) | ||
| - **Summary Metrics**: Track quantiles of values (e.g., percentiles of response times) | ||
| - **Batch Processing**: Efficiently batch multiple metrics for better performance | ||
| - **Label Support**: Add custom labels to metrics for better filtering and grouping | ||
| - **Environment Detection**: Automatic environment labeling (dev/stg/prd) | ||
| - **Thread Safety**: Thread-safe implementation for concurrent access | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - Python 3.7+ | ||
| - Access Management System with plugin support | ||
| - `prometheus_client` library | ||
|
|
||
| ### Install the Plugin | ||
|
|
||
| 1. **Clone or download the plugin files** to your desired location | ||
|
|
||
| 2. **Install dependencies**: | ||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| 3. **Install the plugin**: | ||
| ```bash | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Environment Variables | ||
|
|
||
| The plugin uses the following environment variables for configuration: | ||
|
|
||
| | Variable | Description | Default | Required | | ||
| |----------|-------------|---------|----------| | ||
| | `FLASK_ENV` | Environment name for metric labeling | `development` | No | | ||
| | `PROMETHEUS_MULTIPROC_DIR` | Directory for multiprocess metrics | None | No* | | ||
| | `PROMETHEUS_DISABLE_CREATED_SERIES` | Disable created series metrics | `False` | No | | ||
|
|
||
| *Required if using multiple processes (e.g., Gunicorn with multiple workers) | ||
|
|
||
| ### Environment Labeling | ||
|
|
||
| The plugin automatically adds environment labels based on `FLASK_ENV`: | ||
|
|
||
| - `development` → `env=dev` | ||
| - `staging` → `env=stg` | ||
| - `production` → `env=prd` | ||
| - Any other value → `env=dev` | ||
|
|
||
| ### Global Tags | ||
|
|
||
| You can set global tags that will be included with all metrics: | ||
|
|
||
| ```python | ||
| from access.plugins.metrics_reporter import metrics_reporter | ||
|
|
||
| # Set global tags | ||
| metrics_reporter.set_global_tags({ | ||
| "service": "access", | ||
| "version": "1.0.0", | ||
| "region": "us-west-2" | ||
| }) | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| The plugin automatically registers with the Access Management System when installed. Metrics will be exposed at the `/metrics` endpoint in Prometheus format. | ||
|
|
||
| ### Metric Types | ||
|
|
||
| #### Counters | ||
|
|
||
| ```python | ||
| # Increment a counter | ||
| metrics_reporter.increment_counter("requests_total", tags={"endpoint": "/api/users"}) | ||
|
|
||
| # Increment with custom value | ||
| metrics_reporter.increment_counter("bytes_processed", value=1024, tags={"type": "upload"}) | ||
| ``` | ||
|
|
||
| #### Gauges | ||
|
|
||
| ```python | ||
| # Set a gauge value | ||
| metrics_reporter.record_gauge("active_connections", 42, tags={"pool": "database"}) | ||
|
|
||
| # Update queue size | ||
| metrics_reporter.record_gauge("queue_size", 15, tags={"queue": "email"}) | ||
| ``` | ||
|
|
||
| #### Histograms | ||
|
|
||
| ```python | ||
| # Record timing (automatically converts ms to seconds) | ||
| metrics_reporter.record_timing("request_duration", 150.5, tags={"endpoint": "/api/users"}) | ||
|
|
||
| # Record custom histogram with buckets | ||
| metrics_reporter.record_histogram( | ||
| "request_size", | ||
| 2048, | ||
| tags={"method": "POST"}, | ||
| buckets=[100, 500, 1000, 5000, 10000] | ||
| ) | ||
| ``` | ||
|
|
||
| #### Summaries | ||
|
|
||
| ```python | ||
| # Record summary metric | ||
| metrics_reporter.record_summary("response_time", 0.25, tags={"service": "auth"}) | ||
| ``` | ||
|
|
||
| ### Batch Processing | ||
|
|
||
| For better performance when recording multiple metrics: | ||
|
|
||
| ```python | ||
| with metrics_reporter.batch_metrics(): | ||
| metrics_reporter.increment_counter("requests_total", tags={"endpoint": "/api/users"}) | ||
| metrics_reporter.record_gauge("active_connections", 42) | ||
| metrics_reporter.record_timing("request_duration", 150.5) | ||
| ``` | ||
|
|
||
| ### Manual Flush | ||
|
|
||
| Force flush any buffered metrics: | ||
|
|
||
| ```python | ||
| metrics_reporter.flush() | ||
| ``` | ||
|
|
||
| ## Prometheus Integration | ||
|
|
||
| ### Exposing Metrics | ||
|
|
||
| The plugin automatically exposes metrics at `/metrics` endpoint. Ensure your Prometheus server is configured to scrape this endpoint. | ||
|
|
||
| ### Example Prometheus Configuration | ||
|
|
||
| ```yaml | ||
| scrape_configs: | ||
| - job_name: 'access-management' | ||
| static_configs: | ||
| - targets: ['localhost:5000'] | ||
| metrics_path: '/metrics' | ||
| scrape_interval: 15s | ||
| ``` | ||
|
|
||
| ### Metric Naming | ||
|
|
||
| All metrics are automatically converted to Prometheus naming conventions: | ||
| - Dots and dashes are replaced with underscores | ||
| - Invalid characters are removed | ||
| - Metrics starting with numbers are prefixed with `access_` | ||
|
|
||
| Examples: | ||
| - `requests.total` → `requests_total` | ||
| - `api-errors` → `api_errors` | ||
| - `1st_request` → `access_1st_request` | ||
|
|
||
| ### Label Validation | ||
|
|
||
| The plugin automatically validates and cleans label names: | ||
| - Only alphanumeric characters and underscores are allowed | ||
| - Labels must start with a letter | ||
| - Invalid labels are logged and skipped | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Common Issues | ||
|
|
||
| 1. **Import Error**: Ensure `prometheus_client` is installed | ||
| ```bash | ||
| pip install prometheus_client>=0.17.0 | ||
| ``` | ||
|
|
||
| 2. **Multiprocess Issues**: Set `PROMETHEUS_MULTIPROC_DIR` environment variable | ||
| ```bash | ||
| export PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_multiproc | ||
| ``` | ||
|
|
||
| 3. **Permission Errors**: Ensure the multiprocess directory is writable | ||
| ```bash | ||
| mkdir -p /tmp/prometheus_multiproc | ||
| chmod 755 /tmp/prometheus_multiproc | ||
| ``` | ||
|
|
||
| 4. **Metric Name Conflicts**: Check for duplicate metric names with different label sets | ||
|
|
||
| ### Debugging | ||
|
|
||
| Enable debug logging to see metric operations: | ||
|
|
||
| ```python | ||
| import logging | ||
| logging.getLogger('metrics_reporter').setLevel(logging.DEBUG) | ||
| ``` | ||
|
|
||
| ### Testing | ||
|
|
||
| Test the metrics endpoint: | ||
|
|
||
| ```bash | ||
| curl http://localhost:5000/metrics | ||
| ``` | ||
|
|
||
| You should see Prometheus-formatted metrics output. | ||
|
|
||
| ## Development | ||
|
|
||
| ### Running Tests | ||
|
|
||
| ```bash | ||
| python -m pytest tests/ | ||
| ``` | ||
|
|
||
| ### Contributing | ||
|
|
||
| 1. Fork the repository | ||
| 2. Create a feature branch | ||
| 3. Make your changes | ||
| 4. Add tests | ||
| 5. Submit a pull request | ||
|
|
||
| ## License | ||
|
|
||
| This plugin is part of the Access Management System and follows the same license terms. | ||
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.