diff --git a/bulletin-board-app/package.json b/bulletin-board-app/package.json index ceea631..b4fc18b 100644 --- a/bulletin-board-app/package.json +++ b/bulletin-board-app/package.json @@ -13,7 +13,8 @@ "vue": "^1.0.10", "vue-resource": "^0.1.17", "tedious": "^2.0.1", - "sequelize": "^4.20.1" + "sequelize": "^4.20.1", + "prom-client": "^10.2.2" }, "devDependencies": { "body-parser": "^1.14.1", diff --git a/bulletin-board-app/server.js b/bulletin-board-app/server.js index dbdcb2d..d9877f1 100644 --- a/bulletin-board-app/server.js +++ b/bulletin-board-app/server.js @@ -3,6 +3,7 @@ var express = require('express'), methodOverride = require('method-override'), errorHandler = require('errorhandler'), morgan = require('morgan'), + prometheus = require('prom-client'), routes = require('./backend'), api = require('./backend/api'); @@ -29,10 +30,42 @@ if ('production' == app.get('env')) { app.use(errorHandler()); } +// counter for Prometheus: +const httpRequestDurationMicroseconds = new prometheus.Histogram({ + name: 'http_request_duration_ms', + help: 'Duration of HTTP requests in ms', + labelNames: ['method', 'route', 'code'], + buckets: [0.10, 5, 15, 50, 100, 200, 300, 400, 500] // buckets for response time from 0.1ms to 500ms +}) + +// record timestamp before request handler: +app.use((req, res, next) => { + res.locals.startEpoch = Date.now() + next() +}) + app.get('/', routes.index); app.get('/api/events', api.events); app.post('/api/events', api.event); -app.delete('/api/events/:eventId', api.event) +app.delete('/api/events/:eventId', api.event); + +app.get('/metrics', (req, res) => { + res.set('Content-Type', prometheus.register.contentType) + res.end(prometheus.register.metrics()) +}) + +// set response duration after handler: +app.use((req, res, next) => { + const responseTimeInMs = Date.now() - res.locals.startEpoch + + httpRequestDurationMicroseconds + .labels(req.method, req.path, res.statusCode) + .observe(responseTimeInMs) + + next() +}) + +prometheus.collectDefaultMetrics(); app.listen(8080); console.log('Magic happens on port 8080...'); \ No newline at end of file diff --git a/bulletin-board-dashboard/dashboard.json b/bulletin-board-dashboard/dashboard.json new file mode 100644 index 0000000..465fe2a --- /dev/null +++ b/bulletin-board-dashboard/dashboard.json @@ -0,0 +1,613 @@ +{ + "__inputs": [ + { + "name": "DS_PROMETHEUS", + "label": "prometheus", + "description": "", + "type": "datasource", + "pluginId": "prometheus", + "pluginName": "Prometheus" + } + ], + "__requires": [ + { + "type": "grafana", + "id": "grafana", + "name": "Grafana", + "version": "4.6.2" + }, + { + "type": "panel", + "id": "graph", + "name": "Graph", + "version": "" + }, + { + "type": "datasource", + "id": "prometheus", + "name": "Prometheus", + "version": "1.0.0" + }, + { + "type": "panel", + "id": "singlestat", + "name": "Singlestat", + "version": "" + } + ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": "-- Grafana --", + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + }, + { + "datasource": "${DS_PROMETHEUS}", + "enable": false, + "expr": "ALERTS", + "hide": false, + "iconColor": "rgba(255, 96, 96, 1)", + "limit": 100, + "name": "Alerts", + "showIn": 0, + "step": "10s", + "type": "alert" + } + ] + }, + "description": "Bulletin Board", + "editable": true, + "gnetId": null, + "graphTooltip": 0, + "hideControls": false, + "id": null, + "links": [], + "refresh": "5s", + "rows": [ + { + "collapse": false, + "height": 250, + "panels": [ + { + "cacheTimeout": null, + "colorBackground": false, + "colorValue": true, + "colors": [ + "rgba(50, 172, 45, 0.97)", + "rgba(237, 129, 40, 0.89)", + "rgba(245, 54, 54, 0.9)" + ], + "datasource": "${DS_PROMETHEUS}", + "decimals": null, + "format": "none", + "gauge": { + "maxValue": 1, + "minValue": 0, + "show": true, + "thresholdLabels": true, + "thresholdMarkers": true + }, + "hideTimeOverride": false, + "id": 6, + "interval": null, + "links": [], + "mappingType": 1, + "mappingTypes": [ + { + "name": "value to text", + "value": 1 + }, + { + "name": "range to text", + "value": 2 + } + ], + "maxDataPoints": 100, + "nullPointMode": "connected", + "nullText": null, + "postfix": "", + "postfixFontSize": "50%", + "prefix": "", + "prefixFontSize": "50%", + "rangeMaps": [ + { + "from": "null", + "text": "N/A", + "to": "null" + } + ], + "span": 3, + "sparkline": { + "fillColor": "rgba(31, 118, 189, 0.18)", + "full": false, + "lineColor": "rgb(31, 120, 193)", + "show": false + }, + "tableColumn": "Value", + "targets": [ + { + "expr": "sum(increase(http_request_duration_ms_count{code=~\"^5..$\"}[1m])) / sum(increase(http_request_duration_ms_count[1m]))", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "", + "refId": "A", + "step": 20 + } + ], + "thresholds": "0.1", + "title": "Error rate", + "type": "singlestat", + "valueFontSize": "80%", + "valueMaps": [ + { + "op": "=", + "text": "N/A", + "value": "null" + } + ], + "valueName": "avg" + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_PROMETHEUS}", + "fill": 1, + "id": 1, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 1, + "links": [ + { + "type": "dashboard" + } + ], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 9, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "sum(rate(http_request_duration_ms_count[1m])) by (service, route, method, code) * 60", + "format": "time_series", + "hide": false, + "intervalFactor": 2, + "legendFormat": "{{service}} - {{method}} {{route}} {{code}}", + "metric": "", + "refId": "A", + "step": 2 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Throughput", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "rpm", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": true, + "title": "Throughput", + "titleSize": "h6" + }, + { + "collapse": false, + "height": 250, + "panels": [ + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_PROMETHEUS}", + "fill": 1, + "id": 4, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "histogram_quantile(0.5, sum(rate(http_request_duration_ms_bucket[1m])) by (le, service, route, method))", + "format": "time_series", + "intervalFactor": 2, + "legendFormat": "{{service}} - {{method}} {{route}}", + "refId": "A", + "step": 2 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Median Response Time", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "ms", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_PROMETHEUS}", + "fill": 1, + "id": 2, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "histogram_quantile(0.95, sum(rate(http_request_duration_ms_bucket[1m])) by (le, service, route, method))", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "{{service}} - {{method}} {{route}}", + "refId": "A", + "step": 2 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "95th Response Time", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transparent": false, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "ms", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": true, + "title": "Response time", + "titleSize": "h6" + }, + { + "collapse": false, + "height": 305, + "panels": [ + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_PROMETHEUS}", + "fill": 1, + "id": 3, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "avg(nodejs_external_memory_bytes / 1024) by (service)", + "format": "time_series", + "intervalFactor": 2, + "legendFormat": "{{service}}", + "refId": "A", + "step": 2 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Memory usage", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "decmbytes", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_PROMETHEUS}", + "fill": 1, + "id": 7, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "avg(process_cpu_seconds_total) by (service)", + "format": "time_series", + "intervalFactor": 2, + "refId": "A" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "CPU usage", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": true, + "title": "Compute stats", + "titleSize": "h6" + } + ], + "schemaVersion": 14, + "style": "dark", + "tags": [], + "templating": { + "list": [] + }, + "time": { + "from": "now-5m", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "timezone": "browser", + "title": "Bulletin Board", + "version": 3 +} \ No newline at end of file diff --git a/bulletin-board-metrics/Dockerfile b/bulletin-board-metrics/Dockerfile new file mode 100644 index 0000000..7618f95 --- /dev/null +++ b/bulletin-board-metrics/Dockerfile @@ -0,0 +1,3 @@ +FROM prom/prometheus:v2.0.0 + +COPY prometheus.yml /etc/prometheus/prometheus.yml \ No newline at end of file diff --git a/bulletin-board-metrics/prometheus.yml b/bulletin-board-metrics/prometheus.yml new file mode 100644 index 0000000..4f312bc --- /dev/null +++ b/bulletin-board-metrics/prometheus.yml @@ -0,0 +1,7 @@ +global: + scrape_interval: 5s + +scrape_configs: + - job_name: 'bb-app' + static_configs: + - targets: ['bb-app:8080'] \ No newline at end of file diff --git a/bulletin-board-proxy/nginx.conf b/bulletin-board-proxy/nginx.conf index 76316c6..af60359 100644 --- a/bulletin-board-proxy/nginx.conf +++ b/bulletin-board-proxy/nginx.conf @@ -24,6 +24,10 @@ http { map $sent_http_content_type $expires { default off; + text/css 1M; + text/javascript 1M; + application/javascript 1M; + application/x-font-woff £M; ~image/ 6M; } @@ -36,7 +40,7 @@ http { location / { proxy_pass http://bb-app:8080/; proxy_cache STATIC; - proxy_cache_valid 200 1h; + proxy_cache_valid 200 5s; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } diff --git a/docker-compose.yml b/docker-compose.yml index a8536e2..6964b84 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,27 +1,49 @@ version: '3.3' services: - - bb-app: - image: sixeyed/bulletin-board-app - build: - context: ./bulletin-board-app - networks: - - bb-net bb-db: - image: sixeyed/bulletin-board-db + image: {dockerId}/bulletin-board-db build: context: ./bulletin-board-db networks: - bb-net + bb-app: + image: {dockerId}/bulletin-board-app + build: + context: ./bulletin-board-app + depends_on: + - bb-db + networks: + - bb-net + bb-proxy: - image: sixeyed/bulletin-board-proxy + image: {dockerId}/bulletin-board-proxy build: context: ./bulletin-board-proxy ports: - "80:80" + depends_on: + - bb-app + networks: + - bb-net + + bb-metrics: + image: {dockerId}/bulletin-board-metrics + build: + context: ./bulletin-board-metrics + depends_on: + - bb-app + networks: + - bb-net + + bb-dashboard: + image: grafana/grafana + ports: + - "3000:3000" + depends_on: + - bb-metrics networks: - bb-net