Skip to content

Commit

Permalink
Setup grafana and prometheus #130
Browse files Browse the repository at this point in the history
Currently the scrape path is still hard coded.
This commit just flushes the local repository copy.

* Dashboards and datasources are provisioned via files.
* Example metric for snapshot of running games is given.
* User arrival doesn't happen on path '^/metrics/'.
  • Loading branch information
marein committed May 17, 2022
1 parent eae97b3 commit 3634002
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Following urls are accessible after the project is successfully started.
| [http://localhost:8082/](http://localhost:8082/) | Redis management interface. |
| [http://localhost:8083/](http://localhost:8083/) | RabbitMq management interface. |
| [http://localhost:8084/](http://localhost:8084/) | Traefik management interface. |
| [http://localhost:8085/](http://localhost:8085/) | Grafana management interface. |

### Production

Expand Down
9 changes: 1 addition & 8 deletions config/connect-four/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
imports:
- { resource: services/command_bus.yml }
- { resource: services/console.yml }
- { resource: services/consumer.yml }
- { resource: services/game.yml }
- { resource: services/normalizer.yml }
- { resource: services/persistence.yml }
- { resource: services/query_bus.yml }
- { resource: services/subscriber.yml }
- { resource: services/ }

doctrine:
dbal:
Expand Down
6 changes: 6 additions & 0 deletions config/connect-four/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
connect-four_metrics:
path: /metrics/connect-four
methods:
- GET
defaults:
_controller: connect-four.metrics-controller::metricsAction
8 changes: 8 additions & 0 deletions config/connect-four/services/metrics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:

connect-four.metrics-controller:
class: Gaming\ConnectFour\Port\Adapter\Http\MetricsController
arguments:
- '@connect-four.query-bus'
tags:
- 'controller.service_arguments'
4 changes: 2 additions & 2 deletions config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
WebInterface:
resource: web-interface/routing.yml
Gaming:
resource: '*/routing.yml'
1 change: 1 addition & 0 deletions config/web-interface/services/event_listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ services:
public: false
arguments:
- '@web-interface.identity-service'
- '/^\/metrics\//'
tags:
- { name: kernel.event_listener, event: kernel.request }
18 changes: 18 additions & 0 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ services:
- "traefik.enable=true"
- "traefik.http.routers.nchan.priority=20"
- "traefik.http.routers.nchan.rule=PathPrefix(`/sse`)"
grafana:
image: marein/php-gaming-website:grafana
restart: on-failure
environment:
GF_AUTH_DISABLE_LOGIN_FORM: 1
GF_AUTH_ANONYMOUS_ENABLED: 1
GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
ports:
- "8085:3000"
prometheus:
image: marein/php-gaming-website:prometheus
restart: on-failure
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
volumes:
- prometheus:/prometheus
service-js-polyfill:
image: gamingplatform/service-js-polyfill
restart: on-failure
Expand Down Expand Up @@ -103,3 +120,4 @@ volumes:
mysql:
redis:
rabbit-mq:
prometheus:
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ services:
- nginx
volumes:
- /var/run/docker.sock:/var/run/docker.sock
grafana:
build:
context: .
dockerfile: ./docker/grafana/Dockerfile
environment:
GF_AUTH_DISABLE_LOGIN_FORM: 1
GF_AUTH_ANONYMOUS_ENABLED: 1
GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
ports:
- "8085:3000"
volumes:
- ./docker/grafana/provisioning:/etc/grafana/provisioning
- ./docker/grafana/dashboards:/var/lib/grafana/dashboards
prometheus:
build:
context: .
dockerfile: ./docker/prometheus/Dockerfile
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
volumes:
- prometheus:/prometheus
restart: on-failure
mysql:
build:
context: .
Expand Down Expand Up @@ -174,3 +197,4 @@ volumes:
mysql:
redis:
rabbit-mq:
prometheus:
4 changes: 4 additions & 0 deletions docker/grafana/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM grafana/grafana-oss:8.5.2

COPY /docker/grafana/provisioning /etc/grafana/provisioning
COPY /docker/grafana/dashboards /var/lib/grafana/dashboards
136 changes: 136 additions & 0 deletions docker/grafana/dashboards/connect-four/application-metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 3,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 0
},
"id": 2,
"interval": "5s",
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"expr": "running_games{domain=\"connect-four\"}",
"refId": "A"
}
],
"title": "Number of running games",
"type": "timeseries"
}
],
"refresh": "5s",
"schemaVersion": 1,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-15m",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "Application metrics",
"uid": "BPTAtVX7z",
"version": 1,
"weekStart": ""
}
11 changes: 11 additions & 0 deletions docker/grafana/provisioning/dashboards/file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

