diff --git a/examples/dbstatscollectorexample/main.go b/examples/dbstatscollectorexample/main.go new file mode 100644 index 0000000..1ac0682 --- /dev/null +++ b/examples/dbstatscollectorexample/main.go @@ -0,0 +1,64 @@ +// Copyright 2022 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// A minimal example of how to include Prometheus instrumentation for database stats. +package main + +import ( + "database/sql" + "flag" + "fmt" + "log" + "net/http" + "time" + + _ "github.com/mattn/go-sqlite3" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") + +func main() { + flag.Parse() + + // Set up an in-memory SQLite DB. + db, err := sql.Open("sqlite3", ":memory:") // In-memory SQLite database + if err != nil { + log.Fatalf("Failed to connect to in-memory database: %v", err) + } + defer db.Close() + + // Set connection pool limits to simulate more activity. + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(5) + db.SetConnMaxIdleTime(5 * time.Minute) + db.SetConnMaxLifetime(30 * time.Minute) + + // Create a new Prometheus registry. + reg := prometheus.NewRegistry() + + // Create and register the DB stats collector. + dbStatsCollector := collectors.NewDBStatsCollector(db, "sqlite_in_memory") + reg.MustRegister(dbStatsCollector) + + // Expose the registered metrics via HTTP. + http.Handle("/metrics", promhttp.HandlerFor( + reg, + promhttp.HandlerOpts{}, + )) + + fmt.Println("Server is running, metrics are available at /metrics") + log.Fatal(http.ListenAndServe(*addr, nil)) +} diff --git a/go.mod b/go.mod index 1339dae..e22f203 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/json-iterator/go v1.1.12 github.com/klauspost/compress v1.17.10 github.com/kylelemons/godebug v1.1.0 + github.com/mattn/go-sqlite3 v1.14.24 github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.59.1 github.com/prometheus/procfs v0.15.1 diff --git a/go.sum b/go.sum index abdd66f..ee58d17 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=