Skip to content

Commit 39fed27

Browse files
committed
Merge remote-tracking branch 'origin/copilot/set-up-prometheus-metrics' into stage
2 parents a14ed64 + d73ec52 commit 39fed27

14 files changed

Lines changed: 16600 additions & 1392 deletions

File tree

.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# API server port
22
PORT=4000
33

4+
# Metrics server port
5+
METRICS_PORT=9090
6+
47
# Hawk API database URL
58
MONGO_HAWK_DB_URL=mongodb://mongodb:27017/hawk
69

.github/workflows/build-and-push-docker-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Build and push docker image
33
on:
44
push:
55
branches:
6-
- '*'
6+
- '**'
77
tags:
88
- 'v*'
99

.github/workflows/bump-version.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ jobs:
66
# If pull request was merged then we should check for a package version update
77
check-version-update:
88
runs-on: ubuntu-22.04
9+
outputs:
10+
should-bump: ${{ steps.version-check.outputs.should-bump }}
911
steps:
1012
# Checkout to target branch
11-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v5
1214
with:
1315
fetch-depth: 0
1416

@@ -26,20 +28,29 @@ jobs:
2628
id: packageOld
2729
uses: codex-team/action-nodejs-package-info@v1
2830

29-
# Stop workflow and do not bump version if it was changed already
30-
- name: Stop workflow and do not bump version if it was changed already
31-
uses: andymckay/cancel-action@0.2
32-
if: steps.packageOld.outputs.version != steps.packageNew.outputs.version
31+
# Check if version should be bumped
32+
- name: Check if version should be bumped
33+
id: version-check
34+
run: |
35+
if [ "${{ steps.packageOld.outputs.version }}" == "${{ steps.packageNew.outputs.version }}" ]; then
36+
echo "should-bump=true" >> $GITHUB_OUTPUT
37+
else
38+
echo "should-bump=false" >> $GITHUB_OUTPUT
39+
fi
3340
3441
bump-version:
3542
needs: check-version-update
43+
if: needs.check-version-update.outputs.should-bump == 'true'
3644
runs-on: ubuntu-22.04
3745
steps:
3846
# Checkout to target branch
39-
- uses: actions/checkout@v2
47+
- uses: actions/checkout@v5
48+
with:
49+
repository: ${{ github.event.pull_request.head.repo.full_name }}
50+
ref: ${{ github.event.pull_request.head.ref }}
4051

4152
# Setup node environment
42-
- uses: actions/setup-node@v3
53+
- uses: actions/setup-node@v5
4354
with:
4455
node-version-file: '.nvmrc'
4556
registry-url: https://registry.npmjs.org/
@@ -54,7 +65,7 @@ jobs:
5465
uses: codex-team/action-nodejs-package-info@v1
5566

5667
# Commit version upgrade
57-
- uses: EndBug/add-and-commit@v7
68+
- uses: EndBug/add-and-commit@v9
5869
with:
5970
author_name: github-actions
6071
author_email: 41898282+github-actions[bot]@users.noreply.github.com