providers:
- name: File
type: file
disableDeletion: true
updateIntervalSeconds: 5
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: true
11 changes: 11 additions & 0 deletions docker/grafana/provisioning/datasources/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

datasources:
- name: Prometheus
type: prometheus
isDefault: true
access: proxy
uid: prometheus
url: http://prometheus:9090
version: 1
editable: true
3 changes: 3 additions & 0 deletions docker/prometheus/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM prom/prometheus:v2.35.0

COPY /docker/prometheus/prometheus.yml /etc/prometheus/prometheus.yml
13 changes: 13 additions & 0 deletions docker/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
global:
scrape_interval: 1m

scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'connect-four'
scrape_interval: 5s
metrics_path: '/metrics/connect-four'
static_configs:
- targets: ['traefik']
4 changes: 4 additions & 0 deletions project
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ buildProductionImages() {
docker build --file docker/mysql/Dockerfile --tag marein/php-gaming-website:mysql .
docker build --file docker/redis/Dockerfile --tag marein/php-gaming-website:redis .
docker build --file docker/rabbit-mq/Dockerfile --tag marein/php-gaming-website:rabbit-mq .
docker build --file docker/grafana/Dockerfile --tag marein/php-gaming-website:grafana .
docker build --file docker/prometheus/Dockerfile --tag marein/php-gaming-website:prometheus .
}

pushProductionImages() {
Expand All @@ -111,6 +113,8 @@ pushProductionImages() {
docker push marein/php-gaming-website:mysql
docker push marein/php-gaming-website:redis
docker push marein/php-gaming-website:rabbit-mq
docker push marein/php-gaming-website:grafana
docker push marein/php-gaming-website:prometheus
}

if [ "$*" = "" ]
Expand Down
38 changes: 38 additions & 0 deletions src/ConnectFour/Port/Adapter/Http/MetricsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Gaming\ConnectFour\Port\Adapter\Http;

use Gaming\Common\Bus\Bus;
use Gaming\ConnectFour\Application\Game\Query\Model\RunningGames\RunningGames;
use Gaming\ConnectFour\Application\Game\Query\RunningGamesQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class MetricsController
{
public function __construct(
private readonly Bus $queryBus
) {
}

public function metricsAction(Request $request): Response
{
$runningGames = $this->queryBus->handle(new RunningGamesQuery());
assert($runningGames instanceof RunningGames);

$metrics = sprintf(
'# HELP running_games Number of running games.' . PHP_EOL .
'# TYPE running_games gauge' . PHP_EOL .
'running_games{domain="connect-four"} %s ' . PHP_EOL,
$runningGames->count()
);

return new Response(
$metrics,
200,
['content-type' => 'text/plain']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@

final class AssignUserIdOnKernelRequest
{
private IdentityService $identityService;

public function __construct(IdentityService $identityService)
{
$this->identityService = $identityService;
public function __construct(
private readonly IdentityService $identityService,
private readonly string $ignoredPathExpression
) {
}

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if ($this->shouldIgnoreRequest($event)) {
return;
}

$request = $event->getRequest();
$session = $request->getSession();
$session = $event->getRequest()->getSession();

if (!$session->has('user')) {
// todo Create user only when it's really needed.
Expand All @@ -33,4 +31,10 @@ public function onKernelRequest(RequestEvent $event): void
$session->set('user', $this->identityService->arrive()['userId']);
}
}

private function shouldIgnoreRequest(RequestEvent $event): bool
{
return !$event->isMainRequest()
|| preg_match($this->ignoredPathExpression, $event->getRequest()->getRequestUri()) === 1;
}
}

0 comments on commit 3634002

Please sign in to comment.