diff --git a/README.md b/README.md index a6babac..feb8cb4 100644 --- a/README.md +++ b/README.md @@ -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' } } @@ -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 diff --git a/package.json b/package.json index f4c1449..18cce47 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "2.0.0", + "version": "2.1.0", "keywords": [], "type": "commonjs", "exports": { diff --git a/server/src/bootstrap.ts b/server/src/bootstrap.ts new file mode 100644 index 0000000..dfb8fca --- /dev/null +++ b/server/src/bootstrap.ts @@ -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>) => number)() + } + }); +}; diff --git a/server/src/config/index.ts b/server/src/config/index.ts index 53f95f2..2377df0 100644 --- a/server/src/config/index.ts +++ b/server/src/config/index.ts @@ -8,7 +8,7 @@ export interface Config { export default { default: { - collectDefaultMetrics: { prefix: 'strapi_' }, + collectDefaultMetrics: { prefix: '' }, labels: [], server: { port: 9000, diff --git a/server/src/index.ts b/server/src/index.ts index f63b230..ebd8281 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -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 };