-
Notifications
You must be signed in to change notification settings - Fork 0
feat: API monitoring #49
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
Merged
+268
−8
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5917cf
feat: /metrics route and pinoy-loki logging
wylited 5fe7acd
feat: testing for /metrics route
wylited c6206ca
feat: add Prometheus config example
polyipseity 3f7cc83
chore(gitignore): ignore Prometheus data folder
polyipseity 778fe93
chore: format
polyipseity 7727a3c
chore: fix lint
polyipseity 67f6bcf
fix: simplify metrics and logger init code
polyipseity 5caae19
fix: test helper
polyipseity 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
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
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,56 @@ | ||
| global: | ||
| scrape_interval: 15s # By default, scrape targets every 15 seconds. | ||
|
|
||
| # Attach these labels to any time series or alerts when communicating with | ||
| # external systems (federation, remote storage, Alertmanager). | ||
| external_labels: | ||
| monitor: 'codelab-monitor' | ||
|
|
||
| # Scrape configurations used for local development: | ||
| # - `prometheus`: Prometheus' own internal metrics endpoint. | ||
| # - `template-api`: this project's API (expects metrics at `http://localhost:3000/metrics`). | ||
| scrape_configs: | ||
| # Prometheus server metrics (job=prometheus) | ||
| - job_name: 'prometheus' | ||
|
|
||
| # Short interval for local testing. | ||
| scrape_interval: 5s | ||
|
|
||
| static_configs: | ||
| - targets: ['localhost:9090'] | ||
|
|
||
| # The template API exposes application metrics at `/metrics`. | ||
| # Make sure the API is running locally and exposes Prometheus metrics (e.g. using `prom-client` or middleware). | ||
| - job_name: 'template-api' | ||
| # Keep a short interval for fast feedback during development. | ||
| scrape_interval: 5s | ||
| # Prometheus will request the `/metrics` path by default, but we show it here explicitly for clarity. | ||
| metrics_path: /metrics | ||
| static_configs: | ||
| - targets: ['localhost:3000'] | ||
|
|
||
| # Optional: relabeling examples for common local/dev setups. Uncomment and adapt as needed. | ||
| # - Add a static label (useful for environment-specific queries) | ||
| # relabel_configs: | ||
| # - target_label: env | ||
| # replacement: dev | ||
| # | ||
| # - Set `instance` to the hostname (strip port) | ||
| # - source_labels: [__address__] | ||
| # regex: '([^:]+):.*' | ||
| # target_label: instance | ||
| # replacement: '$1' | ||
| # | ||
| # - Drop a specific target (useful to temporarily disable scraping) | ||
| # - source_labels: [__address__] | ||
| # regex: '127\\.0\\.0\\.1:3000' | ||
| # action: drop | ||
| # | ||
| # - Preserve original address in another label before rewriting (advanced example) | ||
| # - source_labels: [__address__] | ||
| # target_label: __param_target | ||
| # replacement: '$1' | ||
| # | ||
| # Notes: | ||
| # - `relabel_configs` run during target discovery and can add/modify/drop labels. | ||
| # - Keep examples commented so the default local setup continues to work. |
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
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
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,41 @@ | ||
| import { build } from "../helper.js"; | ||
| import * as assert from "node:assert"; | ||
| import { test } from "node:test"; | ||
|
|
||
| test("metrics route without key", async (t) => { | ||
| const app = await build(t); | ||
|
|
||
| const response = await app.inject({ | ||
| url: "/metrics", | ||
| }); | ||
|
|
||
| assert.equal(response.statusCode, 200); | ||
| }); | ||
wylited marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test("metrics route with key", async (t) => { | ||
| const app = await build(t, { prometheusKey: "secret" }); | ||
|
|
||
| // Without auth header | ||
| const response = await app.inject({ | ||
| url: "/metrics", | ||
| }); | ||
| assert.equal(response.statusCode, 401); | ||
|
|
||
| // With correct auth header | ||
| const responseAuth = await app.inject({ | ||
| url: "/metrics", | ||
| headers: { | ||
| authorization: "Bearer secret", | ||
| }, | ||
| }); | ||
| assert.equal(responseAuth.statusCode, 200); | ||
wylited marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // With incorrect auth header | ||
| const responseBadAuth = await app.inject({ | ||
| url: "/metrics", | ||
| headers: { | ||
| authorization: "Bearer wrong", | ||
| }, | ||
| }); | ||
| assert.equal(responseBadAuth.statusCode, 401); | ||
| }); | ||
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.