@platformatic/service 1.16.0 → 1.17.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.
- package/index.js +5 -0
- package/lib/plugins/metrics.js +47 -31
- package/lib/schema.js +5 -0
- package/package.json +11 -11
- package/.taprc +0 -2
package/index.js
CHANGED
|
@@ -28,6 +28,11 @@ async function platformaticService (app, opts) {
|
|
|
28
28
|
const beforePlugins = opts.beforePlugins || arguments[2] || []
|
|
29
29
|
|
|
30
30
|
if (isKeyEnabled('metrics', config)) {
|
|
31
|
+
if (config.metrics.server === 'own' && parseInt(config.server.port) === parseInt(config.metrics.port)) {
|
|
32
|
+
app.log.warn('In order to serve metrics on the same port as the core applicaton, set metrics.server to "parent".')
|
|
33
|
+
config.metrics.server = 'parent'
|
|
34
|
+
}
|
|
35
|
+
|
|
31
36
|
app.register(setupMetrics, config.metrics)
|
|
32
37
|
}
|
|
33
38
|
|
package/lib/plugins/metrics.js
CHANGED
|
@@ -8,26 +8,44 @@ const basicAuth = require('@fastify/basic-auth')
|
|
|
8
8
|
const fastifyAccepts = require('@fastify/accepts')
|
|
9
9
|
const Fastify = require('fastify')
|
|
10
10
|
|
|
11
|
-
// This is a global
|
|
11
|
+
// This is a global httpServer to match global
|
|
12
12
|
// prometheus. It's an antipattern, so do
|
|
13
13
|
// not use it elsewhere.
|
|
14
|
-
let
|
|
14
|
+
let httpServer = null
|
|
15
15
|
|
|
16
16
|
module.exports = fp(async function (app, opts) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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()
|
|
22
31
|
}
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
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
|
+
})
|
|
26
43
|
}
|
|
27
44
|
}
|
|
45
|
+
|
|
28
46
|
app.register(metricsPlugin, {
|
|
29
47
|
defaultMetrics: { enabled: true },
|
|
30
|
-
endpoint: null,
|
|
48
|
+
endpoint: server === 'parent' ? metricsEndpoint : null,
|
|
31
49
|
name: 'metrics',
|
|
32
50
|
routeMetrics: { enabled: true },
|
|
33
51
|
clearRegisterOnInit: true
|
|
@@ -56,24 +74,28 @@ module.exports = fp(async function (app, opts) {
|
|
|
56
74
|
app.metrics.client.register.registerMetric(eluMetric)
|
|
57
75
|
})
|
|
58
76
|
|
|
59
|
-
if (
|
|
60
|
-
await new Promise((resolve) =>
|
|
61
|
-
|
|
77
|
+
if (httpServer && httpServer.address().port !== port) {
|
|
78
|
+
await new Promise((resolve) => httpServer.close(resolve))
|
|
79
|
+
httpServer = null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (server === 'parent') {
|
|
83
|
+
return
|
|
62
84
|
}
|
|
63
85
|
|
|
64
|
-
if (!
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
86
|
+
if (!httpServer) {
|
|
87
|
+
httpServer = http.createServer()
|
|
88
|
+
httpServer.listen(port, hostname)
|
|
89
|
+
httpServer.unref()
|
|
68
90
|
}
|
|
69
91
|
|
|
70
92
|
const promServer = Fastify({
|
|
71
93
|
name: 'Prometheus server',
|
|
72
94
|
serverFactory: (handler) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return
|
|
95
|
+
httpServer.removeAllListeners('request')
|
|
96
|
+
httpServer.removeAllListeners('clientError')
|
|
97
|
+
httpServer.on('request', handler)
|
|
98
|
+
return httpServer
|
|
77
99
|
},
|
|
78
100
|
logger: app.log.child({ name: 'prometheus' })
|
|
79
101
|
})
|
|
@@ -81,7 +103,7 @@ module.exports = fp(async function (app, opts) {
|
|
|
81
103
|
promServer.register(fastifyAccepts)
|
|
82
104
|
|
|
83
105
|
const metricsEndpointOptions = {
|
|
84
|
-
url:
|
|
106
|
+
url: metricsEndpoint,
|
|
85
107
|
method: 'GET',
|
|
86
108
|
logLevel: 'warn',
|
|
87
109
|
handler: async (req, reply) => {
|
|
@@ -95,15 +117,9 @@ module.exports = fp(async function (app, opts) {
|
|
|
95
117
|
}
|
|
96
118
|
}
|
|
97
119
|
|
|
98
|
-
if (
|
|
99
|
-
const { username, password } = opts.auth
|
|
120
|
+
if (auth) {
|
|
100
121
|
await promServer.register(basicAuth, {
|
|
101
|
-
validate:
|
|
102
|
-
if (username !== user || password !== pass) {
|
|
103
|
-
return reply.code(401).send({ message: 'Unauthorized' })
|
|
104
|
-
}
|
|
105
|
-
return done()
|
|
106
|
-
}
|
|
122
|
+
validate: basicAuthValidator
|
|
107
123
|
})
|
|
108
124
|
metricsEndpointOptions.onRequest = promServer.basicAuth
|
|
109
125
|
}
|
package/lib/schema.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@fastify/aws-lambda": "^3.3.0",
|
|
21
21
|
"@fastify/compress": "^6.5.0",
|
|
22
22
|
"bindings": "^1.5.0",
|
|
23
|
-
"c8": "^
|
|
23
|
+
"c8": "^9.0.0",
|
|
24
24
|
"glob": "^10.3.10",
|
|
25
25
|
"json-schema-to-typescript": "^13.1.1",
|
|
26
26
|
"openapi-types": "^12.1.3",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"standard": "^17.1.0",
|
|
32
32
|
"strip-ansi": "^7.1.0",
|
|
33
33
|
"ts-standard": "^12.0.2",
|
|
34
|
-
"tsd": "^0.
|
|
34
|
+
"tsd": "^0.30.0",
|
|
35
35
|
"typescript": "^5.2.2",
|
|
36
36
|
"undici": "^6.0.0",
|
|
37
37
|
"vscode-json-languageservice": "^5.3.6",
|
|
@@ -76,14 +76,14 @@
|
|
|
76
76
|
"rfdc": "^1.3.0",
|
|
77
77
|
"ua-parser-js": "^1.0.36",
|
|
78
78
|
"undici": "^6.0.0",
|
|
79
|
-
"@platformatic/authenticate": "1.
|
|
80
|
-
"@platformatic/
|
|
81
|
-
"@platformatic/
|
|
82
|
-
"@platformatic/
|
|
83
|
-
"@platformatic/
|
|
84
|
-
"@platformatic/
|
|
85
|
-
"@platformatic/
|
|
86
|
-
"@platformatic/
|
|
79
|
+
"@platformatic/authenticate": "1.17.0",
|
|
80
|
+
"@platformatic/client": "1.17.0",
|
|
81
|
+
"@platformatic/config": "1.17.0",
|
|
82
|
+
"@platformatic/generators": "1.17.0",
|
|
83
|
+
"@platformatic/metaconfig": "1.17.0",
|
|
84
|
+
"@platformatic/scalar-theme": "1.17.0",
|
|
85
|
+
"@platformatic/telemetry": "1.17.0",
|
|
86
|
+
"@platformatic/utils": "1.17.0"
|
|
87
87
|
},
|
|
88
88
|
"standard": {
|
|
89
89
|
"ignore": [
|
package/.taprc
DELETED