SlideShare a Scribd company logo
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 1 di 47
Come usare Postgresql per gestire lo storage 
delle metriche di Prometheus
Lucio Grenzi
l.grenzi@gmail.com
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 2 di 47
Delphi developer since 1999
IT Consultant
Full stack web developer
Postgresql addicted
      lucio grenzi
dogwolf
Who I am
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 3 di 47
AgendaAgenda
 Intro to Prometheus
Intro to TimeScaleDB
Pg_prometheus
Prometheus-postgresql-adapter
Prometheus HA
Conclusion and final thoughts
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 4 di 47
Why monitoring activities?Why monitoring activities?
Detect errors and bottlenecks
Auditing
Measure performances
Trending to see changes during time
Drive business/technical processes
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 5 di 47
Prometheus
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 6 di 47
PrometheusPrometheus
Open source (Apache 2 license)
Only metrics, not logging
Pull based approach
Multidimensional data model
Built-in time series database
Flexibile query langage (Qml)
Alertmanager handle notifications
For all levels of the stack 
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 7 di 47
Prometheus historyPrometheus history
Inspired by the monitoring tool Borgmon used at
Google
Developed at SoundCloud starting in 2012
In May 2016, the Cloud Native Computing
Foundation accepted Prometheus as its second
incubated project
Inspired by the monitoring tool Borgmon used at
Google
Developed at SoundCloud starting in 2012
In May 2016, the Cloud Native Computing
Foundation accepted Prometheus as its second
incubated project
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 8 di 47
Prometheus CommunityPrometheus Community
Open source project
IRC: #prometheus
User mailing lists:
Prometheus-announce
Prometheus-users
Prometheus-announce.
Third-party commercial support
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 9 di 47
Prometheus architecturePrometheus architecture
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 10 di 47
Integration withIntegration with
Many existing integrations: Java, JMX, Python, Go,
Ruby, .Net, Machine, Cloudwatch, EC2, MySQL,
PostgreSQL, Haskell, Bash, Node.js, SNMP,
Consul, HAProxy, Mesos, Bind, CouchDB, Django,
Mtail, Heka, Memcached, RabbitMQ, Redis,
RethinkDB, Rsyslog, Meteor.js, Minecraft...
Graphite, Statsd, Collectd, Scollector, Munin,
Nagios integrations aid transition.
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 11 di 47
InstallationInstallation
Pre-compiled binaries
Compile from source: makefile
Docker
Ansible
Chef
Puppet
Saltstack
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 12 di 47
ConfigurationConfiguration
 Written in Yaml format
