add multi-user mode closes #3

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-11-17 19:33:05 +03:00
parent 4d7bfa713a
commit 4ae2793781
4 changed files with 47 additions and 12 deletions

View File

@ -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)

2
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=

View File

@ -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 {