Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrations/chat/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RUN pnpm --filter @botpresshub/chat run build
FROM node:${NODE_VERSION}-bullseye-slim AS deploy

COPY --from=base /usr/app/integrations/chat/.botpress/dist/index.cjs ./index.cjs
RUN echo "const server = require('./index.cjs')\nconst port = process.env.PORT ? parseInt(process.env.PORT, 10) : 8081\nserver.default.start(port)" > server.js
COPY integrations/chat/server.js ./server.js

# set port env and expose it
ENV PORT=8081
Expand Down
1 change: 1 addition & 0 deletions integrations/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"chalk": "^4.1.2",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"prom-client": "^15.1.3",
"qs": "^6.11.0",
"uuid": "^9.0.0"
},
Expand Down
9 changes: 9 additions & 0 deletions integrations/chat/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const server = require("./index.cjs");

const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 8081;
const metricsPort = process.env.METRICS_PORT ? parseInt(process.env.METRICS_PORT, 10) : 9090;

if (process.env.METRICS_ENABLED !== "false") {
server.default.startMetricsServer(metricsPort);
}
server.default.start(port);
40 changes: 28 additions & 12 deletions integrations/chat/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Request } from '@botpress/sdk'
import * as api from './api'
import { extraRoutes } from './extra-routes'
import { handleRequest } from './gen/handler'
import { handleRequest, Router } from './gen/handler'
import { httpRequestsTotal, httpRequestDuration } from './metrics'
import { Handler } from './types'

const isPushpinRequest = (req: Request) => 'grip-sig' in req.headers

const apiRoutes = api.routes as Record<string, Record<string, api.Route>>
const routes = { ...apiRoutes, ...extraRoutes }
const router = new Router(Object.keys(routes))

export const makeHandler =
(props: api.OperationTools): Handler =>
async (args) => {
const apiRoutes = api.routes as Record<string, Record<string, api.Route>>
const routes = { ...apiRoutes, ...extraRoutes }

if (args.req.method.toLowerCase() === 'options') {
// preflight request
return {
Expand All @@ -28,13 +30,27 @@ export const makeHandler =
}
}

const match = router.match(args.req.path)
const normalizedPath = match?.path ?? 'not_found'
const method = args.req.method.toLowerCase()

const { auth, signals, convIdStore, userIdStore, apiUtils } = props
return handleRequest(routes, {
...args,
auth,
signals,
convIdStore,
userIdStore,
apiUtils,
})
const start = performance.now()
let statusCode = '500'
try {
const response = await handleRequest(routes, {
...args,
auth,
signals,
convIdStore,
userIdStore,
apiUtils,
})
statusCode = String(response.status ?? 200)
return response
} finally {
const durationSeconds = (performance.now() - start) / 1000
httpRequestsTotal.inc({ method, status_code: statusCode, path: normalizedPath })
httpRequestDuration.observe({ method, status_code: statusCode, path: normalizedPath }, durationSeconds)
}
}
7 changes: 6 additions & 1 deletion integrations/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuthKeyHandler } from './auth-key'
import * as debug from './debug'
import { makeHandler } from './handler'
import { MemorySpace, ChatIdStore, InMemoryChatIdStore, DynamoDbChatIdStore } from './id-store'
import { startMetricsServer } from './metrics-server'
import { Options, options } from './options'
import { CompositeSignalEmiter, PushpinEmitter, SignalEmitter, WebhookEmitter } from './signal-emitter'
import { initTracing, normalizePath, runWithSpan, setSpanAttributes, SPAN_ATTRS } from './tracing'
Expand Down Expand Up @@ -156,7 +157,11 @@ const emitEvent = async (args: ActionArgs) => {
})
}

export default new bp.Integration({
class IntegrationWithMetrics extends bp.Integration {
public startMetricsServer = startMetricsServer
}

export default new IntegrationWithMetrics({
register: async () => {},
unregister: async () => {},
__advanced: {
Expand Down
39 changes: 39 additions & 0 deletions integrations/chat/src/metrics-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as http from 'http'
import { collectDefaultMetrics } from 'prom-client'
import { registry } from './metrics'

export const startMetricsServer = (port: number) => {
collectDefaultMetrics({ register: registry })
const server = http.createServer((req, res) => {
void (async () => {
try {
if (req.url === '/health') {
res.writeHead(200).end('ok')
return
}

if (req.url === '/metrics') {
const metrics = await registry.metrics()
res.writeHead(200, { 'Content-Type': registry.contentType })
res.end(metrics)
return
}

res.writeHead(404).end('Not Found')
} catch (err) {
console.error('Metrics server error:', err)
if (!res.headersSent) {
res.writeHead(500).end('Internal Server Error')
}
}
})()
})

server.on('error', (err) => {
console.error(`Metrics server failed to start: ${err.message}`)
})

server.listen(port, () => {
console.log(`Metrics server listening on port ${port}`)
})
}
18 changes: 18 additions & 0 deletions integrations/chat/src/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Registry, Counter, Histogram } from 'prom-client'

export const registry = new Registry()

export const httpRequestsTotal = new Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'status_code', 'path'],
registers: [registry],
})

export const httpRequestDuration = new Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'status_code', 'path'],
buckets: [0.05, 0.1, 0.5, 1, 3, 10, 60, 120],
registers: [registry],
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "^3.564.0",
"@botpress/api": "1.90.0",
"@botpress/api": "1.91.0",
"@botpress/cli": "workspace:*",
"@botpress/client": "workspace:*",
"@botpress/sdk": "workspace:*",
Expand Down
Loading
Loading