-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (63 loc) · 1.7 KB
/
main.go
File metadata and controls
76 lines (63 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/kintsdev/postmetric/pkg/metrics"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
port = TenaryString(os.Getenv("PORT") == "", "8080", os.Getenv("PORT"))
host = TenaryString(os.Getenv("HOST") == "", "", os.Getenv("HOST"))
)
func TenaryString(value bool, v1 string, v2 string) string {
if value {
return v1
}
return v2
}
func init() {
metrics.RegisterMetrics()
}
func main() {
// PostgreSQL connection string from environment variables
pgConnStr := os.Getenv("POSTGRES_CONNECTION_STRING")
if pgConnStr == "" {
log.Fatal("Environment variable POSTGRES_CONNECTION_STRING is required.")
}
// Create connection pool configuration
config, err := pgxpool.ParseConfig(pgConnStr)
if err != nil {
log.Fatalf("Error parsing connection string: %v", err)
}
// Set pool configuration
config.MaxConns = 5
config.MinConns = 1
config.MaxConnLifetime = time.Hour
config.MaxConnIdleTime = 30 * time.Minute
// Create connection pool
pool, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatalf("Error connecting to PostgreSQL: %v", err)
}
defer pool.Close()
// Periodic metric collection
go func() {
for {
metrics.CollectMetrics(context.Background(), pool)
log.Println("Metrics collected.")
time.Sleep(10 * time.Second)
}
}()
// Expose metrics endpoint
http.Handle("/", http.RedirectHandler("/metrics", http.StatusMovedPermanently))
http.Handle("/metrics", promhttp.Handler())
log.Println("Prometheus exporter started on " + net.JoinHostPort(host, port))
log.Fatal(
http.ListenAndServe(net.JoinHostPort(host, port), nil),
)
}