Skip to content

Commit

Permalink
Kerem/metrics discovery dockerfile pt2 (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
mechanical-turk authored Jul 17, 2024
1 parent 4b03ad4 commit 32d512b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/metrics-discovery/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ COPY ./packages/proto /river/packages/proto
COPY ./packages/metrics-discovery /river/packages/metrics-discovery

# install dependencies and build
RUN apk add --no-cache git
RUN apk add --no-cache git curl
RUN corepack enable
RUN yarn install
RUN yarn build:metrics-discovery

WORKDIR /river/packages/metrics-discovery

HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl -f http://localhost:8080 || exit 1

CMD [ "yarn", "start"]
14 changes: 6 additions & 8 deletions packages/metrics-discovery/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
volumes:
prometheus_data: {}

services:
river-metrics-discovery:
container_name: river-metrics-discovery
Expand All @@ -11,12 +8,14 @@ services:
- ./prometheus/etc/:/river/packages/metrics-discovery/prometheus/etc:rw
env_file:
- ./.env
ports:
- '8080:8080'

prometheus:
depends_on:
- river-metrics-discovery
river-metrics-discovery:
condition: service_healthy
container_name: prometheus
restart: always
image: prom/prometheus:latest
user: root
labels:
Expand All @@ -26,7 +25,6 @@ services:
# collect_counters_with_distributions
volumes:
- ./prometheus/etc:/prometheus/etc
- prometheus_data:/prometheus
command:
- --config.file=/prometheus/etc/prometheus.yml
- --log.level=debug
Expand All @@ -35,8 +33,8 @@ services:

datadog:
depends_on:
- prometheus
- river-metrics-discovery
river-metrics-discovery:
condition: service_healthy
container_name: datadog
image: datadog/agent:7
ports:
Expand Down
20 changes: 20 additions & 0 deletions packages/metrics-discovery/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { MetricsDiscovery } from './metrics-discovery'
import { envVarsSchema } from './env-vars'
import { sleep } from './utils'
import { createPrometheusConfig } from './create-prometheus-config'
import http from 'node:http'

const PROMETHEUS_TARGETS_FILE = './prometheus/etc/targets.json'
const SLEEP_DURATION_MS = 1000 * 60 * 5 // 5 minutes
const PORT = 8080

let numWrites = 0

const run = async () => {
console.info('Creating prometheus config...')
Expand All @@ -18,13 +22,29 @@ const run = async () => {
env: envVars.ENV,
})

const server = http.createServer((req, res) => {
if (numWrites === 0) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end('No prometheus targets written yet\n')
return
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Healthy\n')
}
})

server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`)
})

for (;;) {
console.info('Getting prometheus targets...')
const targets = await metricsDiscovery.getPrometheusTargets()
console.info('Writing prometheus targets...', targets)
await fs.promises.writeFile(PROMETHEUS_TARGETS_FILE, targets, {
encoding: 'utf8',
})
numWrites++
console.info(`Prometheus targets written to: ${PROMETHEUS_TARGETS_FILE}`)
console.info(`Sleeping for ${SLEEP_DURATION_MS} ms...`)
await sleep(SLEEP_DURATION_MS)
Expand Down

0 comments on commit 32d512b

Please sign in to comment.