Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Feb 22, 2024
2 parents f7f0fb0 + bd973a9 commit 474c3d1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions plugin-server/src/main/services/http-server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import express, { Request, Response } from 'express'
import { DateTime } from 'luxon'
import { IngestionConsumer, KafkaJSIngestionConsumer } from 'main/ingestion-queues/kafka-queue'
import * as prometheus from 'prom-client'

import { status } from '../../utils/status'
import { delay } from '../../utils/utils'

prometheus.collectDefaultMetrics()
const v8Profiler = require('v8-profiler-next')
v8Profiler.setGenerateType(1)

import express, { Request, Response } from 'express'

import { delay } from '../../utils/utils'

export const expressApp: express.Application = express()

export function setupCommonRoutes(
healthChecks: { [service: string]: () => Promise<boolean> | boolean },
analyticsEventsIngestionConsumer?: KafkaJSIngestionConsumer | IngestionConsumer
): express.Application {
expressApp.get('/_health', async (req, res) => {
expressApp.get('/_health', getHealth(healthChecks))
expressApp.get('/_ready', getReady(analyticsEventsIngestionConsumer))
expressApp.get('/_metrics', getMetrics)
expressApp.get('/metrics', getMetrics)
expressApp.get('/_profile/:type', getProfileByType)

return expressApp
}

const getHealth =
(healthChecks: { [service: string]: () => Promise<boolean> | boolean }) => async (req: Request, res: Response) => {
// Check that all health checks pass. Note that a failure of these
// _may_ result in the process being terminated by e.g. Kubernetes
// so the stakes are high.
Expand Down Expand Up @@ -68,9 +76,11 @@ export function setupCommonRoutes(
}

return res.status(statusCode).json({ status: statusCode === 200 ? 'ok' : 'error', checks: checkResultsMapping })
})
}

expressApp.get('/_ready', (req, res) => {
const getReady =
(analyticsEventsIngestionConsumer?: KafkaJSIngestionConsumer | IngestionConsumer) =>
(req: Request, res: Response) => {
// Check that, if the server should have a kafka queue,
// the Kafka consumer is ready to consume messages
if (!analyticsEventsIngestionConsumer || analyticsEventsIngestionConsumer.consumerReady) {
Expand All @@ -87,14 +97,7 @@ export function setupCommonRoutes(
status: 'error',
}
return res.status(503).json(responseBody)
})

expressApp.get('/_metrics', getMetrics)
expressApp.get('/metrics', getMetrics)
expressApp.get('/_profile/:type', getProfileByType)

return expressApp
}
}

const getMetrics = async (req: Request, res: Response) => {
try {
Expand Down

0 comments on commit 474c3d1

Please sign in to comment.