Skip to content

Commit 13df7a9

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

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// A minimal example of how to include Prometheus instrumentation for database stats.
15+
package main
16+
17+
import (
18+
"database/sql"
19+
"flag"
20+
"fmt"
21+
"log"
22+
"net/http"
23+
"time"
24+
25+
_ "github.com/mattn/go-sqlite3"
26+
"github.com/prometheus/client_golang/prometheus"
27+
"github.com/prometheus/client_golang/prometheus/collectors"
28+
"github.com/prometheus/client_golang/prometheus/promhttp"
29+
)
30+
31+
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
32+
33+
func main() {
34+
flag.Parse()
35+
36+
// Set up an in-memory SQLite DB.
37+
db, err := sql.Open("sqlite3", ":memory:") // In-memory SQLite database
38+
if err != nil {
39+
log.Fatalf("Failed to connect to in-memory database: %v", err)
40+
}
41+
defer db.Close()
42+
43+
// Set connection pool limits to simulate more activity.
44+
db.SetMaxOpenConns(10)
45+
db.SetMaxIdleConns(5)
46+
db.SetConnMaxIdleTime(5 * time.Minute)
47+
db.SetConnMaxLifetime(30 * time.Minute)
48+
49+
// Create a new Prometheus registry.
50+
reg := prometheus.NewRegistry()
51+
52+
// Create and register the DB stats collector.
53+
dbStatsCollector := collectors.NewDBStatsCollector(db, "sqlite_in_memory")
54+
reg.MustRegister(dbStatsCollector)
55+
56+
// Expose the registered metrics via HTTP.
57+
http.Handle("/metrics", promhttp.HandlerFor(
58+
reg,
59+
promhttp.HandlerOpts{},
60+
))
61+
62+
fmt.Println("Server is running, metrics are available at /metrics")
63+
log.Fatal(http.ListenAndServe(*addr, nil))
64+
}

go.mod

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)