Skip to content

Commit

Permalink
basic prometheus plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Mar 24, 2023
1 parent dde0f08 commit 510c330
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 4 deletions.
37 changes: 36 additions & 1 deletion cli/brook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/txthinking/brook/plugins/dialwithip"
"github.com/txthinking/brook/plugins/logger"
"github.com/txthinking/brook/plugins/pprof"
"github.com/txthinking/brook/plugins/prometheus"
"github.com/txthinking/brook/plugins/socks5dial"
"github.com/txthinking/brook/plugins/thedns"
"github.com/txthinking/brook/plugins/tproxy"
Expand All @@ -54,7 +55,7 @@ func main() {
df := func() {}
app := cli.NewApp()
app.Name = "Brook"
app.Version = "20230401"
app.Version = "20230404"
app.Usage = "A cross-platform network tool designed for developers"
app.Authors = []*cli.Author{
{
Expand Down Expand Up @@ -107,6 +108,14 @@ func main() {
Value: 60,
Usage: "time (s)",
},
&cli.StringFlag{
Name: "prometheus",
Usage: "prometheus http listen addr, such as :7070. If it is transmitted on the public network, it is recommended to use it with nico",
},
&cli.StringFlag{
Name: "prometheusPath",
Usage: "prometheus http path, such as /xxx. If it is transmitted on the public network, a hard-to-guess value is recommended",
},
}
app.Before = func(c *cli.Context) error {
if c.String("pprof") != "" {
Expand Down Expand Up @@ -163,6 +172,32 @@ func main() {
}
p.TouchBrook()
}
if c.String("prometheus") != "" {
if c.String("prometheusPath") == "" {
return errors.New("You forgot the --prometheusPath")
}
var m map[string]string
if len(c.StringSlice("tag")) > 0 {
m = make(map[string]string)
for _, v := range c.StringSlice("tag") {
l := strings.Split(v, ":")
if len(l) != 2 {
return errors.New("Invalid tag " + v)
}
m[l[0]] = l[1]
}
}
p := prometheus.NewPrometheus(c.String("prometheus"), c.String("prometheusPath"), m)
p.TouchBrook()
g.Add(&runnergroup.Runner{
Start: func() error {
return p.ListenAndServe()
},
Stop: func() error {
return p.Shutdown()
},
})
}
return nil
}
app.Commands = []*cli.Command{
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/miekg/dns v1.1.51
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/phuslu/iploc v1.0.20230201
github.com/prometheus/client_golang v1.14.0
github.com/quic-go/quic-go v0.32.0
github.com/tdewolff/minify v2.3.6+incompatible
github.com/txthinking/crypto v0.0.0-20210716135230-de9624a415a4
Expand All @@ -23,11 +24,18 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
Expand All @@ -39,4 +47,5 @@ require (
golang.org/x/mod v0.7.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.3.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading

0 comments on commit 510c330

Please sign in to comment.