-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af7fb93
commit 6d0a676
Showing
13 changed files
with
1,551 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.1' | ||
|
||
services: | ||
|
||
postgres: | ||
image: postgres:11-alpine | ||
environment: | ||
POSTGRES_DB: pg-test | ||
ports: | ||
- 5432:5432 | ||
tmpfs: /var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.1' | ||
|
||
services: | ||
|
||
postgres: | ||
image: postgres:11-alpine | ||
environment: | ||
POSTGRES_DB: db-example | ||
ports: | ||
- 5432:5432 | ||
tmpfs: /var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/lab259/go-rscsrv" | ||
"github.com/lab259/go-rscsrv-prometheus/examples/promsql/driver_collector/services" | ||
"github.com/lab259/go-rscsrv-prometheus/promhermes" | ||
h "github.com/lab259/hermes" | ||
"github.com/lab259/hermes/middlewares" | ||
) | ||
|
||
func main() { | ||
serviceStarter := rscsrv.DefaultServiceStarter( | ||
&services.DefaultDriverCollector, | ||
&services.DefaultPromService, | ||
) | ||
if err := serviceStarter.Start(); err != nil { | ||
panic(err) | ||
} | ||
|
||
router := h.DefaultRouter() | ||
router.Use(middlewares.RecoverableMiddleware, middlewares.LoggingMiddleware) | ||
router.Get("/metrics", promhermes.Handler(&services.DefaultPromService)) | ||
|
||
app := h.NewApplication(h.ApplicationConfig{ | ||
ServiceStarter: serviceStarter, | ||
HTTP: h.FasthttpServiceConfiguration{ | ||
Bind: ":3000", | ||
}, | ||
}, router) | ||
|
||
log.Println("Go to http://localhost:3000/metrics") | ||
|
||
// Updating metrics | ||
go func() { | ||
psqlInfo := fmt.Sprintf("user=postgres password=postgres dbname=pg-test sslmode=disable") | ||
db, err := sql.Open(services.DefaultDriverCollector.DriverName, psqlInfo) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
|
||
err = db.Ping() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
db.Exec(`DROP TABLE users`) | ||
|
||
_, err = db.Exec(` | ||
CREATE TABLE users ( | ||
id int NOT NULL PRIMARY KEY, | ||
name text NOT NULL | ||
); | ||
INSERT INTO users (id, name) | ||
VALUES (1, 'john') | ||
`) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var opt int = 1 | ||
|
||
for { | ||
time.Sleep(3 * time.Second) | ||
switch opt { | ||
case 1: | ||
log.Println("Executing query") | ||
_, err := db.Query("select name from users") | ||
if err != nil { | ||
panic(err) | ||
} | ||
opt++ | ||
case 2: | ||
log.Println("Executing update") | ||
_, err := db.Exec("update users set name = 'Charles' where id = 1") | ||
if err != nil { | ||
panic(err) | ||
} | ||
opt = 1 | ||
default: | ||
panic("invalid option") | ||
} | ||
} | ||
}() | ||
app.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package services | ||
|
||
import ( | ||
promsrv "github.com/lab259/go-rscsrv-prometheus" | ||
) | ||
|
||
var DefaultPromService PromService | ||
|
||
type PromService struct { | ||
promsrv.Service | ||
} | ||
|
||
// Name implements the rscsrv.Service interface. | ||
func (srv *PromService) Name() string { | ||
return "Prometheus Service" | ||
} | ||
|
||
func (service *PromService) Start() error { | ||
return nil | ||
} |
42 changes: 42 additions & 0 deletions
42
examples/promsql/driver_collector/services/psql_collector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/lab259/go-rscsrv-prometheus/promsql" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
var DefaultDriverCollector DriverCollectorService | ||
|
||
type DriverCollectorService struct { | ||
promsql.DriverCollector | ||
} | ||
|
||
// Name implements the rscsrv.Service interface. | ||
func (srv *DriverCollectorService) Name() string { | ||
return "Driver Collector Service" | ||
} | ||
|
||
func (service *DriverCollectorService) Start() error { | ||
collector, err := promsql.Register(promsql.DriverCollectorOpts{ | ||
DriverName: "postgres", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
service.DriverCollector = *collector | ||
|
||
return DefaultPromService.Register(&service.DriverCollector) | ||
} | ||
|
||
// Restart restarts the Prometheus service. | ||
func (service *DriverCollectorService) Restart() error { | ||
if err := service.Stop(); err != nil { | ||
return err | ||
} | ||
return service.Start() | ||
} | ||
|
||
// Stop stops the Prometheus service. | ||
func (service *DriverCollectorService) Stop() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.