add metrics http exporter

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-11-16 20:17:37 +03:00
parent 31528740dc
commit 26ddc88547
4 changed files with 1609 additions and 20 deletions

View File

@ -8,12 +8,18 @@ import (
"time"
grpccli "go.unistack.org/micro-client-grpc/v3"
jsoncodec "go.unistack.org/micro-codec-json/v3"
jsonpbcodec "go.unistack.org/micro-codec-jsonpb/v3"
protocodec "go.unistack.org/micro-codec-proto/v3"
victoriametrics "go.unistack.org/micro-meter-victoriametrics/v3"
httpsrv "go.unistack.org/micro-server-http/v3"
healthhandler "go.unistack.org/micro-server-http/v3/handler/health"
meterhandler "go.unistack.org/micro-server-http/v3/handler/meter"
"go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v3/logger/slog"
"go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/semconv"
"go.unistack.org/micro/v3/server"
"go.unistack.org/servicechecker/pkg/config"
"go.unistack.org/servicechecker/pkg/grpcconn"
"go.unistack.org/servicechecker/pkg/scheduler"
@ -29,7 +35,11 @@ func main() {
l := slog.NewLogger()
l.Init()
m := victoriametrics.NewMeter()
m := victoriametrics.NewMeter(
victoriametrics.PrometheusCompat(true),
meter.WriteFDMetrics(true),
meter.WriteProcessMetrics(true),
)
f, err := os.Open("config.yaml")
if err != nil {
l.Fatal(ctx, "failed to open config", err)
@ -58,6 +68,26 @@ func main() {
}
}()
s := httpsrv.NewServer(
server.Codec("application/json", jsoncodec.NewCodec()),
server.Address(cfg.Meter.Addr),
)
if err = s.Init(); err != nil {
l.Fatal(ctx, "http server init error", err)
}
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 err = s.Start(); err != nil {
l.Fatal(ctx, "failed to start http server", err)
}
clients := make(map[string]client.Client)
gcli := grpccli.NewClient(
client.Codec("application/json", jsonpbcodec.NewCodec()),

18
go.mod
View File

@ -6,27 +6,29 @@ require (
github.com/go-co-op/gocron/v2 v2.12.3
github.com/google/uuid v1.6.0
go.unistack.org/micro-client-grpc/v3 v3.11.10
go.unistack.org/micro-codec-json/v3 v3.10.1
go.unistack.org/micro-codec-jsonpb/v3 v3.10.3
go.unistack.org/micro-codec-proto/v3 v3.10.2
go.unistack.org/micro-codec-yaml/v3 v3.10.2
go.unistack.org/micro-meter-victoriametrics/v3 v3.8.9
go.unistack.org/micro-server-http/v3 v3.11.33
go.unistack.org/micro/v3 v3.10.100
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
google.golang.org/protobuf v1.35.2
)
require (
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
github.com/valyala/histogram v1.2.0 // indirect
go.unistack.org/metrics v0.0.1 // indirect
go.unistack.org/micro-client-http/v3 v3.9.14 // indirect
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.67.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f // indirect
google.golang.org/grpc v1.68.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

1569
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -3,11 +3,17 @@ package config
import (
"io"
yamlcodec "go.unistack.org/micro-codec-yaml/v3"
mtime "go.unistack.org/micro/v3/util/time"
yaml "gopkg.in/yaml.v3"
)
type Meter struct {
Addr string `json:"addr,omitempty" yaml:"addr,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
}
type Config struct {
Meter *Meter `json:"meter,omitempty" yaml:"meter,omitempty"`
Checks []*Check `json:"checks,omitempty" yaml:"checks,omitempty"`
}
@ -35,5 +41,5 @@ func (cfg *Config) Parse(r io.Reader) error {
return err
}
return yaml.Unmarshal(buf, cfg)
return yamlcodec.NewCodec().Unmarshal(buf, cfg)
}