@platformatic/service 1.29.0 → 1.31.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/config.d.ts CHANGED
@@ -145,7 +145,7 @@ export interface PlatformaticService {
145
145
  port?: number | string;
146
146
  hostname?: string;
147
147
  endpoint?: string;
148
- server?: "own" | "parent";
148
+ server?: "own" | "parent" | "hide";
149
149
  defaultMetrics?: {
150
150
  enabled: boolean;
151
151
  };
@@ -104,16 +104,25 @@ const metricsPlugin = fp(async function (app, opts = {}) {
104
104
  })
105
105
  }
106
106
 
107
+ function cleanMetrics () {
108
+ const metrics = app.metrics.client.register._metrics
109
+ for (const metricName in metrics) {
110
+ if (defaultMetrics.enabled || metricName.startsWith(prefix)) {
111
+ delete metrics[metricName]
112
+ }
113
+ }
114
+ }
115
+
107
116
  let isRestarting = false
108
117
  app.addHook('onReady', async () => {
109
118
  app.addPreRestartHook(async () => {
110
119
  isRestarting = true
111
- app.metrics.client.register.clear()
120
+ cleanMetrics()
112
121
  })
113
122
  })
114
123
  app.addHook('onClose', async () => {
115
124
  if (!isRestarting) {
116
- app.metrics.client.register.clear()
125
+ cleanMetrics()
117
126
  }
118
127
  })
119
128
  }, {
@@ -177,38 +186,41 @@ module.exports = fp(async function (app, opts) {
177
186
  await closeMetricsServer()
178
187
  }
179
188
 
180
- let onRequestHook
181
- if (auth) {
182
- const { username, password } = auth
189
+ if (server !== 'hide') {
190
+ let onRequestHook
191
+ if (auth) {
192
+ const { username, password } = auth
193
+
194
+ await metricsServer.register(require('@fastify/basic-auth'), {
195
+ validate: function (user, pass, req, reply, done) {
196
+ if (username !== user || password !== pass) {
197
+ return reply.code(401).send({ message: 'Unauthorized' })
198
+ }
199
+ return done()
200
+ }
201
+ })
202
+ onRequestHook = metricsServer.basicAuth
203
+ }
183
204
 
184
- await metricsServer.register(require('@fastify/basic-auth'), {
185
- validate: function (user, pass, req, reply, done) {
186
- if (username !== user || password !== pass) {
187
- return reply.code(401).send({ message: 'Unauthorized' })
205
+ metricsServer.register(require('@fastify/accepts'))
206
+
207
+ metricsServer.route({
208
+ url: metricsEndpoint,
209
+ method: 'GET',
210
+ logLevel: 'warn',
211
+ onRequest: onRequestHook,
212
+ handler: async (req, reply) => {
213
+ const promRegistry = app.metrics.client.register
214
+ const accepts = req.accepts()
215
+ if (!accepts.type('text/plain') && accepts.type('application/json')) {
216
+ return promRegistry.getMetricsAsJSON()
188
217
  }
189
- return done()
218
+ reply.type('text/plain')
219
+ return promRegistry.metrics()
190
220
  }
191
221
  })
192
- onRequestHook = metricsServer.basicAuth
193
222
  }
194
223
 
195
- metricsServer.register(require('@fastify/accepts'))
196
- metricsServer.route({
197
- url: metricsEndpoint,
198
- method: 'GET',
199
- logLevel: 'warn',
200
- onRequest: onRequestHook,
201
- handler: async (req, reply) => {
202
- const promRegistry = app.metrics.client.register
203
- const accepts = req.accepts()
204
- if (!accepts.type('text/plain') && accepts.type('application/json')) {
205
- return promRegistry.getMetricsAsJSON()
206
- }
207
- reply.type('text/plain')
208
- return promRegistry.metrics()
209
- }
210
- })
211
-
212
224
  if (server === 'own') {
213
225
  await metricsServer.ready()
214
226
  }
package/lib/schema.js CHANGED
@@ -558,7 +558,7 @@ const metrics = {
558
558
  endpoint: { type: 'string' },
559
559
  server: {
560
560
  type: 'string',
561
- enum: ['own', 'parent']
561
+ enum: ['own', 'parent', 'hide']
562
562
  },
563
563
  defaultMetrics: {
564
564
  type: 'object',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "1.29.0",
3
+ "version": "1.31.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -78,13 +78,13 @@
78
78
  "semgrator": "^0.3.0",
79
79
  "ua-parser-js": "^1.0.37",
80
80
  "undici": "^6.9.0",
81
- "@platformatic/authenticate": "1.29.0",
82
- "@platformatic/config": "1.29.0",
83
- "@platformatic/client": "1.29.0",
84
- "@platformatic/scalar-theme": "1.29.0",
85
- "@platformatic/generators": "1.29.0",
86
- "@platformatic/telemetry": "1.29.0",
87
- "@platformatic/utils": "1.29.0"
81
+ "@platformatic/authenticate": "1.31.0",
82
+ "@platformatic/client": "1.31.0",
83
+ "@platformatic/generators": "1.31.0",
84
+ "@platformatic/config": "1.31.0",
85
+ "@platformatic/scalar-theme": "1.31.0",
86
+ "@platformatic/telemetry": "1.31.0",
87
+ "@platformatic/utils": "1.31.0"
88
88
  },
89
89
  "standard": {
90
90
  "ignore": [
package/schema.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "https://platformatic.dev/schemas/v1.29.0/service",
3
- "version": "1.29.0",
2
+ "$id": "https://platformatic.dev/schemas/v1.31.0/service",
3
+ "version": "1.31.0",
4
4
  "title": "Platformatic Service",
5
5
  "type": "object",
6
6
  "properties": {
@@ -613,7 +613,8 @@
613
613
  "type": "string",
614
614
  "enum": [
615
615
  "own",
616
- "parent"
616
+ "parent",
617
+ "hide"
617
618
  ]
618
619
  },
619
620
  "defaultMetrics": {