-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathadmin-app.ts
50 lines (39 loc) · 1.77 KB
/
admin-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import fastify, { FastifyInstance, FastifyServerOptions } from 'fastify'
import { routes, plugins, setErrorHandler } from './http'
import { Registry } from 'prom-client'
const build = (opts: FastifyServerOptions = {}, appInstance?: FastifyInstance): FastifyInstance => {
const app = fastify(opts)
app.register(plugins.signals)
app.register(plugins.adminTenantId)
app.register(plugins.logRequest({ excludeUrls: ['/status', '/metrics', '/health'] }))
app.register(routes.tenants, { prefix: 'tenants' })
app.register(routes.objects, { prefix: 'tenants' })
app.register(routes.migrations, { prefix: 'migrations' })
app.register(routes.s3Credentials, { prefix: 's3' })
let registriesToMerge: Registry[] = []
if (appInstance) {
app.get('/metrics', async (req, reply) => {
if (registriesToMerge.length === 0) {
const globalRegistry = appInstance.metrics.client.register
const defaultRegistries = (appInstance.metrics as any).getCustomDefaultMetricsRegistries()
const routeRegistries = (appInstance.metrics as any).getCustomRouteMetricsRegistries()
registriesToMerge = Array.from(
new Set([globalRegistry, ...defaultRegistries, ...routeRegistries])
)
}
if (registriesToMerge.length === 1) {
const data = await registriesToMerge[0].metrics()
return reply.type(registriesToMerge[0].contentType).send(data)
}
const merged = appInstance.metrics.client.Registry.merge(registriesToMerge)
const data = await merged.metrics()
return reply.type(merged.contentType).send(data)
})
} else {
app.register(plugins.metrics({ enabledEndpoint: true }))
}
app.get('/status', async (_, response) => response.status(200).send())
setErrorHandler(app)
return app
}
export default build