Skip to content

Commit

Permalink
[rk-prom] Support global initializer [commit:dongxuny]
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxuny committed Nov 23, 2020
1 parent 1892e54 commit bd375d3
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 14 deletions.
21 changes: 15 additions & 6 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rookie-ninja/rk-common/context"
rk_entry "github.com/rookie-ninja/rk-common/entry"
"github.com/rookie-ninja/rk-query"
"go.uber.org/zap"
"io/ioutil"
Expand All @@ -32,6 +33,10 @@ var (

const PromEntryNameDefault = "rk-prom"

func init() {
rk_ctx.RegisterEntryInitializer(NewPromEntries)
}

type bootConfig struct {
Prom struct {
Path string `yaml:"path"`
Expand Down Expand Up @@ -111,20 +116,21 @@ func WithPusherURL(url string) PromEntryOption {
}
}

func NewPromEntryWithConfig(path string, factory *rk_query.EventFactory, logger *zap.Logger) *PromEntry {
func NewPromEntries(path string, factory *rk_query.EventFactory, logger *zap.Logger) map[string]rk_entry.Entry {
bytes := readFile(path)
config := &bootConfig{}
if err := yaml.Unmarshal(bytes, config); err != nil {
shutdownWithError(err)
return nil
}

return getPromServerEntry(config, factory, logger)
return getPromServerEntries(config, factory, logger)
}

func getPromServerEntry(config *bootConfig, factory *rk_query.EventFactory, logger *zap.Logger) *PromEntry {
func getPromServerEntries(config *bootConfig, factory *rk_query.EventFactory, logger *zap.Logger) map[string]rk_entry.Entry {
res := make(map[string]rk_entry.Entry)
if config.Prom.Enabled {
return NewPromEntry(
entry := NewPromEntry(
WithPort(config.Prom.Port),
WithPath(config.Prom.Path),
WithLogger(logger),
Expand All @@ -133,23 +139,26 @@ func getPromServerEntry(config *bootConfig, factory *rk_query.EventFactory, logg
WithPusherInterval(config.Prom.Pusher.Interval),
WithPusherJob(config.Prom.Pusher.Job),
WithPusherURL(config.Prom.Pusher.URL))
res[entry.GetName()] = entry
}

return nil
return res
}

func NewPromEntry(opts ...PromEntryOption) *PromEntry {
entry := &PromEntry{
port: defaultPort,
path: defaultPath,
name: "rk-prom",
name: PromEntryNameDefault,
entryType: "prom",
}

for i := range opts {
opts[i](entry)
}

rk_ctx.GlobalAppCtx.AddEntry(entry.GetName(), entry)

return entry
}

Expand Down
3 changes: 2 additions & 1 deletion example/boot/boot_from_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

func main() {
fac := rk_query.NewEventFactory()
entry := rk_prom.NewPromEntryWithConfig("example/boot/boot.yaml", fac, rk_logger.StdoutLogger)
maps := rk_prom.NewPromEntries("example/boot/boot.yaml", fac, rk_logger.StdoutLogger)
entry := maps[rk_prom.PromEntryNameDefault]
entry.Bootstrap(fac.CreateEvent())

// with custom namespace and subsystem
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/common v0.10.0
github.com/rookie-ninja/rk-common v1.0.2
github.com/prometheus/client_golang v1.8.0
github.com/prometheus/common v0.14.0
github.com/rookie-ninja/rk-common v1.0.5
github.com/rookie-ninja/rk-logger v1.0.5
github.com/rookie-ninja/rk-query v1.0.7
go.uber.org/zap v1.16.0
Expand Down
Loading

0 comments on commit bd375d3

Please sign in to comment.