-
Notifications
You must be signed in to change notification settings - Fork 0
Add prometheus metrics to backend #28
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,22 @@ | |
| import sqlite3 | ||
| from models.snack import Snack, SnackCreateSchema, SnackUpdateSchema,BulkSnackCreate | ||
| from typing import List | ||
| import prometheus_client | ||
|
|
||
| # Prometheus metrics | ||
| # track count of different types of snacks | ||
| snack_gauge = prometheus_client.Gauge( | ||
| "snack_gauge", | ||
| "Amount of snack types in inventory", | ||
| ["sku"], | ||
| ) | ||
|
|
||
| # track count of purchases | ||
| purchase_count = prometheus_client.Counter( | ||
| "purchase_count", | ||
| "Total number of snacks bought from inventory", | ||
| ["sku"], | ||
| ) | ||
|
|
||
| def get_db_connection(db_file_path:str="data/db.sqlite3"): | ||
| """Creates and returns a SQLite database connection""" | ||
|
|
@@ -79,6 +95,7 @@ def delete_snack(sku: str) -> Snack: | |
| RETURNING * | ||
| """, (sku,)) | ||
| record = cursor.fetchone() | ||
| snack_gauge.remove(sku) | ||
| return Snack(**record) | ||
|
|
||
|
|
||
|
|
@@ -102,6 +119,8 @@ def create_snack(snack: SnackCreateSchema) -> Snack: | |
| snack.photo_url | ||
| )) | ||
| record = cursor.fetchone() | ||
| snack = Snack(**record) | ||
| snack_gauge.labels(sku=snack.sku).set(snack.quantity) | ||
| return Snack(**record) | ||
|
|
||
|
|
||
|
|
@@ -124,6 +143,9 @@ def update_snack(sku: str, updates: SnackUpdateSchema) -> Snack: | |
| description = updates.description if updates.description is not None else current_snack['description'] | ||
| category = updates.category if updates.category is not None else current_snack['category'] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation of quantity_change as current_snack['quantity'] - quantity can be negative if quantity is greater than current_snack['quantity'], which may lead to incorrect logic downstream. This needs validation or adjustment to handle such cases properly. |
||
| photo_url = updates.photo_url if updates.photo_url is not None else current_snack['photo_url'] | ||
|
|
||
| purchase_made = quantity < current_snack['quantity'] | ||
| quantity_change = current_snack['quantity'] - quantity | ||
|
|
||
| cursor.execute(""" | ||
| UPDATE snacks | ||
|
|
@@ -146,7 +168,11 @@ def update_snack(sku: str, updates: SnackUpdateSchema) -> Snack: | |
| sku | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In update_snack, purchase_count.labels(sku).inc(quantity_change) is called without checking if quantity_change is positive. Incrementing with negative or zero values can cause incorrect metric reporting. Add validation to ensure only positive increments. |
||
| )) | ||
| record = cursor.fetchone() | ||
| return Snack(**record) | ||
| snack = Snack(**record) | ||
| if purchase_made: | ||
| purchase_count.labels(sku).inc(quantity_change) | ||
| snack_gauge.labels(sku).set(quantity) | ||
| return snack | ||
|
|
||
|
|
||
| # Bulk Processing function | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic assumes that a decrease in quantity means a purchase was made, but this may not always be true (e.g., returns, adjustments). Consider revising the logic to accurately reflect purchase events.