Skip to content
Open
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
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"express": "^4.18.2",
"cors": "^2.8.5",
"body-parser": "^1.20.2",
"pg": "^8.11.0"
"pg": "^8.11.0",
"prom-client": "^15.1.0"
},
"devDependencies": {
"nodemon": "^3.0.1"
Expand Down
37 changes: 37 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,39 @@ const cors = require('cors')
const bodyParser = require('body-parser')
const fs = require('fs')
const path = require('path')
const promClient = require('prom-client')

const app = express()
app.use(cors())
app.use(bodyParser.json())

const collectDefaultMetrics = promClient.collectDefaultMetrics
collectDefaultMetrics()

const httpRequestsTotal = new promClient.Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'path', 'status']
})

const httpRequestDuration = new promClient.Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'path', 'status'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 2, 5]
})

app.use((req, res, next) => {
const start = Date.now()
res.on('finish', () => {
const duration = (Date.now() - start) / 1000
const path = req.route ? req.route.path : req.path
httpRequestsTotal.inc({ method: req.method, path, status: res.statusCode })
httpRequestDuration.observe({ method: req.method, path, status: res.statusCode }, duration)
})
next()
})

const PORT = process.env.PORT || 3000
const DATA_PATH = path.join(__dirname, '..', 'data', 'tasks.json')

Expand Down Expand Up @@ -62,4 +90,13 @@ app.delete('/tasks/:id', (req, res) => {

app.get('/health', (req, res) => res.json({ status: 'ok' }))

app.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', promClient.register.contentType)
res.end(await promClient.register.metrics())
} catch (err) {
res.status(500).end(err)
}
})

app.listen(PORT, () => console.log(`TaskFlow backend listening on ${PORT}`))
38 changes: 36 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
db:
image: postgres:17
Expand Down Expand Up @@ -61,9 +59,45 @@ services:
networks:
- taskflow-network

prometheus:
image: prom/prometheus:v2.48.0
container_name: taskflow-prometheus
ports:
- "9090:9090"
volumes:
- ./infrastructure/monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prometheus-data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.enable-lifecycle'
networks:
- taskflow-network

grafana:
image: grafana/grafana:10.2.0
container_name: taskflow-grafana
ports:
- "3001:3000"
environment:
- GF_SECURITY_ADMIN_USER=${GRAFANA_ADMIN_USER:-admin}
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin}
- GF_USERS_ALLOW_SIGN_UP=false
volumes:
- ./infrastructure/monitoring/grafana/provisioning:/etc/grafana/provisioning:ro
- grafana-data:/var/lib/grafana
depends_on:
- prometheus
networks:
- taskflow-network

volumes:
db-data:
driver: local
prometheus-data:
driver: local
grafana-data:
driver: local

networks:
taskflow-network:
Expand Down
Loading
Loading