Skip to content

Commit

Permalink
fix(preview-service): listen externally on metrics port
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsproat committed Sep 5, 2024
1 parent cf9348e commit 032d19b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/preview-service-acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
PREVIEW_SERVICE_IMAGE: speckle/preview-service:local
OUTPUT_FILE_PATH: /tmp/preview-service-output.png
NODE_ENV: test
PG_CONNECTION_STRING: postgres://preview_service_test:preview_service_test@localhost:5432/preview_service_test
PG_CONNECTION_STRING: postgres://preview_service_test:preview_service_test@postgres:5432/preview_service_test

- uses: actions/upload-artifact@v4
name: Upload the output from the preview-service
Expand Down
3 changes: 2 additions & 1 deletion docker-compose-speckle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ services:
mem_limit: '3000m'
memswap_limit: '3000m'
environment:
HOST: '127.0.0.1'
HOST: '127.0.0.1' # Only accept connections from localhost, as preview service does not need to be exposed outside the container.
METRICS_HOST: '127.0.0.1' # Amend if you want to expose Prometheus metrics outside of the container
LOG_LEVEL: 'info'
PG_CONNECTION_STRING: 'postgres://speckle:speckle@postgres/speckle'

Expand Down
6 changes: 4 additions & 2 deletions packages/preview-service/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { serverLogger } from '@/observability/logging.js'
import { appFactory as metricsAppFactory } from '@/observability/metricsApp.js'
import { appFactory } from '@/server/app.js'
import { getAppPort, getHost, getMetricsPort } from '@/utils/env.js'
import { getAppPort, getHost, getMetricsHost, getMetricsPort } from '@/utils/env.js'
import http from 'http'
import type { Knex } from 'knex'
import { isNaN, isString, toNumber } from 'lodash-es'
Expand Down Expand Up @@ -39,12 +39,14 @@ export const startServer = (params: { db: Knex; serveOnRandomPort?: boolean }) =
onListening(server)
})
server.listen(inputPort, host)

const metricsHost = getMetricsHost()
metricsServer.on('error', onErrorFactory(inputPort))
metricsServer.on('listening', () => {
serverLogger.info('📊 Started Preview Service metrics server')
onListening(metricsServer)
})
metricsServer.listen(inputMetricsPort, host)
metricsServer.listen(inputMetricsPort, metricsHost)

return { app, server, metricsServer }
}
Expand Down
1 change: 1 addition & 0 deletions packages/preview-service/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getChromiumExecutablePath = () => {
export const getHealthCheckFilePath = () =>
process.env.HEALTHCHECK_FILE_PATH || '/tmp/last_successful_query'
export const getHost = () => process.env.HOST || '127.0.0.1'
export const getMetricsHost = () => process.env.METRICS_HOST || '127.0.0.1'
export const getLogLevel = () => process.env.LOG_LEVEL || 'info'
export const getMetricsPort = () => process.env.PROMETHEUS_METRICS_PORT || '9094'
export const getNodeEnv = () => process.env.NODE_ENV || 'production'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ spec:
{{- end }}

env:
- name: HOST
value: '127.0.0.1' # Only accept connections from localhost, as preview service does not need to be exposed outside the container.
- name: METRICS_HOST
value: '0.0.0.0' # Expose metrics outside of the container so it can be scraped by Prometheus
- name: PORT
value: {{ .Values.preview_service.port | quote }}

Expand Down

0 comments on commit 032d19b

Please sign in to comment.