From 5b5d0d43a356ad83ead0cded2b3fe39162b9b190 Mon Sep 17 00:00:00 2001 From: Brett Hoerner Date: Fri, 15 Sep 2023 08:55:46 -0600 Subject: [PATCH] chore(plugin-server): make it easier to run multiple plugin-server instances locally (#17456) * chore(plugin-server): allow customizing the HTTP server port * chore(plugin-server): add NO_WATCH mode for development * fix: http-server test --- bin/plugin-server | 10 +++++++++- plugin-server/package.json | 1 + plugin-server/src/config/config.ts | 3 +++ plugin-server/src/main/pluginsServer.ts | 2 +- plugin-server/src/main/services/http-server.ts | 9 ++++----- plugin-server/src/types.ts | 1 + plugin-server/tests/http-server.test.ts | 6 +++--- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bin/plugin-server b/bin/plugin-server index 75000245ac5..157e7896fc9 100755 --- a/bin/plugin-server +++ b/bin/plugin-server @@ -46,7 +46,15 @@ if [ $? -ne 0 ]; then exit 1 fi -[[ -n $DEBUG ]] && cmd="pnpm start:dev" || cmd="node dist/index.js" +if [[ -n $DEBUG ]]; then + if [[ -n $NO_WATCH ]]; then + cmd="pnpm start:devNoWatch" + else + cmd="pnpm start:dev" + fi +else + cmd="node dist/index.js" +fi if [[ -n $NO_RESTART_LOOP ]]; then echo "▶️ Starting plugin server..." diff --git a/plugin-server/package.json b/plugin-server/package.json index be9bebdd9b2..e2d766f344b 100644 --- a/plugin-server/package.json +++ b/plugin-server/package.json @@ -11,6 +11,7 @@ "start": "pnpm start:dist", "start:dist": "BASE_DIR=.. node dist/index.js", "start:dev": "NODE_ENV=dev BASE_DIR=.. nodemon --watch src/ --exec node -r @swc-node/register src/index.ts", + "start:devNoWatch": "NODE_ENV=dev BASE_DIR=.. node -r @swc-node/register src/index.ts", "build": "pnpm clean && pnpm compile", "clean": "rm -rf dist/*", "typescript:compile": "tsc -b", diff --git a/plugin-server/src/config/config.ts b/plugin-server/src/config/config.ts index 9cecab54d8c..f3245ce6223 100644 --- a/plugin-server/src/config/config.ts +++ b/plugin-server/src/config/config.ts @@ -7,6 +7,8 @@ import { KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW, } from './kafka-topics' +export const DEFAULT_HTTP_SERVER_PORT = 6738 + export const defaultConfig = overrideWithEnv(getDefaultConfig()) export function getDefaultConfig(): PluginsServerConfig { @@ -75,6 +77,7 @@ export function getDefaultConfig(): PluginsServerConfig { SENTRY_DSN: null, SENTRY_PLUGIN_SERVER_TRACING_SAMPLE_RATE: 0, SENTRY_PLUGIN_SERVER_PROFILING_SAMPLE_RATE: 0, + HTTP_SERVER_PORT: DEFAULT_HTTP_SERVER_PORT, STATSD_HOST: null, STATSD_PORT: 8125, STATSD_PREFIX: 'plugin-server.', diff --git a/plugin-server/src/main/pluginsServer.ts b/plugin-server/src/main/pluginsServer.ts index 6d3f32638ce..08fc4c6ed0e 100644 --- a/plugin-server/src/main/pluginsServer.ts +++ b/plugin-server/src/main/pluginsServer.ts @@ -447,7 +447,7 @@ export async function startPluginsServer( } if (capabilities.http) { - httpServer = createHttpServer(healthChecks, analyticsEventsIngestionConsumer) + httpServer = createHttpServer(serverConfig.HTTP_SERVER_PORT, healthChecks, analyticsEventsIngestionConsumer) } // If session recordings consumer is defined, then join it. If join diff --git a/plugin-server/src/main/services/http-server.ts b/plugin-server/src/main/services/http-server.ts index 0d84c9815f5..89716d23366 100644 --- a/plugin-server/src/main/services/http-server.ts +++ b/plugin-server/src/main/services/http-server.ts @@ -5,13 +5,12 @@ import * as prometheus from 'prom-client' import { status } from '../../utils/status' -export const HTTP_SERVER_PORT = 6738 - prometheus.collectDefaultMetrics() const v8Profiler = require('v8-profiler-next') v8Profiler.setGenerateType(1) export function createHttpServer( + port: number, healthChecks: { [service: string]: () => Promise | boolean }, analyticsEventsIngestionConsumer?: KafkaJSIngestionConsumer | IngestionConsumer ): Server { @@ -47,7 +46,7 @@ export function createHttpServer( // } // } const checkResults = await Promise.all( - // Note that we do not ues `Promise.allSettled` here so we can + // Note that we do not use `Promise.allSettled` here so we can // assume that all promises have resolved. If there was a // rejected promise, the http server should catch it and return // a 500 status code. @@ -118,8 +117,8 @@ export function createHttpServer( } }) - server.listen(HTTP_SERVER_PORT, () => { - status.info('🩺', `Status server listening on port ${HTTP_SERVER_PORT}`) + server.listen(port, () => { + status.info('🩺', `Status server listening on port ${port}`) }) return server diff --git a/plugin-server/src/types.ts b/plugin-server/src/types.ts index 9a0e0f4ebfe..9cc7fbfa216 100644 --- a/plugin-server/src/types.ts +++ b/plugin-server/src/types.ts @@ -151,6 +151,7 @@ export interface PluginsServerConfig { SENTRY_DSN: string | null SENTRY_PLUGIN_SERVER_TRACING_SAMPLE_RATE: number // Rate of tracing in plugin server (between 0 and 1) SENTRY_PLUGIN_SERVER_PROFILING_SAMPLE_RATE: number // Rate of profiling in plugin server (between 0 and 1) + HTTP_SERVER_PORT: number STATSD_HOST: string | null STATSD_PORT: number STATSD_PREFIX: string diff --git a/plugin-server/tests/http-server.test.ts b/plugin-server/tests/http-server.test.ts index eed0dd1907f..3900168cd20 100644 --- a/plugin-server/tests/http-server.test.ts +++ b/plugin-server/tests/http-server.test.ts @@ -1,7 +1,7 @@ import http from 'http' +import { DEFAULT_HTTP_SERVER_PORT } from '../src/config/config' import { startPluginsServer } from '../src/main/pluginsServer' -import { HTTP_SERVER_PORT } from '../src/main/services/http-server' import { makePiscina } from '../src/worker/piscina' import { resetTestDatabase } from './helpers/sql' @@ -40,7 +40,7 @@ describe('http server', () => { ) await new Promise((resolve) => - http.get(`http://localhost:${HTTP_SERVER_PORT}/_health`, (res) => { + http.get(`http://localhost:${DEFAULT_HTTP_SERVER_PORT}/_health`, (res) => { const { statusCode } = res expect(statusCode).toEqual(200) resolve(null) @@ -68,7 +68,7 @@ describe('http server', () => { ) await new Promise((resolve) => - http.get(`http://localhost:${HTTP_SERVER_PORT}/_ready`, (res) => { + http.get(`http://localhost:${DEFAULT_HTTP_SERVER_PORT}/_ready`, (res) => { const { statusCode } = res expect(statusCode).toEqual(200) resolve(null)