Skip to content

Commit 7158b92

Browse files
committed
add example for dbstatscollector
Signed-off-by: manask322 <[email protected]>
1 parent e1675ce commit 7158b92

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build go1.17
2+
// +build go1.17
3+
4+
// A minimal example of how to include Prometheus instrumentation for database stats.
5+
package main
6+
7+
import (
8+
"database/sql"
9+
"flag"
10+
"fmt"
11+
"log"
12+
"net/http"
13+
"time"
14+
15+
_ "github.com/mattn/go-sqlite3"
16+
"github.com/prometheus/client_golang/prometheus"
17+
"github.com/prometheus/client_golang/prometheus/collectors"
18+
"github.com/prometheus/client_golang/prometheus/promhttp"
19+
)
20+
21+
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
22+
23+
func main() {
24+
flag.Parse()
25+
26+
// Set up an in-memory SQLite DB.
27+
db, err := sql.Open("sqlite3", ":memory:") // In-memory SQLite database
28+
if err != nil {
29+
log.Fatalf("Failed to connect to in-memory database: %v", err)
30+
}
31+
defer db.Close()
32+
33+
// Set connection pool limits to simulate more activity.
34+
db.SetMaxOpenConns(10)
35+
db.SetMaxIdleConns(5)
36+
db.SetConnMaxIdleTime(5 * time.Minute)
37+
db.SetConnMaxLifetime(30 * time.Minute)
38+
39+
// Create a new Prometheus registry.
40+
reg := prometheus.NewRegistry()
41+
42+
// Create and register the DB stats collector.
43+
dbStatsCollector := collectors.NewDBStatsCollector(db, "sqlite_in_memory")
44+
reg.MustRegister(dbStatsCollector)
45+
46+
// Expose the registered metrics via HTTP.
47+
http.Handle("/metrics", promhttp.HandlerFor(
48+
reg,
49+
promhttp.HandlerOpts{},
50+
))
51+
52+
fmt.Println("Server is running, metrics are available at /metrics")
53+
log.Fatal(http.ListenAndServe(*addr, nil))
54+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/json-iterator/go v1.1.12
1010
github.com/klauspost/compress v1.17.10
1111
github.com/kylelemons/godebug v1.1.0
12+
github.com/mattn/go-sqlite3 v1.14.24
1213
github.com/prometheus/client_model v0.6.1
1314
github.com/prometheus/common v0.59.1
1415
github.com/prometheus/procfs v0.15.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2121
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2222
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
2323
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
24+
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
25+
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
2426
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2527
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
2628
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

0 commit comments

Comments
 (0)