Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
chore(backend): add sqlite metrics + dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Feb 28, 2024
1 parent 911426e commit 86ab6a5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
26 changes: 26 additions & 0 deletions backend/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"runtime"

"crawshaw.io/sqlite"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -21,4 +22,29 @@ func init() {
// Unregister default Go runtime collector, and register it again with more metrics.
prometheus.Unregister(collectors.NewGoCollector())
prometheus.MustRegister(collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsAll)))

prometheus.MustRegister(newSQLiteCollector())
}

type sqliteCollector struct {
prepares *prometheus.Desc
}

func newSQLiteCollector() *sqliteCollector {
return &sqliteCollector{
prepares: prometheus.NewDesc(
"mintter_sqlite_queries_total",
"Total number of queries executed.",
nil,
nil,
),
}
}

func (s *sqliteCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- s.prepares
}

func (s *sqliteCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(s.prepares, prometheus.CounterValue, float64(sqlite.PrepareCount()))
}
107 changes: 103 additions & 4 deletions monitoring/grafana/dashboards/mintter/desktop.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
},
"id": 7,
"panels": [],
"title": "Networking",
"title": "mintterd",
"type": "row"
},
{
Expand Down Expand Up @@ -742,8 +742,7 @@
},
"properties": [
{
"id": "custom.width",
"value": null
"id": "custom.width"
}
]
}
Expand Down Expand Up @@ -816,6 +815,106 @@
}
],
"type": "table"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": 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
}
]
},
"unit": "qps",
"unitScale": true
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 30
},
"id": 11,
"options": {
"legend": {
"calcs": [
"mean",
"last",
"min",
"max"
],
"displayMode": "table",
"placement": "bottom",
"showLegend": true,
"sortBy": "Mean",
"sortDesc": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"expr": "rate(mintter_sqlite_queries_total{job=\"mintterd\", instance=~\"$instance\"}[$__rate_interval])",
"instant": false,
"legendFormat": "{{instance}} – Queries",
"range": true,
"refId": "A"
}
],
"title": "SQLite Queries Rate",
"type": "timeseries"
}
],
"refresh": "10s",
Expand Down Expand Up @@ -883,6 +982,6 @@
"timezone": "browser",
"title": "Mintter Desktop Overview",
"uid": "dddh3zotqe22od",
"version": 13,
"version": 14,
"weekStart": ""
}
14 changes: 14 additions & 0 deletions third_party/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,20 @@ import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
"unsafe"
)

var prepareCount uint64

// PrepareCount returns the total number of prepare calls made to the SQLite library.
// This counter is global for the entire process, and is quite a bit less than ideal,
// but no time to implement it more nicely.
func PrepareCount() uint64 {
return atomic.LoadUint64(&prepareCount)
}

// Conn is an open connection to an SQLite3 database.
//
// A Conn can only be used by goroutine at a time.
Expand Down Expand Up @@ -379,6 +389,8 @@ func (conn *Conn) Prep(query string) *Stmt {
//
// https://www.sqlite.org/c3ref/prepare.html
func (conn *Conn) Prepare(query string) (*Stmt, error) {
atomic.AddUint64(&prepareCount, 1)

if stmt := conn.stmts[query]; stmt != nil {
if err := stmt.Reset(); err != nil {
return nil, err
Expand Down Expand Up @@ -419,6 +431,8 @@ func (conn *Conn) Prepare(query string) (*Stmt, error) {
//
// https://www.sqlite.org/c3ref/prepare.html
func (conn *Conn) PrepareTransient(query string) (stmt *Stmt, trailingBytes int, err error) {
atomic.AddUint64(&prepareCount, 1)

stmt, trailingBytes, err = conn.prepare(query, 0)
if stmt != nil {
runtime.SetFinalizer(stmt, func(stmt *Stmt) {
Expand Down

0 comments on commit 86ab6a5

Please sign in to comment.