@platformatic/service 1.24.0 → 1.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/plugins/metrics.js +69 -67
  2. package/package.json +10 -10
@@ -1,51 +1,14 @@
1
1
  'use strict'
2
2
 
3
- const http = require('http')
4
- const { eventLoopUtilization } = require('perf_hooks').performance
3
+ const http = require('node:http')
4
+ const { eventLoopUtilization } = require('node:perf_hooks').performance
5
+ const fastify = require('fastify')
5
6
  const fp = require('fastify-plugin')
6
- const metricsPlugin = require('fastify-metrics')
7
- const basicAuth = require('@fastify/basic-auth')
8
- const fastifyAccepts = require('@fastify/accepts')
9
- const Fastify = require('fastify')
10
7
 
11
- // This is a global httpServer to match global
12
- // prometheus. It's an antipattern, so do
13
- // not use it elsewhere.
14
- let httpServer = null
15
-
16
- module.exports = fp(async function (app, opts) {
17
- const server = opts.server ?? 'own'
18
- const hostname = opts.hostname ?? '0.0.0.0'
19
- const port = opts.port ?? 9090
20
- const metricsEndpoint = opts.endpoint ?? '/metrics'
21
- const auth = opts.auth ?? null
22
-
23
- let basicAuthValidator = null
24
- if (auth) {
25
- const { username, password } = auth
26
- basicAuthValidator = function (user, pass, req, reply, done) {
27
- if (username !== user || password !== pass) {
28
- return reply.code(401).send({ message: 'Unauthorized' })
29
- }
30
- return done()
31
- }
32
-
33
- if (server === 'parent') {
34
- await app.register(basicAuth, {
35
- validate: basicAuthValidator
36
- })
37
-
38
- app.addHook('onRoute', (routeOptions) => {
39
- if (routeOptions.url === metricsEndpoint) {
40
- routeOptions.onRequest = app.basicAuth
41
- }
42
- })
43
- }
44
- }
45
-
46
- app.register(metricsPlugin, {
8
+ const metricsPlugin = fp(async function (app) {
9
+ app.register(require('fastify-metrics'), {
47
10
  defaultMetrics: { enabled: true },
48
- endpoint: server === 'parent' ? metricsEndpoint : null,
11
+ endpoint: null,
49
12
  name: 'metrics',
50
13
  routeMetrics: { enabled: true },
51
14
  clearRegisterOnInit: true
@@ -73,14 +36,18 @@ module.exports = fp(async function (app, opts) {
73
36
 
74
37
  app.metrics.client.register.registerMetric(eluMetric)
75
38
  })
39
+ }, {
40
+ encapsulate: false
41
+ })
76
42
 
77
- if (httpServer && httpServer.address().port !== port) {
78
- await new Promise((resolve) => httpServer.close(resolve))
79
- httpServer = null
80
- }
43
+ // This is a global httpServer to match global
44
+ // prometheus. It's an antipattern, so do
45
+ // not use it elsewhere.
46
+ let httpServer = null
81
47
 
82
- if (server === 'parent') {
83
- return
48
+ async function createMetricsServer (app, hostname, port) {
49
+ if (httpServer && httpServer.address().port !== port) {
50
+ await closeMetricsServer()
84
51
  }
85
52
 
86
53
  if (!httpServer) {
@@ -89,7 +56,7 @@ module.exports = fp(async function (app, opts) {
89
56
  httpServer.unref()
90
57
  }
91
58
 
92
- const promServer = Fastify({
59
+ const promServer = fastify({
93
60
  name: 'Prometheus server',
94
61
  serverFactory: (handler) => {
95
62
  httpServer.removeAllListeners('request')
@@ -100,34 +67,69 @@ module.exports = fp(async function (app, opts) {
100
67
  logger: app.log.child({ name: 'prometheus' })
101
68
  })
102
69
 
103
- promServer.register(fastifyAccepts)
70
+ app.addHook('onClose', async () => {
71
+ await promServer.close()
72
+ })
73
+
74
+ return promServer
75
+ }
76
+
77
+ async function closeMetricsServer () {
78
+ if (httpServer) {
79
+ await new Promise((resolve) => httpServer.close(resolve))
80
+ httpServer = null
81
+ }
82
+ }
83
+
84
+ module.exports = fp(async function (app, opts) {
85
+ const server = opts.server ?? 'own'
86
+ const hostname = opts.hostname ?? '0.0.0.0'
87
+ const port = opts.port ?? 9090
88
+ const metricsEndpoint = opts.endpoint ?? '/metrics'
89
+ const auth = opts.auth ?? null
90
+
91
+ app.register(metricsPlugin)
92
+
93
+ let metricsServer = app
94
+ if (server === 'own') {
95
+ metricsServer = await createMetricsServer(app, hostname, port)
96
+ } else {
97
+ await closeMetricsServer()
98
+ }
104
99
 
105
- const metricsEndpointOptions = {
100
+ let onRequestHook
101
+ if (auth) {
102
+ const { username, password } = auth
103
+
104
+ await metricsServer.register(require('@fastify/basic-auth'), {
105
+ validate: function (user, pass, req, reply, done) {
106
+ if (username !== user || password !== pass) {
107
+ return reply.code(401).send({ message: 'Unauthorized' })
108
+ }
109
+ return done()
110
+ }
111
+ })
112
+ onRequestHook = metricsServer.basicAuth
113
+ }
114
+
115
+ metricsServer.register(require('@fastify/accepts'))
116
+ metricsServer.route({
106
117
  url: metricsEndpoint,
107
118
  method: 'GET',
108
119
  logLevel: 'warn',
120
+ onRequest: onRequestHook,
109
121
  handler: async (req, reply) => {
110
122
  const promRegistry = app.metrics.client.register
111
123
  const accepts = req.accepts()
112
124
  if (!accepts.type('text/plain') && accepts.type('application/json')) {
113
- return await promRegistry.getMetricsAsJSON()
125
+ return promRegistry.getMetricsAsJSON()
114
126
  }
115
127
  reply.type('text/plain')
116
- return await promRegistry.metrics()
128
+ return promRegistry.metrics()
117
129
  }
118
- }
119
-
120
- if (auth) {
121
- await promServer.register(basicAuth, {
122
- validate: basicAuthValidator
123
- })
124
- metricsEndpointOptions.onRequest = promServer.basicAuth
125
- }
126
- promServer.route(metricsEndpointOptions)
127
-
128
- app.addHook('onClose', async (instance) => {
129
- await promServer.close()
130
130
  })
131
131
 
132
- await promServer.ready()
132
+ if (server === 'own') {
133
+ await metricsServer.ready()
134
+ }
133
135
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "1.24.0",
3
+ "version": "1.26.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -54,7 +54,7 @@
54
54
  "ajv": "^8.12.0",
55
55
  "cli-progress": "^3.12.0",
56
56
  "close-with-grace": "^1.2.0",
57
- "code-block-writer": "^12.0.0",
57
+ "code-block-writer": "^13.0.0",
58
58
  "colorette": "^2.0.20",
59
59
  "commist": "^3.2.0",
60
60
  "console-table-printer": "^2.12.0",
@@ -77,14 +77,14 @@
77
77
  "rfdc": "^1.3.1",
78
78
  "ua-parser-js": "^1.0.37",
79
79
  "undici": "^6.6.0",
80
- "@platformatic/client": "1.24.0",
81
- "@platformatic/authenticate": "1.24.0",
82
- "@platformatic/config": "1.24.0",
83
- "@platformatic/generators": "1.24.0",
84
- "@platformatic/scalar-theme": "1.24.0",
85
- "@platformatic/telemetry": "1.24.0",
86
- "@platformatic/utils": "1.24.0",
87
- "@platformatic/metaconfig": "1.24.0"
80
+ "@platformatic/authenticate": "1.26.0",
81
+ "@platformatic/config": "1.26.0",
82
+ "@platformatic/generators": "1.26.0",
83
+ "@platformatic/client": "1.26.0",
84
+ "@platformatic/metaconfig": "1.26.0",
85
+ "@platformatic/telemetry": "1.26.0",
86
+ "@platformatic/scalar-theme": "1.26.0",
87
+ "@platformatic/utils": "1.26.0"
88
88
  },
89
89
  "standard": {
90
90
  "ignore": [