LICENSE

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Business Source License 1.1
2+
3+
Parameters
4+
5+
Licensor: CodeX (Hawk)
6+
Licensed Work: hawk.api.nodejs (https://github.com/codex-team/hawk.api.nodejs)
7+
The Licensed Work is © 2025 CodeX
8+
Additional Use Grant: Self-hosted use for own/internal needs and research/evaluation is permitted.
9+
Using this code to provide a competing error-tracking SaaS or commercial
10+
hosted service without the Licensor’s prior permission is prohibited.
11+
12+
Change Date: 2030-01-01
13+
14+
Change License: AGPL-3.0
15+
16+
-------------------------------------------------------------------------------
17+
18+
License text copyright © 2024 MariaDB plc, All Rights Reserved.
19+
“Business Source License” is a trademark of MariaDB plc.
20+
21+
Terms
22+
23+
The Licensor hereby grants you the right to copy, modify, create derivative
24+
works, redistribute, and make non-production use of the Licensed Work. The
25+
Licensor may make an Additional Use Grant, above, permitting limited
26+
production use.
27+
28+
Effective on the Change Date, or the fourth anniversary of the first publicly
29+
available distribution of a specific version of the Licensed Work under this
30+
License, whichever comes first, the Licensor hereby grants you rights under
31+
the terms of the Change License, and the rights granted in the paragraph
32+
above terminate.
33+
34+
If your use of the Licensed Work does not comply with the requirements
35+
currently in effect as described in this License, you must purchase a
36+
commercial license from the Licensor, its affiliated entities, or authorized
37+
resellers, or you must refrain from using the Licensed Work.
38+
39+
All copies of the original and modified Licensed Work, and derivative works
40+
of the Licensed Work, are subject to this License. This License applies
41+
separately for each version of the Licensed Work and the Change Date may vary
42+
for each version of the Licensed Work released by Licensor.
43+
44+
You must conspicuously display this License on each original or modified copy
45+
of the Licensed Work. If you receive the Licensed Work in original or
46+
modified form from a third party, the terms and conditions set forth in this
47+
License apply to your use of that work.
48+
49+
Any use of the Licensed Work in violation of this License will automatically
50+
terminate your rights under this License for the current and all other
51+
versions of the Licensed Work.
52+
53+
This License does not grant you any right in any trademark or logo of
54+
Licensor or its affiliates (provided that you may use a trademark or logo of
55+
Licensor as expressly required by this License). TO THE EXTENT PERMITTED BY
56+
APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN “AS IS” BASIS. LICENSOR
57+
HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING
58+
(WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
59+
PURPOSE, NON-INFRINGEMENT, AND TITLE. MariaDB hereby grants you permission to
60+
use this License’s text to license your works, and to refer to it using the
61+
trademark “Business Source License”, as long as you comply with the Covenants
62+
of Licensor below.
63+
64+
Covenants of Licensor
65+
66+
In consideration of the right to use this License’s text and the “Business
67+
Source License” name and trademark, Licensor covenants to MariaDB, and to all
68+
other recipients of the licensed work to be provided by Licensor:
69+
70+
1. To specify as the Change License the GPL Version 2.0 or any later version,
71+
or a license that is compatible with GPL Version 2.0 or a later version,
72+
where “compatible” means that software provided under the Change License can
73+
be included in a program with software provided under GPL Version 2.0 or a
74+
later version. Licensor may specify additional Change Licenses without
75+
limitation.
76+
77+
2. To either: (a) specify an additional grant of rights to use that does not
78+
impose any additional restriction on the right granted in this License, as
79+
the Additional Use Grant; or (b) insert the text “None”.
80+
81+
3. To specify a Change Date not later than the fourth anniversary of the first
82+
publicly available distribution of a specific version of the Licensed Work
83+
under this License.
84+
85+
4. Not to modify this License in any other way.
86+
87+
Notice
88+
89+
The Business Source License (this document, or the “License”) is not an Open
90+
Source license. However, the Licensed Work will eventually be made available
91+
under an Open Source License, as stated in this License.
92+
93+
For more information on the use of the Business Source License for MariaDB
94+
products, please visit the MariaDB Business Source License FAQ.
95+
For more information on the use of the Business Source License generally,
96+
please visit the Adopting and Developing Business Source License FAQ.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Hawk API
22

3+
![License](https://img.shields.io/badge/license-BSL--1.1-orange)
4+
35
## Start API
46
For deployment (both in production and in development), you can use Docker.
57
See Docker instructions [here](DOCKER.md).
@@ -29,3 +31,8 @@ You can view API Schema visualization in `/voyager` page in your browser. To see
2931

3032
Run `yarn migrations:up` command to apply migration revisions or
3133
`yarn migrations:down` to rollback the last revision.
34+
35+
## License
36+
37+
Source code is available under **Business Source License 1.1 (BSL 1.1)**.
38+
See [`LICENSE`](./LICENSE) for terms, including:

docs/METRICS.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Prometheus Metrics
2+
3+
This application exposes Prometheus-compatible metrics on a separate port from the main API server.
4+
5+
## Configuration
6+
7+
The metrics server runs on a separate port configured via the `METRICS_PORT` environment variable:
8+
9+
```bash
10+
# Default: 9090
11+
METRICS_PORT=9090
12+
```
13+
14+
Add this to your `.env` file. See `.env.sample` for reference.
15+
16+
## Metrics Endpoint
17+
18+
The metrics are served at:
19+
20+
```
21+
http://localhost:9090/metrics
22+
```
23+
24+
(Replace `9090` with your configured `METRICS_PORT` if different)
25+
26+
## Available Metrics
27+
28+
### Default Node.js Metrics
29+
30+
The following default Node.js metrics are automatically collected:
31+
32+
- **nodejs_version_info** - Node.js version information
33+
- **process_cpu_user_seconds_total** - Total user CPU time spent in seconds
34+
- **process_cpu_system_seconds_total** - Total system CPU time spent in seconds
35+
- **nodejs_heap_size_total_bytes** - Total heap size in bytes
36+
- **nodejs_heap_size_used_bytes** - Used heap size in bytes
37+
- **nodejs_external_memory_bytes** - External memory in bytes
38+
- **nodejs_heap_space_size_total_bytes** - Total heap space size in bytes
39+
- **nodejs_heap_space_size_used_bytes** - Used heap space size in bytes
40+
- **nodejs_eventloop_lag_seconds** - Event loop lag in seconds
41+
- **nodejs_eventloop_lag_min_seconds** - Minimum event loop lag
42+
- **nodejs_eventloop_lag_max_seconds** - Maximum event loop lag
43+
- **nodejs_eventloop_lag_mean_seconds** - Mean event loop lag
44+
- **nodejs_eventloop_lag_stddev_seconds** - Standard deviation of event loop lag
45+
- **nodejs_eventloop_lag_p50_seconds** - 50th percentile event loop lag
46+
- **nodejs_eventloop_lag_p90_seconds** - 90th percentile event loop lag
47+
- **nodejs_eventloop_lag_p99_seconds** - 99th percentile event loop lag
48+
49+
### Custom HTTP Metrics
50+
51+
#### http_request_duration_seconds (Histogram)
52+
53+
Duration of HTTP requests in seconds, labeled by:
54+
- `method` - HTTP method (GET, POST, etc.)
55+
- `route` - Request route/path
56+
- `status_code` - HTTP status code
57+
58+
Buckets: 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10 seconds
59+
60+
#### http_requests_total (Counter)
61+
62+
Total number of HTTP requests, labeled by:
63+
- `method` - HTTP method (GET, POST, etc.)
64+
- `route` - Request route/path
65+
- `status_code` - HTTP status code
66+
67+
## Testing
68+
69+
### Manual Testing
70+
71+
You can test the metrics endpoint using curl:
72+
73+
```bash
74+
curl http://localhost:9090/metrics
75+
```
76+
77+
Or run the provided test script:
78+
79+
```bash
80+
./test-metrics.sh
81+
```
82+
83+
### Integration Tests
84+
85+
Integration tests for metrics are located in `test/integration/cases/metrics.test.ts`.
86+
87+
Run them with:
88+
89+
```bash
90+
npm run test:integration
91+
```
92+
93+
## Implementation Details
94+
95+
The metrics implementation uses the `prom-client` library and consists of:
96+
97+
1. **Metrics Module** (`src/metrics/index.ts`):
98+
- Initializes a Prometheus registry
99+
- Configures default Node.js metrics collection
100+
- Defines custom HTTP metrics (duration histogram and request counter)
101+
- Provides middleware for tracking HTTP requests
102+
- Creates a separate Express app for serving metrics
103+
104+
2. **Integration** (`src/index.ts`):
105+
- Adds metrics middleware to the main Express app
106+
- Starts metrics server on a separate port
107+
- Keeps metrics server isolated from main API traffic
108+
109+
## Prometheus Configuration
110+
111+
To scrape these metrics with Prometheus, add the following to your `prometheus.yml`:
112+
113+
```yaml
114+
scrape_configs:
115+
- job_name: 'hawk-api'
116+
static_configs:
117+
- targets: ['localhost:9090']
118+
```
119+
120+
Adjust the target host and port according to your deployment.

0 commit comments

Comments
 (0)