From 4ae2793781af905d2d7100b603f0067d96a42872 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 17 Nov 2024 19:33:05 +0300 Subject: [PATCH] add multi-user mode closes #3 Signed-off-by: Vasiliy Tolstov --- cmd/servicechecker/main.go | 51 +++++++++++++++++++++++++++++++------- go.mod | 2 +- go.sum | 4 +-- pkg/config/config.go | 2 ++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/cmd/servicechecker/main.go b/cmd/servicechecker/main.go index 4cd349b..e47b416 100644 --- a/cmd/servicechecker/main.go +++ b/cmd/servicechecker/main.go @@ -36,19 +36,31 @@ import ( "google.golang.org/protobuf/types/dynamicpb" ) -var clients = make(map[string]client.Client) +var ( + clients = make(map[string]client.Client) + meters = make(map[string]meter.Meter) +) func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) defer stop() - l := slog.NewLogger() - l.Init() m := victoriametrics.NewMeter( victoriametrics.PrometheusCompat(true), - meter.WriteFDMetrics(true), - meter.WriteProcessMetrics(true), + // meter.WriteFDMetrics(true), + // meter.WriteProcessMetrics(true), ) + + l := slog.NewLogger(logger.WithMeter(m)) + if err := l.Init(); err != nil { + l.Fatal(ctx, "failed to init logger", err) + } + + if err := m.Init(); err != nil { + l.Fatal(ctx, "failed to init meter", err) + } + + meters["default"] = m f, err := os.Open("config.yaml") if err != nil { l.Fatal(ctx, "failed to open config", err) @@ -89,8 +101,16 @@ func main() { if err := healthhandler.RegisterHealthServiceServer(s, healthhandler.NewHandler()); err != nil { l.Fatal(ctx, "failed to set http handler", err) } - if err := meterhandler.RegisterMeterServiceServer(s, meterhandler.NewHandler(meterhandler.Meter(m))); err != nil { - l.Fatal(ctx, "failed to set http handler", err) + + if !cfg.App.MultiUser { + if err := meterhandler.RegisterMeterServiceServer(s, meterhandler.NewHandler(meterhandler.Meter(m))); err != nil { + l.Fatal(ctx, "failed to set http handler", err) + } + } else { + hdlr := &metricsHandler{h: meterhandler.NewHandler()} + if err := meterhandler.RegisterMeterServiceServer(s, hdlr); err != nil { + l.Fatal(ctx, "failed to set http handler", err) + } } if err = s.Start(); err != nil { @@ -129,6 +149,19 @@ func main() { if !check.Active { continue } + + var mtr meter.Meter + if !cfg.App.MultiUser { + mtr = meters["default"] + } else { + if v, ok := meters[check.User]; ok && v != nil { + mtr = v + } else { + mtr = m.Set() + meters[check.User] = mtr + } + } + for _, task := range check.Tasks { l.Info(ctx, fmt.Sprintf("task %#+v", task)) if !task.Active { @@ -140,9 +173,9 @@ func main() { switch { case task.GRPC != nil: - fn, args, err = newGRPCTask(ctx, l, m, check.Name, task) + fn, args, err = newGRPCTask(ctx, l, mtr, check.Name, task) case task.HTTP != nil: - fn, args, err = newHTTPTask(ctx, l, m, check.Name, task) + fn, args, err = newHTTPTask(ctx, l, mtr, check.Name, task) } if err != nil { l.Error(ctx, "failed to create task", err) diff --git a/go.mod b/go.mod index 8f318bb..8b63217 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( go.unistack.org/micro-codec-yaml/v3 v3.10.2 go.unistack.org/micro-meter-victoriametrics/v3 v3.8.9 go.unistack.org/micro-proto/v3 v3.4.1 - go.unistack.org/micro-server-http/v3 v3.11.33 + go.unistack.org/micro-server-http/v3 v3.11.34 go.unistack.org/micro/v3 v3.10.100 google.golang.org/protobuf v1.35.2 ) diff --git a/go.sum b/go.sum index 25f5a47..4082fbf 100644 --- a/go.sum +++ b/go.sum @@ -912,8 +912,8 @@ go.unistack.org/micro-meter-victoriametrics/v3 v3.8.9 h1:ZXCS0eFiSdvcFYxpxV2Q77g go.unistack.org/micro-meter-victoriametrics/v3 v3.8.9/go.mod h1:xODJQ0Nu/F8k34D/z2ITL91OskI/C674XCkugAxmc3Q= go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= -go.unistack.org/micro-server-http/v3 v3.11.33 h1:kWiMVeKDlAcXkijBQ7YJKNJPjoM9gI6JI0W2/HMoM/Y= -go.unistack.org/micro-server-http/v3 v3.11.33/go.mod h1:3hlTQj3e6HQziJa2Coz/BwvVbt3oRejOevyAPUCuBEk= +go.unistack.org/micro-server-http/v3 v3.11.34 h1:Cu44/IWyTBa0I1LIt8TZu/z130uDs2t/OZJwV5H5NX0= +go.unistack.org/micro-server-http/v3 v3.11.34/go.mod h1:3hlTQj3e6HQziJa2Coz/BwvVbt3oRejOevyAPUCuBEk= go.unistack.org/micro/v3 v3.10.94/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= go.unistack.org/micro/v3 v3.10.100 h1:yWOaU0ImCGm5k5MUzlIobJUOr+KLfrR/BoDZvcHyKxM= go.unistack.org/micro/v3 v3.10.100/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g= diff --git a/pkg/config/config.go b/pkg/config/config.go index 7acb673..a1c9007 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,6 +17,7 @@ type MeterConfig struct { } type Config struct { + App *AppConfig `json:"app,omitempty" yaml:"app,omitempty"` Meter *MeterConfig `json:"meter,omitempty" yaml:"meter,omitempty"` Checks []*CheckConfig `json:"checks,omitempty" yaml:"checks,omitempty"` } @@ -27,6 +28,7 @@ type CheckConfig struct { Timeout mtime.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"` Interval mtime.Duration `json:"interval,omitempty" yaml:"interval,omitempty"` Active bool `json:"active,omitempty" yaml:"active,omitempty"` + User string `json:"user,omitempty" yaml:"user,omitempty"` } type HTTPConfig struct {