Sections:
global
rule_files
scrape_configs
alerting
remote_write
remote_read
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 13 di 47
# my global config
global:
scrape_interval: 15s
evaluation_interval: 30s
# scrape_timeout is set to the global default
(10s).
external_labels:
monitor: codelab
foo: bar
rule_files:
- "first.rules"
- "my/*.rules"
remote_write:
- url: http://remote1/push
write_relabel_configs:
- source_labels: [__name__]
regex: expensive.*
action: drop
- url: http://remote2/push
tls_config:
cert_file: valid_cert_file
key_file: valid_key_file
remote_read:
- url: http://remote1/read
read_recent: true
- url: http://remote3/read
read_recent: false
required_matchers:
job: special
tls_config:
cert_file: valid_cert_file
key_file: valid_key_file
scrape_configs:
- job_name: prometheus
honor_labels: true
# scrape_interval is defined by the configured
global (15s).
# scrape_timeout is defined by the global
default (10s).
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
file_sd_configs:
- files:
- foo/*.slow.json
- foo/*.slow.yml
- single/file.yml
refresh_interval: 10m
- files:
- bar/*.yaml
static_configs:
- targets: ['localhost:9090', 'localhost:9191']
labels:
my: label
your: label
relabel_configs:
- source_labels: [job, __meta_dns_name]
regex: (.*)some-[regex]
target_label: job
replacement: foo-${1}
# action defaults to 'replace'
- source_labels: [abc]
target_label: cde
- replacement: static
target_label: abc
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 14 di 47
Prometheus storagePrometheus storage
Local on-disk time series database
Optionally integrates with remote storage systems
Not depending on distributed storage.
It could be run at any individual service nodes.
https://prometheus.io/docs/prometheus/latest/storage/
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 15 di 47
PromQLPromQL
Prometheus query language
Can multiply, add, aggregate, join, predict, take
quantiles across many metrics in the same query
Not SQL style
Designed for time series data
http_requests_total{job="prometheus"}[5m]
http_requests_total{job="prometheus", code="200"}
http_requests_total{job="prometheus"}[5m]
http_requests_total{job="prometheus", code="200"}
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 16 di 47
Data modelData model
All data are store as time series
a float64 value
a millisecond-precision timestamp
Streams of timestamped values belonging to the
same metric
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 17 di 47
Metric names and labelsMetric names and labels
Every time series is identified by its metric name
Labels are key­value pairs
<metric name>{<label name>=<label value>, …}<metric name>{<label name>=<label value>, …}
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 18 di 47
Long term metric storageLong term metric storage
The default retention period is 15 days
The data become large if you set long retention
period
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 19 di 47
TimescaleDB
TimeScaleDBTimeScaleDB
Open source (Apache 2 license)
Time oriented features
Enabling long-term storage to Prometheus.
https://www.timescale.com/
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 21 di 47
https://docs.timescale.com/v1.2/getting-started/installation/docker/installation-docker
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 22 di 47
LicensingLicensing
https://www.timescale.com/products
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 23 di 47
Setup TimescaleDBSetup TimescaleDB
­­ Connect to the database
c myDB
­­ Extend the database with TimescaleDB
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
­­ Connect to the database
c myDB
­­ Extend the database with TimescaleDB
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
# Connect to PostgreSQL, using a superuser named 'postgres'
psql ­U postgres ­h localhost
# Connect to PostgreSQL, using a superuser named 'postgres'
psql ­U postgres ­h localhost
­­ Create a database
CREATE database myDB;
­­ Create a database
CREATE database myDB;
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 24 di 47
HypertableHypertable
Abstraction of a single continuous table across all
space and time intervals
Query via vanilla SQL
­­ We start by creating a regular SQL table
CREATE TABLE sensor_data (
  time             TIMESTAMPTZ       NOT NULL,
  location        TEXT              NOT NULL,
  temperature DOUBLE PRECISION  NULL,
  humidity       DOUBLE PRECISION  NULL,
  light              DOUBLE PRECISION  NULL
);
­­ Then we convert it into a hypertable that is partitioned by time
SELECT create_hypertable('sensor_data', 'time');
­­ We start by creating a regular SQL table
CREATE TABLE sensor_data (
  time             TIMESTAMPTZ       NOT NULL,
  location        TEXT              NOT NULL,
  temperature DOUBLE PRECISION  NULL,
  humidity       DOUBLE PRECISION  NULL,
  light              DOUBLE PRECISION  NULL
);
­­ Then we convert it into a hypertable that is partitioned by time
SELECT create_hypertable('sensor_data', 'time');
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 25 di 47
MigrationMigration
pg_dump for exporting schema and data
Copy over the database schema
choose which tables will become hypertables
Backup data (CSV).
Import the data into TimescaleDB
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 26 di 47
https://docs.timescale.com/v1.3/tutorials/prometheus-adapter
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 27 di 47
prometheus-postgresql-adapterprometheus-postgresql-adapter
Translation proxy
Data needs to be first deserialized and then
converted into the Prometheus native format
before it is inserted into the database
pg_prometheus is required
http://github.com/timescale/prometheus-postgresql-adapter/
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 28 di 47
pg_prometheuspg_prometheus
Postgresql extension
Translates from the Prometheus data model into a
compact SQL model
https://github.com/timescale/pg_prometheus
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 29 di 47
 Data are stored in a “normalized format”
 labels are stored in a labels table
 metric values in a values table
Column | Type | Modifiers
-------------+--------------------------+-----------
time | timestamp with time zone |
value | double precision |
labels_id | integer |
Column | Type | Modifiers
-------------+--------------------------+-----------
time | timestamp with time zone |
value | double precision |
labels_id | integer |
pg_prometheus – normalizationpg_prometheus – normalization
Column | Type | Modifiers
------------+---------+-------------------------------------------------------------
id | integer | not null default nextval('metrics_labels_id_seq'::regclass)
metric_name | text | not null
labels | jsonb |
Column | Type | Modifiers
------------+---------+-------------------------------------------------------------
id | integer | not null default nextval('metrics_labels_id_seq'::regclass)
metric_name | text | not null
labels | jsonb |
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 30 di 47
InstallationInstallation
Pre­compliled binaries
Docker image
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 31 di 47
Run all via docker composeRun all via docker compose
services:
  db:
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
    image: timescale/pg_prometheus:latest
    ports:
    ­ 5432:5432/tcp
  prometheus:
    image: prom/prometheus:latest
    ports:
    ­ 9090:9090/tcp
    volumes:
    ­ ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
  prometheus_postgresql_adapter:
    build:
      context: .
    depends_on:
    ­ db
    ­ prometheus
    environment:
      TS_PROM_LOG_LEVEL: debug
      TS_PROM_PG_DB_CONNECT_RETRIES: 10
      TS_PROM_PG_HOST: db
      TS_PROM_PG_PASSWORD: postgres
      TS_PROM_PG_SCHEMA: postgres
      TS_PROM_WEB_TELEMETRY_PATH: /metrics­text
    image: timescale/prometheus­postgresql­adapter:latest
    ports:
    ­ 9201:9201/tcp
version: '3.0'l
services:
  db:
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
    image: timescale/pg_prometheus:latest
    ports:
    ­ 5432:5432/tcp
  prometheus:
    image: prom/prometheus:latest
    ports:
    ­ 9090:9090/tcp
    volumes:
    ­ ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
  prometheus_postgresql_adapter:
    build:
      context: .
    depends_on:
    ­ db
    ­ prometheus
    environment:
      TS_PROM_LOG_LEVEL: debug
      TS_PROM_PG_DB_CONNECT_RETRIES: 10
      TS_PROM_PG_HOST: db
      TS_PROM_PG_PASSWORD: postgres
      TS_PROM_PG_SCHEMA: postgres
      TS_PROM_WEB_TELEMETRY_PATH: /metrics­text
    image: timescale/prometheus­postgresql­adapter:latest
    ports:
    ­ 9201:9201/tcp
version: '3.0'l
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 32 di 47
Prometheus.ymlPrometheus.yml
remote_write:
    ­ url: "http://prometheus_postgresql_adapter:9201/write"
remote_read:
    ­ url: "http://prometheus_postgresql_adapter:9201/read"
remote_write:
    ­ url: "http://prometheus_postgresql_adapter:9201/write"
remote_read:
    ­ url: "http://prometheus_postgresql_adapter:9201/read"
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 33 di 47
TimescaleDB- tuneTimescaleDB- tune
Program for tuning a TimescaleDB db
Parse existing postgresql.conf
Ensure extension is installed
Provide reccomendations
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 34 di 47
Prometheus HA
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 35 di 47
Prometheus HAPrometheus HA
Avoiding data loss
Single point of failure
Avoiding missing alerts
Local storage is limited by single nodes
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 36 di 47
Prometheus solutionPrometheus solution
Run identical Prometheus server on two or more
separate machines (same configuration)
Identical alerts will be deduplicated by the
Alertmanager.
https://prometheus.io/docs/introduction/faq/#can-prometheus-be-made-highly-available
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 37 di 47
https://mkezz.wordpress.com/2018/09/05/high-availability-prometheus-and-alertmanager/
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 38 di 47
But ...But ...
It can be hard to keep data in sync. Often parallel
Prometheus instances do not have identical data.
scrape intervals may differ, since each instance has its
own clock
Remote storage
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 39 di 47
Prometheus HA + TimescaleDBPrometheus HA + TimescaleDB
https://blog.timescale.com/prometheus-ha-postgresql-8de68d19b6f5/
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 40 di 47
Prometheus ConfigurationPrometheus Configuration
global:
 scrape_interval: 5s
 evaluation_interval: 10s
scrape_configs:
 ­ job_name: prometheus
   static_configs:
     ­ targets: ['localhost:9090']
remote_write:
 ­ url: "http://localhost:9201/write"
remote_read:
 ­ url: "http://localhost:9201/read"
   read_recent: true
global:
 scrape_interval: 5s
 evaluation_interval: 10s
scrape_configs:
 ­ job_name: prometheus
   static_configs:
     ­ targets: ['localhost:9090']
remote_write:
 ­ url: "http://localhost:9201/write"
remote_read:
 ­ url: "http://localhost:9201/read"
   read_recent: true
global:
 scrape_interval: 5s
 evaluation_interval: 10s
scrape_configs:
 ­ job_name: prometheus
   static_configs:
     ­ targets: ['localhost:9091']
remote_write:
 ­ url: "http://localhost:9202/write"
remote_read:
 ­ url: "http://localhost:9202/read"
   read_recent: true
global:
 scrape_interval: 5s
 evaluation_interval: 10s
scrape_configs:
 ­ job_name: prometheus
   static_configs:
     ­ targets: ['localhost:9091']
remote_write:
 ­ url: "http://localhost:9202/write"
remote_read:
 ­ url: "http://localhost:9202/read"
   read_recent: true
Node 1 Node 2
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 41 di 47
./prometheus ­­web.listen­address=:9091 –storage.tsdb.path=data1/
./prometheus ­­web.listen­address=:9092 ­­storage.tsdb.path=data2/
./prometheus ­­web.listen­address=:9091 –storage.tsdb.path=data1/
./prometheus ­­web.listen­address=:9092 ­­storage.tsdb.path=data2/
Start Prometheus instances
Start Prometheus-adapters
./prometheus­postgresql­adapter ­web.listen­address=:9201 ­leader­election.pg­advisory­lock­id=1
 ­leader­election.pg­advisory­lock.prometheus­timeout=6s
./prometheus­postgresql­adapter ­web.listen­address=:9202 ­leader­election.pg­advisory­lock­id=1 
­leader­election.pg­advisory­lock.prometheus­timeout=6s
./prometheus­postgresql­adapter ­web.listen­address=:9201 ­leader­election.pg­advisory­lock­id=1
 ­leader­election.pg­advisory­lock.prometheus­timeout=6s
./prometheus­postgresql­adapter ­web.listen­address=:9202 ­leader­election.pg­advisory­lock­id=1 
­leader­election.pg­advisory­lock.prometheus­timeout=6s
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 42 di 47
Adapter leader electionAdapter leader election
Allow only one instance to write into the Database
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 43 di 47
Concurrency controlConcurrency control
Consensum algoritm (Raft)
Acquire a shared mutex (lock)
Zookeeper, Consul
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 44 di 47
Postgresql advisory lockPostgresql advisory lock
Introduced in Postgresql 9.4
Application enforced database lock
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 45 di 47
ConclusionConclusion
HA support                     ?
Long storage support     ?
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 46 di 47
Questions?Questions?
PGDay.IT 2019 – 17 Magggio 2019 -Bologna 47 di 47

More Related Content

ODP
Patroni: PostgreSQL HA in the cloud
PDF
Paolo Kreth - Persistence layers for microservices – the converged database a...
PDF
Yann Albou & Sébastien Féré - GitOps as a way to manage enterprise K8s and vi...
PDF
Ballerina: Cloud Native Programming Language
PPTX
Exascale Process Management Interface
PPTX
SLUG 2015 PMIx Overview
PDF
Elasticsearch Monitoring in Openshift
PDF
resume-2016spring
Patroni: PostgreSQL HA in the cloud
Paolo Kreth - Persistence layers for microservices – the converged database a...
Yann Albou & Sébastien Féré - GitOps as a way to manage enterprise K8s and vi...
Ballerina: Cloud Native Programming Language
Exascale Process Management Interface
SLUG 2015 PMIx Overview
Elasticsearch Monitoring in Openshift
resume-2016spring

Similar to How to use Postgresql in order to handle Prometheus metrics storage (20)

PPTX
Timings API: Performance Assertion during the functional testing
PDF
Monitoring Flink with Prometheus
PDF
Flink Forward Berlin 2018: Maximilian Bode - "Monitoring Flink with Prometheus"
PDF
20181116 Massive Log Processing using I/O optimized PostgreSQL
PDF
CodeCamp Iasi - Creating serverless data analytics system on GCP using BigQuery
PDF
Cwin16 tls-a micro-service deployment - v1.0
PDF
Supercharge your data analytics with BigQuery
PDF
Ballerina: A Cloud Native Programming Language
PPTX
OpenACC and Open Hackathons Monthly Highlights: September 2022.pptx
PDF
Automatically partitioning packet processing applications for pipelined archi...
ODP
Rabbitmq & Postgresql
PDF
PPTX
OpenACC Monthly Highlights: November 2020
PDF
REST in Peace. Long live gRPC!
PDF
OpenACC and Hackathons Monthly Highlights: April 2023
PDF
GE Predix 新手入门 赵锴 物联网_IoT
PDF
Combinación de logs, métricas y seguimiento para una visibilidad centralizada
PPTX
OpenACC Monthly Highlights: February 2021
PPTX
GitHub Actions - using Free Oracle Cloud Infrastructure (OCI)
PPT
Os Lonergan
Timings API: Performance Assertion during the functional testing
Monitoring Flink with Prometheus
Flink Forward Berlin 2018: Maximilian Bode - "Monitoring Flink with Prometheus"
20181116 Massive Log Processing using I/O optimized PostgreSQL
CodeCamp Iasi - Creating serverless data analytics system on GCP using BigQuery
Cwin16 tls-a micro-service deployment - v1.0
Supercharge your data analytics with BigQuery
Ballerina: A Cloud Native Programming Language
OpenACC and Open Hackathons Monthly Highlights: September 2022.pptx
Automatically partitioning packet processing applications for pipelined archi...
Rabbitmq & Postgresql
OpenACC Monthly Highlights: November 2020
REST in Peace. Long live gRPC!
OpenACC and Hackathons Monthly Highlights: April 2023
GE Predix 新手入门 赵锴 物联网_IoT
Combinación de logs, métricas y seguimiento para una visibilidad centralizada
OpenACC Monthly Highlights: February 2021
GitHub Actions - using Free Oracle Cloud Infrastructure (OCI)
Os Lonergan
Ad

More from Lucio Grenzi (11)

ODP
Building serverless application on the Apache Openwhisk platform
ODP
Postgrest: the REST API for PostgreSQL databases
PPTX
Full slidescr16
ODP
Use Ionic Framework to develop mobile application
ODP
Jenkins djangovillage
ODP
Geodjango and HTML 5
ODP
PLV8 - The PostgreSQL web side
ODP
Pg tap
PPT
Geodjango
PPT
Yui app-framework
PPT
node.js e Postgresql
Building serverless application on the Apache Openwhisk platform
Postgrest: the REST API for PostgreSQL databases
Full slidescr16
Use Ionic Framework to develop mobile application
Jenkins djangovillage
Geodjango and HTML 5
PLV8 - The PostgreSQL web side
Pg tap
Geodjango
Yui app-framework
node.js e Postgresql
Ad

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Transform Your Business with a Software ERP System
PDF
Nekopoi APK 2025 free lastest update
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
history of c programming in notes for students .pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
medical staffing services at VALiNTRY
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administration Chapter 2
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
CHAPTER 2 - PM Management and IT Context
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Transform Your Business with a Software ERP System
Nekopoi APK 2025 free lastest update
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
history of c programming in notes for students .pptx
Reimagine Home Health with the Power of Agentic AI​
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
medical staffing services at VALiNTRY
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administration Chapter 2
Operating system designcfffgfgggggggvggggggggg
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
CHAPTER 2 - PM Management and IT Context

How to use Postgresql in order to handle Prometheus metrics storage

  • 1. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 1 di 47 Come usare Postgresql per gestire lo storage  delle metriche di Prometheus Lucio Grenzi l.grenzi@gmail.com
  • 2. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 2 di 47 Delphi developer since 1999 IT Consultant Full stack web developer Postgresql addicted       lucio grenzi dogwolf Who I am
  • 3. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 3 di 47 AgendaAgenda  Intro to Prometheus Intro to TimeScaleDB Pg_prometheus Prometheus-postgresql-adapter Prometheus HA Conclusion and final thoughts
  • 4. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 4 di 47 Why monitoring activities?Why monitoring activities? Detect errors and bottlenecks Auditing Measure performances Trending to see changes during time Drive business/technical processes
  • 5. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 5 di 47 Prometheus
  • 6. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 6 di 47 PrometheusPrometheus Open source (Apache 2 license) Only metrics, not logging Pull based approach Multidimensional data model Built-in time series database Flexibile query langage (Qml) Alertmanager handle notifications For all levels of the stack 
  • 7. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 7 di 47 Prometheus historyPrometheus history Inspired by the monitoring tool Borgmon used at Google Developed at SoundCloud starting in 2012 In May 2016, the Cloud Native Computing Foundation accepted Prometheus as its second incubated project Inspired by the monitoring tool Borgmon used at Google Developed at SoundCloud starting in 2012 In May 2016, the Cloud Native Computing Foundation accepted Prometheus as its second incubated project
  • 8. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 8 di 47 Prometheus CommunityPrometheus Community Open source project IRC: #prometheus User mailing lists: Prometheus-announce Prometheus-users Prometheus-announce. Third-party commercial support
  • 9. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 9 di 47 Prometheus architecturePrometheus architecture
  • 10. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 10 di 47 Integration withIntegration with Many existing integrations: Java, JMX, Python, Go, Ruby, .Net, Machine, Cloudwatch, EC2, MySQL, PostgreSQL, Haskell, Bash, Node.js, SNMP, Consul, HAProxy, Mesos, Bind, CouchDB, Django, Mtail, Heka, Memcached, RabbitMQ, Redis, RethinkDB, Rsyslog, Meteor.js, Minecraft... Graphite, Statsd, Collectd, Scollector, Munin, Nagios integrations aid transition.
  • 11. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 11 di 47 InstallationInstallation Pre-compiled binaries Compile from source: makefile Docker Ansible Chef Puppet Saltstack
  • 12. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 12 di 47 ConfigurationConfiguration  Written in Yaml format Sections: global rule_files scrape_configs alerting remote_write remote_read
  • 13. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 13 di 47 # my global config global: scrape_interval: 15s evaluation_interval: 30s # scrape_timeout is set to the global default (10s). external_labels: monitor: codelab foo: bar rule_files: - "first.rules" - "my/*.rules" remote_write: - url: http://remote1/push write_relabel_configs: - source_labels: [__name__] regex: expensive.* action: drop - url: http://remote2/push tls_config: cert_file: valid_cert_file key_file: valid_key_file remote_read: - url: http://remote1/read read_recent: true - url: http://remote3/read read_recent: false required_matchers: job: special tls_config: cert_file: valid_cert_file key_file: valid_key_file scrape_configs: - job_name: prometheus honor_labels: true # scrape_interval is defined by the configured global (15s). # scrape_timeout is defined by the global default (10s). # metrics_path defaults to '/metrics' # scheme defaults to 'http'. file_sd_configs: - files: - foo/*.slow.json - foo/*.slow.yml - single/file.yml refresh_interval: 10m - files: - bar/*.yaml static_configs: - targets: ['localhost:9090', 'localhost:9191'] labels: my: label your: label relabel_configs: - source_labels: [job, __meta_dns_name] regex: (.*)some-[regex] target_label: job replacement: foo-${1} # action defaults to 'replace' - source_labels: [abc] target_label: cde - replacement: static target_label: abc
  • 14. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 14 di 47 Prometheus storagePrometheus storage Local on-disk time series database Optionally integrates with remote storage systems Not depending on distributed storage. It could be run at any individual service nodes. https://prometheus.io/docs/prometheus/latest/storage/
  • 15. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 15 di 47 PromQLPromQL Prometheus query language Can multiply, add, aggregate, join, predict, take quantiles across many metrics in the same query Not SQL style Designed for time series data http_requests_total{job="prometheus"}[5m] http_requests_total{job="prometheus", code="200"} http_requests_total{job="prometheus"}[5m] http_requests_total{job="prometheus", code="200"}
  • 16. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 16 di 47 Data modelData model All data are store as time series a float64 value a millisecond-precision timestamp Streams of timestamped values belonging to the same metric
  • 17. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 17 di 47 Metric names and labelsMetric names and labels Every time series is identified by its metric name Labels are key­value pairs <metric name>{<label name>=<label value>, …}<metric name>{<label name>=<label value>, …}
  • 18. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 18 di 47 Long term metric storageLong term metric storage The default retention period is 15 days The data become large if you set long retention period
  • 19. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 19 di 47 TimescaleDB
  • 20. TimeScaleDBTimeScaleDB Open source (Apache 2 license) Time oriented features Enabling long-term storage to Prometheus. https://www.timescale.com/
  • 21. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 21 di 47 https://docs.timescale.com/v1.2/getting-started/installation/docker/installation-docker
  • 22. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 22 di 47 LicensingLicensing https://www.timescale.com/products
  • 23. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 23 di 47 Setup TimescaleDBSetup TimescaleDB ­­ Connect to the database c myDB ­­ Extend the database with TimescaleDB CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; ­­ Connect to the database c myDB ­­ Extend the database with TimescaleDB CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; # Connect to PostgreSQL, using a superuser named 'postgres' psql ­U postgres ­h localhost # Connect to PostgreSQL, using a superuser named 'postgres' psql ­U postgres ­h localhost ­­ Create a database CREATE database myDB; ­­ Create a database CREATE database myDB;
  • 24. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 24 di 47 HypertableHypertable Abstraction of a single continuous table across all space and time intervals Query via vanilla SQL ­­ We start by creating a regular SQL table CREATE TABLE sensor_data (   time             TIMESTAMPTZ       NOT NULL,   location        TEXT              NOT NULL,   temperature DOUBLE PRECISION  NULL,   humidity       DOUBLE PRECISION  NULL,   light              DOUBLE PRECISION  NULL ); ­­ Then we convert it into a hypertable that is partitioned by time SELECT create_hypertable('sensor_data', 'time'); ­­ We start by creating a regular SQL table CREATE TABLE sensor_data (   time             TIMESTAMPTZ       NOT NULL,   location        TEXT              NOT NULL,   temperature DOUBLE PRECISION  NULL,   humidity       DOUBLE PRECISION  NULL,   light              DOUBLE PRECISION  NULL ); ­­ Then we convert it into a hypertable that is partitioned by time SELECT create_hypertable('sensor_data', 'time');
  • 25. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 25 di 47 MigrationMigration pg_dump for exporting schema and data Copy over the database schema choose which tables will become hypertables Backup data (CSV). Import the data into TimescaleDB
  • 26. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 26 di 47 https://docs.timescale.com/v1.3/tutorials/prometheus-adapter
  • 27. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 27 di 47 prometheus-postgresql-adapterprometheus-postgresql-adapter Translation proxy Data needs to be first deserialized and then converted into the Prometheus native format before it is inserted into the database pg_prometheus is required http://github.com/timescale/prometheus-postgresql-adapter/
  • 28. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 28 di 47 pg_prometheuspg_prometheus Postgresql extension Translates from the Prometheus data model into a compact SQL model https://github.com/timescale/pg_prometheus
  • 29. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 29 di 47  Data are stored in a “normalized format”  labels are stored in a labels table  metric values in a values table Column | Type | Modifiers -------------+--------------------------+----------- time | timestamp with time zone | value | double precision | labels_id | integer | Column | Type | Modifiers -------------+--------------------------+----------- time | timestamp with time zone | value | double precision | labels_id | integer | pg_prometheus – normalizationpg_prometheus – normalization Column | Type | Modifiers ------------+---------+------------------------------------------------------------- id | integer | not null default nextval('metrics_labels_id_seq'::regclass) metric_name | text | not null labels | jsonb | Column | Type | Modifiers ------------+---------+------------------------------------------------------------- id | integer | not null default nextval('metrics_labels_id_seq'::regclass) metric_name | text | not null labels | jsonb |
  • 30. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 30 di 47 InstallationInstallation Pre­compliled binaries Docker image
  • 31. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 31 di 47 Run all via docker composeRun all via docker compose services:   db:     environment:       POSTGRES_PASSWORD: postgres       POSTGRES_USER: postgres     image: timescale/pg_prometheus:latest     ports:     ­ 5432:5432/tcp   prometheus:     image: prom/prometheus:latest     ports:     ­ 9090:9090/tcp     volumes:     ­ ./prometheus.yml:/etc/prometheus/prometheus.yml:ro   prometheus_postgresql_adapter:     build:       context: .     depends_on:     ­ db     ­ prometheus     environment:       TS_PROM_LOG_LEVEL: debug       TS_PROM_PG_DB_CONNECT_RETRIES: 10       TS_PROM_PG_HOST: db       TS_PROM_PG_PASSWORD: postgres       TS_PROM_PG_SCHEMA: postgres       TS_PROM_WEB_TELEMETRY_PATH: /metrics­text     image: timescale/prometheus­postgresql­adapter:latest     ports:     ­ 9201:9201/tcp version: '3.0'l services:   db:     environment:       POSTGRES_PASSWORD: postgres       POSTGRES_USER: postgres     image: timescale/pg_prometheus:latest     ports:     ­ 5432:5432/tcp   prometheus:     image: prom/prometheus:latest     ports:     ­ 9090:9090/tcp     volumes:     ­ ./prometheus.yml:/etc/prometheus/prometheus.yml:ro   prometheus_postgresql_adapter:     build:       context: .     depends_on:     ­ db     ­ prometheus     environment:       TS_PROM_LOG_LEVEL: debug       TS_PROM_PG_DB_CONNECT_RETRIES: 10       TS_PROM_PG_HOST: db       TS_PROM_PG_PASSWORD: postgres       TS_PROM_PG_SCHEMA: postgres       TS_PROM_WEB_TELEMETRY_PATH: /metrics­text     image: timescale/prometheus­postgresql­adapter:latest     ports:     ­ 9201:9201/tcp version: '3.0'l
  • 32. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 32 di 47 Prometheus.ymlPrometheus.yml remote_write:     ­ url: "http://prometheus_postgresql_adapter:9201/write" remote_read:     ­ url: "http://prometheus_postgresql_adapter:9201/read" remote_write:     ­ url: "http://prometheus_postgresql_adapter:9201/write" remote_read:     ­ url: "http://prometheus_postgresql_adapter:9201/read"
  • 33. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 33 di 47 TimescaleDB- tuneTimescaleDB- tune Program for tuning a TimescaleDB db Parse existing postgresql.conf Ensure extension is installed Provide reccomendations
  • 34. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 34 di 47 Prometheus HA
  • 35. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 35 di 47 Prometheus HAPrometheus HA Avoiding data loss Single point of failure Avoiding missing alerts Local storage is limited by single nodes
  • 36. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 36 di 47 Prometheus solutionPrometheus solution Run identical Prometheus server on two or more separate machines (same configuration) Identical alerts will be deduplicated by the Alertmanager. https://prometheus.io/docs/introduction/faq/#can-prometheus-be-made-highly-available
  • 37. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 37 di 47 https://mkezz.wordpress.com/2018/09/05/high-availability-prometheus-and-alertmanager/
  • 38. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 38 di 47 But ...But ... It can be hard to keep data in sync. Often parallel Prometheus instances do not have identical data. scrape intervals may differ, since each instance has its own clock Remote storage
  • 39. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 39 di 47 Prometheus HA + TimescaleDBPrometheus HA + TimescaleDB https://blog.timescale.com/prometheus-ha-postgresql-8de68d19b6f5/
  • 40. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 40 di 47 Prometheus ConfigurationPrometheus Configuration global:  scrape_interval: 5s  evaluation_interval: 10s scrape_configs:  ­ job_name: prometheus    static_configs:      ­ targets: ['localhost:9090'] remote_write:  ­ url: "http://localhost:9201/write" remote_read:  ­ url: "http://localhost:9201/read"    read_recent: true global:  scrape_interval: 5s  evaluation_interval: 10s scrape_configs:  ­ job_name: prometheus    static_configs:      ­ targets: ['localhost:9090'] remote_write:  ­ url: "http://localhost:9201/write" remote_read:  ­ url: "http://localhost:9201/read"    read_recent: true global:  scrape_interval: 5s  evaluation_interval: 10s scrape_configs:  ­ job_name: prometheus    static_configs:      ­ targets: ['localhost:9091'] remote_write:  ­ url: "http://localhost:9202/write" remote_read:  ­ url: "http://localhost:9202/read"    read_recent: true global:  scrape_interval: 5s  evaluation_interval: 10s scrape_configs:  ­ job_name: prometheus    static_configs:      ­ targets: ['localhost:9091'] remote_write:  ­ url: "http://localhost:9202/write" remote_read:  ­ url: "http://localhost:9202/read"    read_recent: true Node 1 Node 2
  • 41. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 41 di 47 ./prometheus ­­web.listen­address=:9091 –storage.tsdb.path=data1/ ./prometheus ­­web.listen­address=:9092 ­­storage.tsdb.path=data2/ ./prometheus ­­web.listen­address=:9091 –storage.tsdb.path=data1/ ./prometheus ­­web.listen­address=:9092 ­­storage.tsdb.path=data2/ Start Prometheus instances Start Prometheus-adapters ./prometheus­postgresql­adapter ­web.listen­address=:9201 ­leader­election.pg­advisory­lock­id=1  ­leader­election.pg­advisory­lock.prometheus­timeout=6s ./prometheus­postgresql­adapter ­web.listen­address=:9202 ­leader­election.pg­advisory­lock­id=1  ­leader­election.pg­advisory­lock.prometheus­timeout=6s ./prometheus­postgresql­adapter ­web.listen­address=:9201 ­leader­election.pg­advisory­lock­id=1  ­leader­election.pg­advisory­lock.prometheus­timeout=6s ./prometheus­postgresql­adapter ­web.listen­address=:9202 ­leader­election.pg­advisory­lock­id=1  ­leader­election.pg­advisory­lock.prometheus­timeout=6s
  • 42. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 42 di 47 Adapter leader electionAdapter leader election Allow only one instance to write into the Database
  • 43. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 43 di 47 Concurrency controlConcurrency control Consensum algoritm (Raft) Acquire a shared mutex (lock) Zookeeper, Consul
  • 44. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 44 di 47 Postgresql advisory lockPostgresql advisory lock Introduced in Postgresql 9.4 Application enforced database lock
  • 45. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 45 di 47 ConclusionConclusion HA support                     ? Long storage support     ?
  • 46. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 46 di 47 Questions?Questions?
  • 47. PGDay.IT 2019 – 17 Magggio 2019 -Bologna 47 di 47