Skip to content

Commit 14ca42a

Browse files
authored
Merge pull request #33 from benjamin99/feature/expose-prom-metrics
feat: add the support to export the metrics on the agent cmd
2 parents 3ec51de + 3451835 commit 14ca42a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

cmd/agent.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,32 @@ import (
2323
)
2424

2525
var (
26-
AgentListenAddr string
27-
AgentRenice int64
26+
AgentListenAddr string
27+
AgentRenice int64
28+
ExportPrometheus bool
29+
PrometheusAddr string
2830
)
2931

3032
func init() {
3133
rootCmd.AddCommand(agent)
3234
agent.Flags().StringVarP(&AgentListenAddr, "ListenAddr", "", ":10000", "the tcp address for agent server to listen")
3335
agent.Flags().Int64VarP(&AgentRenice, "Renice", "", -10, "try renice the sink pg process")
36+
agent.Flags().BoolVarP(&ExportPrometheus, "ExportPrometheus", "", false, "export the prometheus metrics or not")
37+
agent.Flags().StringVarP(&PrometheusAddr, "PrometheusAddr", "", ":2112", "the tcp address for prometheus server to listen")
3438
}
3539

3640
var agent = &cobra.Command{
3741
Use: "agent",
3842
Short: "run as a agent accepting remote config",
3943
RunE: func(cmd *cobra.Command, args []string) (err error) {
44+
if ExportPrometheus {
45+
startPrometheusServer(PrometheusAddr)
46+
}
47+
4048
logrus.WithFields(logrus.Fields{
4149
"AgentListenAddr": AgentListenAddr,
4250
}).Info("starting agent")
51+
4352
agent := &Agent{}
4453
return serveGRPC(&pb.Agent_ServiceDesc, AgentListenAddr, agent, func() {
4554
if err := agent.cleanup(); err != nil {

cmd/root.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"syscall"
1313

14+
"github.com/prometheus/client_golang/prometheus/promhttp"
1415
"github.com/rueian/pgcapture/pkg/sink"
1516
"github.com/rueian/pgcapture/pkg/source"
1617
"github.com/sirupsen/logrus"
@@ -106,3 +107,24 @@ func trimSlot(topic string) string {
106107
topic = strings.ReplaceAll(topic, "-", "_")
107108
return topic
108109
}
110+
111+
func startPrometheusServer(addr string) {
112+
handler := promhttp.Handler()
113+
server := &http.Server{
114+
Addr: addr,
115+
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
116+
if req.Method == http.MethodGet && req.URL.Path == "/metrics" {
117+
handler.ServeHTTP(w, req)
118+
} else {
119+
http.NotFound(w, req)
120+
}
121+
}),
122+
}
123+
124+
logrus.WithFields(logrus.Fields{"addr": addr}).Info("starting prometheus server")
125+
go func() {
126+
if err := server.ListenAndServe(); err != nil {
127+
logrus.Error(err)
128+
}
129+
}()
130+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/jackc/pgtype v1.7.0
1515
github.com/jackc/pgx/v4 v4.10.1
1616
github.com/pganalyze/pg_query_go/v2 v2.0.2
17+
github.com/prometheus/client_golang v1.11.1
1718
github.com/sirupsen/logrus v1.6.0
1819
github.com/spf13/cobra v1.7.0
1920
github.com/streamnative/pulsar-admin-go v0.1.0
@@ -48,7 +49,6 @@ require (
4849
github.com/mtibben/percent v0.2.1 // indirect
4950
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
5051
github.com/pkg/errors v0.9.1 // indirect
51-
github.com/prometheus/client_golang v1.11.1 // indirect
5252
github.com/prometheus/client_model v0.2.0 // indirect
5353
github.com/prometheus/common v0.26.0 // indirect
5454
github.com/prometheus/procfs v0.6.0 // indirect

0 commit comments

Comments
 (0)