Ghost/ghost
Chris Raible 768336efad
Added a prometheus metrics exporter to Ghost (#21193)
ref
https://linear.app/tryghost/issue/ENG-1505/add-prometheus-metrics-server-to-allow-monitoring-ghost-metrics

# Summary
This commit includes two main components: a prometheus client class to
collect metrics from Ghost, and a standalone metrics server that exposes
a /metrics endpoint at a separate port (9416 by default) from the main
Ghost app.

The prometheus client is a very thin wrapper around
[prom-client](https://github.com/siimon/prom-client). We could use
prom-client directly, but this approach should make it easier to switch
to a different prometheus client package (or make our own) if we ever
need to down the line.

The list of default metrics this enables is specified in an e2e test
[here](https://github.com/TryGhost/Ghost/pull/21192/files#diff-ebc52236be2cd14b40be89220ae961f48d3f837693f7d1da76db292348915941R66-R92).
This also gives us the ability to create and collect custom metrics,
although none are included in this commit yet.

# Configuration
The prometheus client and the metrics server are both disabled by
default, but can be enabled by setting the metrics_server:enabled flag
to true.

You can also define a custom host and port using `metrics_server:host`
and `metrics_server:port`.

## Why not expose the /metrics endpoint in one of the existing express
apps?
The standalone express app exists for two main reasons:

1. We don't want these metrics to be public, and the easiest way to
accomplish that is to expose the /metrics endpoint at a different port
that won't be exposed to the internet.

2. Creating a standalone express instance decouples the metrics endpoint
from the Ghost server, so if Ghost is not responding for whatever
reason, we should still be able to scrape metrics to understand what's
going on internally.


## Impact on Boot & Shut down time
The prometheus client is initialized early in the boot process so we can
collect metrics during the boot sequence. Testing locally has shown that
this increases boot time by ~20ms. The metrics server which exposes the
/metrics endpoint is not initialized until after the background
services, and it is not awaited, to avoid impacting boot time. None of
this code, including the requires, will run if the
metrics_server:enabled flag is set to false (or not set).

Shutting down the metrics server is added as a cleanup task for the main
Ghost server instance, and is setup to shut down with 0 grace period to
avoid impacting shut down time.
2024-10-03 11:34:25 -07:00
..
adapter-cache-memory-ttl Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
adapter-cache-redis Added an optional timeout parameter to AdapterCacheRedis (#20131) 2024-05-02 20:39:23 -07:00
adapter-manager Update TryGhost packages 2024-08-05 12:12:34 +02:00
admin Pin dependency i18n-iso-countries to 7.12.0 2024-10-02 07:28:28 +00:00
announcement-bar-settings Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
api-framework Update TryGhost packages 2024-08-05 12:12:34 +02:00
api-version-compatibility-service Fixed handling requests with mismatching version and missing key 2024-05-02 13:03:26 +02:00
audience-feedback 🔒 Added uuid verification to member endpoints not requiring a session 2024-08-20 16:24:02 +02:00
bookshelf-repository 🐛 Fixed member filtering for "Unsubscribed from newsletter" filters (#20926) 2024-09-16 11:16:49 +02:00
bootstrap-socket Update TryGhost packages 2024-08-05 12:12:34 +02:00
collections 🐛 Fixed member filtering for "Unsubscribed from newsletter" filters (#20926) 2024-09-16 11:16:49 +02:00
constants Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
core Added a prometheus metrics exporter to Ghost (#21193) 2024-10-03 11:34:25 -07:00
custom-theme-settings-service 🐛 Fixed member filtering for "Unsubscribed from newsletter" filters (#20926) 2024-09-16 11:16:49 +02:00
data-generator Update TryGhost packages 2024-08-05 12:12:34 +02:00
domain-events Update TryGhost packages 2024-08-05 12:12:34 +02:00
donations Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
dynamic-routing-events Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
email-addresses Update dependency nodemailer to v6.9.15 2024-09-03 16:26:49 +00:00
email-analytics-provider-mailgun Reimplemented email analytics prioritizing email opens (#20914) 2024-09-05 08:10:07 -05:00
email-analytics-service Fixed Date.now() use in email analytics unit tests (#21188) 2024-10-02 17:54:48 +00:00
email-content-generator Lazyloaded several required dependencies 2024-07-26 09:52:26 +02:00
email-events Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
email-service Update dependency html-validate to v8.24.0 2024-09-25 08:23:04 +02:00
email-suppression-list Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
express-dynamic-redirects Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
external-media-inliner Add Lexical support to the external-media-inliner (#19149) 2023-11-29 17:56:03 +00:00
extract-api-key Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
ghost Update dependency express to v4.21.0 2024-09-12 07:37:27 +02:00
html-to-plaintext Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
i18n Updated Norwegian i18n string order (#21210) 2024-10-03 17:48:16 +00:00
importer-handler-content-files Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
importer-revue Update Koenig packages 2024-08-14 15:34:44 +01:00
in-memory-repository 🐛 Fixed member filtering for "Unsubscribed from newsletter" filters (#20926) 2024-09-16 11:16:49 +02:00
job-manager Update dependency @sinonjs/fake-timers to v11.3.1 2024-08-26 14:08:32 +02:00
link-redirects Updated documentation for LinkRedirects (#20378) 2024-06-12 23:27:15 +00:00
link-replacer Pinned dependencies (#20257) 2024-05-27 15:29:32 +00:00
link-tracking 🐛 Fixed member filtering for "Unsubscribed from newsletter" filters (#20926) 2024-09-16 11:16:49 +02:00
magic-link Added Sentry message logging for generating magic links (#20789) 2024-08-20 08:38:12 +01:00
mail-events Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
mailgun-client Added configurable target delivery window for batch sending (#20719) 2024-09-05 22:28:40 -07:00
member-attribution Fixed extra arguments being supplied to function calls 2024-05-07 11:44:07 +02:00
member-events 🎨 Added staff notification when a sub is canceled due to failed payments (#20534) 2024-07-15 08:07:18 +02:00
members-api 🎨 Added a maximum limit of 100 for GET members admin endpoint (#20643) 2024-09-18 11:17:56 +00:00
members-csv Update dependency pump to v3.0.2 2024-09-11 10:16:42 +00:00
members-events-service Added caching to LastSeenAtUpdater (#20964) 2024-09-13 00:54:43 -07:00
members-importer Update TryGhost packages 2024-08-05 12:12:34 +02:00
members-ssr Update TryGhost packages 2024-08-05 12:12:34 +02:00
mentions-email-report Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
metrics-server Added a prometheus metrics exporter to Ghost (#21193) 2024-10-03 11:34:25 -07:00
milestones Update TryGhost packages 2024-08-05 12:12:34 +02:00
minifier Update dependency terser to v5.34.1 2024-09-30 08:54:20 +00:00
model-to-domain-event-interceptor Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
mw-api-version-mismatch Update TryGhost packages 2024-08-05 12:12:34 +02:00
mw-cache-control Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
mw-error-handler Update TryGhost packages 2024-08-05 12:12:34 +02:00
mw-session-from-token Update dependency express to v4.21.0 2024-09-12 07:37:27 +02:00
mw-update-user-last-seen Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
mw-version-match Update TryGhost packages 2024-08-05 12:12:34 +02:00
mw-vhost Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
nql-filter-expansions Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
oembed-service 🐛 Fixed fetching and storing bookmark card icons and thumbnails (#21036) 2024-09-19 15:52:08 +05:30
offers Update TryGhost packages 2024-08-05 12:12:34 +02:00
package-json Update TryGhost packages 2024-08-05 12:12:34 +02:00
payments 🐛 Fixed Tips & Donations checkout error for sites with long titles 2024-09-03 21:08:24 +01:00
post-events Added unschedule bulk action to posts (#20945) 2024-09-24 15:32:45 +08:00
post-revisions Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
posts-service Added unschedule bulk action to posts (#20945) 2024-09-24 15:32:45 +08:00
recommendations Switched build and build:ts scripts 2024-09-02 15:20:03 +02:00
referrers Configured all unit tests to use dot reporter 2023-10-05 12:24:24 +02:00
security Updated uuid to crypto.randomUUID() (#20821) 2024-09-12 09:09:30 +07:00
session-service Update dependency express to v4.21.0 2024-09-12 07:37:27 +02:00
settings-path-manager Update TryGhost packages 2024-08-05 12:12:34 +02:00
slack-notifications Update TryGhost packages 2024-08-05 12:12:34 +02:00
staff-service Updated notification email mobile styles 2024-08-29 17:15:45 +01:00
stats-service Update dependency luxon to v3.5.0 2024-08-05 09:19:48 +02:00
stripe Cleaned up comments after Stripe Controller refactor (#20943) 2024-09-09 05:31:52 +00:00
tiers Update TryGhost packages 2024-08-05 12:12:34 +02:00
tinybird Fix origin attribution in Tinybird analytics hits (#21187) 2024-10-02 17:27:43 +01:00
update-check-service Updated uuid to crypto.randomUUID() (#20821) 2024-09-12 09:09:30 +07:00
verification-trigger Update TryGhost packages 2024-08-05 12:12:34 +02:00
version-notifications-data-service Fixed handling requests with mismatching version and missing key 2024-05-02 13:03:26 +02:00
webmentions Update TryGhost packages 2024-08-05 12:12:34 +02:00
tsconfig.json Disabled TypeScript incremental building 2023-08-09 18:27:56 +02:00