Skip to content

Commit

Permalink
Merge pull request #29 from XanderD99/model_lifecycle_metric
Browse files Browse the repository at this point in the history
Model lifecycle metric
  • Loading branch information
XanderD99 authored Nov 18, 2024
2 parents 9662da0 + e0d850c commit bd113b7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = [
enabled: true,
config: {
// see collectDefaultMetricsOption of prom-client
collectDefaultMetrics: false || { prefix: 'strapi_' }
collectDefaultMetrics: false || { prefix: '' }
labels: { name: "strapi-prometheus" },
server: false || { port: 9000, host: '0.0.0.0', path: '/metrics' }
}
Expand All @@ -41,15 +41,16 @@ module.exports = [

## 📊 Metrics

|name|description|type|buckets|
|---|---|---|---|
|http_request_duration_seconds|Duration of HTTP requests in seconds|Histogram|`exponentialBuckets(.001, 1.5, 12)`. Buckets from 1ms up to 10 seconds|
|http_request_content_length_bytes|Histogram of the size of payloads sent to the server, measured in bytes.|Histogram|`exponentialBuckets(512000, 2, 10)`. Buckets from 500KB up to ~500MB|
|http_response_content_length_bytes|Histogram of the size of payloads sent by the server, measured in bytes.|Histogram|`exponentialBuckets(512000, 2, 10)`. Buckets from 500KB up to ~500MB|
|http_requests_total|Total number of HTTP requests|Counter||
|http_active_requests|Number of active HTTP requests|Gauge||
|http_errors_total|Total number of HTTP errors|Counter||
|strapi_version_info|Strapi version info|Gauge||
|name|description|type|
|---|---|---|
|http_request_duration_seconds|Duration of HTTP requests in seconds|Histogram|
|http_request_content_length_bytes|Histogram of the size of payloads sent to the server, measured in bytes.|Histogram|
|http_response_content_length_bytes|Histogram of the size of payloads sent by the server, measured in bytes.|Histogram|
|http_requests_total|Total number of HTTP requests|Counter|
|http_active_requests|Number of active HTTP requests|Gauge|
|http_errors_total|Total number of HTTP errors|Counter|
|strapi_version_info|Strapi version info|Gauge|
|lifecycle_duration_seconds|'Tracks the duration of Strapi lifecycle events in seconds|Histogram|

## 👮‍♀️ Security

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.0",
"version": "2.1.0",
"keywords": [],
"type": "commonjs",
"exports": {
Expand Down
51 changes: 51 additions & 0 deletions server/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Core } from '@strapi/strapi';
import { Histogram } from 'prom-client';


const lifecycleDurationSeconds = new Histogram({
name: 'lifecycle_duration_seconds',
help: 'Tracks the duration of Strapi lifecycle events in seconds.',
labelNames: ['model', 'event'],
buckets: [
0.001, // 1 ms
0.005, // 5 ms
0.01, // 10 ms
0.05, // 50 ms
0.1, // 100 ms
0.2, // 200 ms
0.5, // 500 ms
1, // 1 second
2, // 2 seconds
5, // 5 seconds
10, // 10 seconds
20, // 20 seconds
30, // 30 seconds
60 // 1 minute
]
});

function formatActionName(action: string): string {
// Remove 'before' or 'after' from the start of the action name
const modifiedAction = action.replace(/^(before|after)/, '');

// Lowercase the first letter of the remaining string
return modifiedAction.charAt(0).toLowerCase() + modifiedAction.slice(1);
}


export default ({ strapi }: { strapi: Core.Strapi }) => {
strapi.db!.lifecycles.subscribe((event) => {
if (event.action.startsWith('before')) {
const labels = {
event: formatActionName(event.action),
model: event.model.singularName,
};

event.state.end = lifecycleDurationSeconds.startTimer(labels);
}

if (event.action.startsWith('after') && event.state.end) {
(event.state.end as (labels?: Partial<Record<"model" | "hook", string | number>>) => number)()
}
});
};
2 changes: 1 addition & 1 deletion server/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Config {

export default {
default: {
collectDefaultMetrics: { prefix: 'strapi_' },
collectDefaultMetrics: { prefix: '' },
labels: [],
server: {
port: 9000,
Expand Down
3 changes: 2 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* Application methods
*/
import register from './register';
import bootstrap from './bootstrap';

/**
* Plugin server methods
*/
import config from './config';

export default { register, config };
export default { register, bootstrap, config };

0 comments on commit bd113b7

Please sign in to comment.