Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBZ-8174 Add example for db activity monitoring #359

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 70 additions & 0 deletions db-activity-monitoring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Detect data mutation patterns with Debezium

In today's dynamic data environments, detecting and understanding data mutation patterns is critical for system reliability.
In this example, we will explore how to use Debezium for comprehensive database activity logging and analysis in microservice architectures.
By integrating with analytics tools, teams can build detailed activity dashboards that reveal the volume and nature of operations per table.
These insights are invaluable for identifying unexpected patterns, such as a sudden drop in inserts caused by a new microservice deployment with a bug.
In this example we'll see how to set up Debezium to expose advanced metrics for this specific use cases, and we'll utilize these metrics to create actionable dashboards.

## Monitoring a Debezium instance

Debezium [collects and exports](https://debezium.io/documentation/reference/1.5/operations/monitoring.html) a set of metrics as JMX beans.
Those metrics can be displayed either via an arbitrary JMX console or, for more complex deployments, a Prometheus and Grafana based solution can be deployed.
This example uses a Docker Compose file to set up and deploy Debezium together with all components necessary to monitor it in Grafana.

## Topology

We need following components to collect and present Debezium metrics:

* Debezium instance with [JMX Exporter](https://github.com/prometheus/jmx_exporter) Java agent installed and configured (see [Docker image](debezium-jmx-exporter))
* Prometheus instance to collect and store exported metrics (see [Docker image](debezium-prometheus))
* Grafana instance presenting the metrics (see [Docker image](debezium-grafana))

## Execution

Before starting running all required services we need to build our order service.

```shell
order-service/mvnw package -f order-service/pom.xml
```

then we can just run our compose file to start everything is needed.

```shell
export DEBEZIUM_VERSION=3.0.0.CR1
mfvitale marked this conversation as resolved.
Show resolved Hide resolved
docker-compose up -d --build
```

When all service are up and running we can register our connector

```shell
# Start PostgreSQL connector
curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" http://localhost:8083/connectors/ -d @postgres-activity-monitoring.json
```

## Accessing the dashboard

Open a web browser and go to the Grafana UI at [http://localhost:3000](http://localhost:3000).
Login into the console as user `admin` with password `admin`.
When asked either change the password (you also can skip this step).

Then you can monitor the order service activity using the pre-built dashboard `General/ Microservices activity monitoring`.
This dashboard will show the rate of created orders within a label that indicate the version of the deployed order service.

The order service will insert 100 orders every 10s so this means that you should see a rate of ~10 order per second.

To simulate a drop, we can just update the `APP_VERSION` env to a value different to `1.0`.

```shell
docker stop order-service
docker rm -f order-service && \
docker compose run -d -e APP_VERSION=1.5 --name order-service order-service
```

In that case the service will start creating orders with a ~50% drop.

An alert has been configured to fire when the order rate is below 7.
You can check that a mail will be sent to a Fake SMTP server, to check it you can
open a web browser and got to the Fake SMTP UI at [http://localhost:8085](http://localhost:8085).


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions db-activity-monitoring/debezium-grafana/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ARG GRAFANA_VERSION
FROM grafana/grafana:${GRAFANA_VERSION}

COPY dashboard.yml /etc/grafana/provisioning/dashboards
COPY datasource.yml /etc/grafana/provisioning/datasources
COPY debezium-dashboard.json /var/lib/grafana/dashboards/debezium-dashboard.json
COPY activity-monitoring-dashboard.json /var/lib/grafana/dashboards/activity-monitoring-dashboard.json
COPY alerting.yaml /etc/grafana/provisioning/alerting/alerting.yaml
COPY grafana.ini /etc/grafana/grafana.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"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"
},
{
"datasource": {
"type": "prometheus",
"uid": "P1809F7CD0C75ACF3"
},
"enable": true,
"expr": "application_info",
"iconColor": "red",
"name": "Order service deployments",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"textFormat": "with version {{version}}",
"titleFormat": "{{name}} deployed"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "prometheus",
"uid": "P1809F7CD0C75ACF3"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"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": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 1,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "P1809F7CD0C75ACF3"
},
"editorMode": "code",
"exemplar": true,
"expr": "rate(debezium_metrics_create_events_count{table=\"inventory.orders\"}[5m])",
"instant": false,
"key": "Q-9c46ab77-5c46-4e72-8a50-40be0a9275ea-0",
"legendFormat": "__auto",
"range": true,
"refId": "A"
}
],
"title": "Order service",
"type": "timeseries"
}
],
"refresh": "5s",
"revision": 1,
"schemaVersion": 38,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-15m",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "Microservices activity monitoring",
"uid": "MkLBe3QSk",
"version": 1,
"weekStart": ""
}
88 changes: 88 additions & 0 deletions db-activity-monitoring/debezium-grafana/alerting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
apiVersion: 1
groups:
- orgId: 1
name: microservices
folder: Infrastructure
interval: 1m
rules:
- uid: BZuGZewIk
title: Order rate drop
condition: C
data:
- refId: A
relativeTimeRange:
from: 600
to: 0
datasourceUid: P1809F7CD0C75ACF3
model:
datasource:
type: prometheus
uid: P1809F7CD0C75ACF3
editorMode: code
expr: rate(debezium_metrics_create_events_count{table="inventory.orders"}[1m])
hide: false
intervalMs: 1000
legendFormat: __auto
maxDataPoints: 43200
range: true
refId: A
- refId: B
relativeTimeRange:
from: 600
to: 0
datasourceUid: __expr__
model:
conditions:
- evaluator:
params:
- 0
- 0
type: gt
operator:
type: and
query:
params: []
reducer:
params: []
type: avg
type: query
datasource:
name: Expression
type: __expr__
uid: __expr__
expression: A
intervalMs: 1000
maxDataPoints: 43200
reducer: last
refId: B
type: reduce
- refId: C
datasourceUid: __expr__
model:
conditions:
- evaluator:
params:
- 7
- 0
type: lt
operator:
type: and
query:
params: []
reducer:
params: []
type: avg
type: query
datasource:
name: Expression
type: __expr__
uid: __expr__
expression: B
intervalMs: 1000
maxDataPoints: 43200
refId: C
type: threshold
noDataState: NoData
execErrState: Error
for: 1m
isPaused: false
10 changes: 10 additions & 0 deletions db-activity-monitoring/debezium-grafana/dashboard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: 1
providers:
- name: 'default'
orgId: 1
folder: ''
type: file
disableDeletion: false
updateIntervalSeconds: 10 #how often Grafana will scan for changed dashboards
options:
path: /var/lib/grafana/dashboards
8 changes: 8 additions & 0 deletions db-activity-monitoring/debezium-grafana/datasource.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: 1

datasources:
- name: prometheus
type: prometheus
url: http://prometheus:9090
access: proxy
version: 1
Loading