From 116ba347e9a7f47d4d65a667c612ac26440aa7fd Mon Sep 17 00:00:00 2001 From: David Christofas Date: Fri, 25 Sep 2020 16:41:02 +0200 Subject: [PATCH] add version command and add build information to metrics Signed-off-by: David Christofas --- ocis/pkg/command/settings.go | 5 +++ .../unreleased/add-version-command.md | 6 +++ settings/go.mod | 6 ++- settings/go.sum | 2 + settings/pkg/command/root.go | 2 + settings/pkg/command/server.go | 10 ++++- settings/pkg/command/version.go | 45 +++++++++++++++++++ settings/pkg/config/config.go | 14 +++--- settings/pkg/flagset/flagset.go | 29 +++++++++++- settings/pkg/metrics/metrics.go | 13 ++++++ .../pkg/proto/v0/settings.pb.micro_test.go | 2 +- settings/pkg/server/grpc/server.go | 3 +- settings/pkg/server/http/server.go | 2 +- settings/pkg/store/filesystem/store.go | 10 ++--- 14 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 settings/changelog/unreleased/add-version-command.md create mode 100644 settings/pkg/command/version.go diff --git a/ocis/pkg/command/settings.go b/ocis/pkg/command/settings.go index 44b5c87a3..38944a687 100644 --- a/ocis/pkg/command/settings.go +++ b/ocis/pkg/command/settings.go @@ -4,6 +4,7 @@ package command import ( "github.com/micro/cli/v2" + "github.com/owncloud/ocis/ocis/pkg/version" "github.com/owncloud/ocis/settings/pkg/command" svcconfig "github.com/owncloud/ocis/settings/pkg/config" "github.com/owncloud/ocis/settings/pkg/flagset" @@ -18,6 +19,9 @@ func SettingsCommand(cfg *config.Config) *cli.Command { Usage: "Start settings server", Category: "Extensions", Flags: flagset.ServerWithConfig(cfg.Settings), + Subcommands: []*cli.Command{ + command.PrintVersion(cfg.Settings), + }, Action: func(ctx *cli.Context) error { settingsCommand := command.Server(configureSettings(cfg)) @@ -34,6 +38,7 @@ func configureSettings(cfg *config.Config) *svcconfig.Config { cfg.Settings.Log.Level = cfg.Log.Level cfg.Settings.Log.Pretty = cfg.Log.Pretty cfg.Settings.Log.Color = cfg.Log.Color + cfg.Settings.Service.Version = version.String if cfg.Tracing.Enabled { cfg.Settings.Tracing.Enabled = cfg.Tracing.Enabled diff --git a/settings/changelog/unreleased/add-version-command.md b/settings/changelog/unreleased/add-version-command.md new file mode 100644 index 000000000..5d0abc3c8 --- /dev/null +++ b/settings/changelog/unreleased/add-version-command.md @@ -0,0 +1,6 @@ +Enhancement: add version command + +Added a version command to print the versions of all running ocis-settings service instances. +Also added build information to the metrics. + +https://github.com/owncloud/product/issues/226 diff --git a/settings/go.mod b/settings/go.mod index e5e4efb4e..28e09b06a 100644 --- a/settings/go.mod +++ b/settings/go.mod @@ -17,8 +17,10 @@ require ( github.com/micro/go-micro/v2 v2.9.1 github.com/mitchellh/gox v1.0.1 github.com/oklog/run v1.0.0 + github.com/olekukonko/tablewriter v0.0.1 github.com/openzipkin/zipkin-go v0.2.2 - github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee // indirect + github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee + github.com/prometheus/client_golang v1.7.1 github.com/restic/calens v0.2.0 github.com/spf13/viper v1.6.3 github.com/stretchr/testify v1.6.1 @@ -31,6 +33,6 @@ require ( ) replace ( - google.golang.org/grpc => google.golang.org/grpc v1.26.0 github.com/owncloud/ocis/ocis-pkg => ../ocis-pkg + google.golang.org/grpc => google.golang.org/grpc v1.26.0 ) diff --git a/settings/go.sum b/settings/go.sum index 0341423cc..9f826563f 100644 --- a/settings/go.sum +++ b/settings/go.sum @@ -728,6 +728,7 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w= +github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -775,6 +776,7 @@ github.com/owncloud/ocis-settings v0.3.2-0.20200828091056-47af10a0e872 h1:VGWM/e github.com/owncloud/ocis-settings v0.3.2-0.20200828091056-47af10a0e872/go.mod h1:vRge9QDkOsc6j76gPBmZs1Z5uOPrV4DIkZCgZCEFwBA= github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee h1:P/bd6+zVU+I+X1+VfSg9av0OD2bfw1tUSHQtWj1JX/I= github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee/go.mod h1:kukd3chd0ARpGNSLcIutgFywSvWIIdQcpyhPtVTyZbM= +github.com/owncloud/ocis/settings v0.0.0-20200918114005-1a0ddd2190ee/go.mod h1:5w91idmyOd8LgYK3eGuqsFBOfVJnSDeEp7S6dHheW14= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE= diff --git a/settings/pkg/command/root.go b/settings/pkg/command/root.go index 25c2edb11..58e8f43eb 100644 --- a/settings/pkg/command/root.go +++ b/settings/pkg/command/root.go @@ -32,12 +32,14 @@ func Execute() error { Flags: flagset.RootWithConfig(cfg), Before: func(c *cli.Context) error { + cfg.Service.Version = version.String return ParseConfig(c, cfg) }, Commands: []*cli.Command{ Server(cfg), Health(cfg), + PrintVersion(cfg), }, } diff --git a/settings/pkg/command/server.go b/settings/pkg/command/server.go index f3b661aae..f08b73409 100644 --- a/settings/pkg/command/server.go +++ b/settings/pkg/command/server.go @@ -2,6 +2,7 @@ package command import ( "context" + "github.com/owncloud/ocis/settings/pkg/metrics" "os" "os/signal" "strings" @@ -128,16 +129,20 @@ func Server(cfg *config.Config) *cli.Command { var ( gr = run.Group{} ctx, cancel = context.WithCancel(context.Background()) + mtrcs = metrics.New() ) defer cancel() + mtrcs.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1) + { server := http.Server( - http.Name("settings"), + http.Name(cfg.Service.Name), http.Logger(logger), http.Context(ctx), http.Config(cfg), + http.Metrics(mtrcs), http.Flags(flagset.RootWithConfig(cfg)), http.Flags(flagset.ServerWithConfig(cfg)), ) @@ -153,10 +158,11 @@ func Server(cfg *config.Config) *cli.Command { { server := grpc.Server( - grpc.Name("settings"), + grpc.Name(cfg.Service.Name), grpc.Logger(logger), grpc.Context(ctx), grpc.Config(cfg), + grpc.Metrics(mtrcs), ) gr.Add(server.Run, func(_ error) { diff --git a/settings/pkg/command/version.go b/settings/pkg/command/version.go new file mode 100644 index 000000000..f5a992669 --- /dev/null +++ b/settings/pkg/command/version.go @@ -0,0 +1,45 @@ +package command + +import ( + "fmt" + "os" + + "github.com/micro/cli/v2" + "github.com/micro/go-micro/v2/registry/mdns" + tw "github.com/olekukonko/tablewriter" + "github.com/owncloud/ocis/settings/pkg/config" + "github.com/owncloud/ocis/settings/pkg/flagset" +) + +// PrintVersion prints the service versions of all running instances. +func PrintVersion(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "Print the versions of the running instances", + Flags: flagset.ListSettingsWithConfig(cfg), + Action: func(c *cli.Context) error { + reg := mdns.NewRegistry() + services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Service.Name) + if err != nil { + fmt.Println(fmt.Errorf("could not get settings services from the registry: %v", err)) + return err + } + + if len(services) == 0 { + fmt.Println("No running settings service found.") + return nil + } + + table := tw.NewWriter(os.Stdout) + table.SetHeader([]string{"Version", "Address", "Id"}) + table.SetAutoFormatHeaders(false) + for _, s := range services { + for _, n := range s.Nodes { + table.Append([]string{s.Version, n.Address, n.Id}) + } + } + table.Render() + return nil + }, + } +} diff --git a/settings/pkg/config/config.go b/settings/pkg/config/config.go index dd18bcc23..c0fd101c1 100644 --- a/settings/pkg/config/config.go +++ b/settings/pkg/config/config.go @@ -28,6 +28,13 @@ type GRPC struct { Namespace string } +// Service provides configuration options for the service +type Service struct { + Name string + Version string + DataPath string +} + // Tracing defines the available tracing configuration. type Tracing struct { Enabled bool @@ -42,11 +49,6 @@ type Asset struct { Path string } -// Storage defines the available storage configuration. -type Storage struct { - DataPath string -} - // TokenManager is the config for using the reva token manager type TokenManager struct { JWTSecret string @@ -55,7 +57,7 @@ type TokenManager struct { // Config combines all available configuration parts. type Config struct { File string - Storage Storage + Service Service Log Log Debug Debug HTTP HTTP diff --git a/settings/pkg/flagset/flagset.go b/settings/pkg/flagset/flagset.go index 5b9b09a93..2f3e4f0fc 100644 --- a/settings/pkg/flagset/flagset.go +++ b/settings/pkg/flagset/flagset.go @@ -157,12 +157,19 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"}, Destination: &cfg.GRPC.Namespace, }, + &cli.StringFlag{ + Name: "name", + Value: "settings", + Usage: "service name", + EnvVars: []string{"SETTINGS_NAME"}, + Destination: &cfg.Service.Name, + }, &cli.StringFlag{ Name: "data-path", Value: "/var/tmp/ocis-settings", Usage: "Mount path for the storage", EnvVars: []string{"SETTINGS_DATA_PATH"}, - Destination: &cfg.Storage.DataPath, + Destination: &cfg.Service.DataPath, }, &cli.StringFlag{ Name: "jwt-secret", @@ -173,3 +180,23 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, } } + +// ListSettingsWithConfig applies list command flags to cfg +func ListSettingsWithConfig(cfg *config.Config) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "grpc-namespace", + Value: "com.owncloud.api", + Usage: "Set the base namespace for the grpc namespace", + EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"}, + Destination: &cfg.GRPC.Namespace, + }, + &cli.StringFlag{ + Name: "name", + Value: "settings", + Usage: "service name", + EnvVars: []string{"SETTINGS_NAME"}, + Destination: &cfg.Service.Name, + }, + } +} diff --git a/settings/pkg/metrics/metrics.go b/settings/pkg/metrics/metrics.go index 81a2827f4..adb6000a5 100644 --- a/settings/pkg/metrics/metrics.go +++ b/settings/pkg/metrics/metrics.go @@ -1,5 +1,7 @@ package metrics +import "github.com/prometheus/client_golang/prometheus" + var ( // Namespace defines the namespace for the defines metrics. Namespace = "ocis" @@ -11,6 +13,7 @@ var ( // Metrics defines the available metrics of this service. type Metrics struct { // Counter *prometheus.CounterVec + BuildInfo *prometheus.GaugeVec } // New initializes the available metrics. @@ -22,11 +25,21 @@ func New() *Metrics { // Name: "greet_total", // Help: "How many greeting requests processed", // }, []string{}), + BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: Namespace, + Subsystem: Subsystem, + Name: "build_info", + Help: "Build information", + }, []string{"version"}), } // prometheus.Register( // m.Counter, // ) + _ = prometheus.Register( + m.BuildInfo, + ) + return m } diff --git a/settings/pkg/proto/v0/settings.pb.micro_test.go b/settings/pkg/proto/v0/settings.pb.micro_test.go index 7249fa8e4..6f76d468f 100644 --- a/settings/pkg/proto/v0/settings.pb.micro_test.go +++ b/settings/pkg/proto/v0/settings.pb.micro_test.go @@ -177,7 +177,7 @@ func init() { ) cfg := config.New() - cfg.Storage.DataPath = dataPath + cfg.Service.DataPath = dataPath handler = svc.NewService(cfg, ocislog.NewLogger(ocislog.Color(true), ocislog.Pretty(true))) err := proto.RegisterBundleServiceHandler(service.Server(), handler) if err != nil { diff --git a/settings/pkg/server/grpc/server.go b/settings/pkg/server/grpc/server.go index e5e751ed6..6fbfedb83 100644 --- a/settings/pkg/server/grpc/server.go +++ b/settings/pkg/server/grpc/server.go @@ -4,7 +4,6 @@ import ( "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/settings/pkg/proto/v0" svc "github.com/owncloud/ocis/settings/pkg/service/v0" - "github.com/owncloud/ocis/settings/pkg/version" ) // Server initializes a new go-micro service ready to run @@ -14,7 +13,7 @@ func Server(opts ...Option) grpc.Service { service := grpc.NewService( grpc.Logger(options.Logger), grpc.Name(options.Name), - grpc.Version(version.String), + grpc.Version(options.Config.Service.Version), grpc.Address(options.Config.GRPC.Addr), grpc.Namespace(options.Config.GRPC.Namespace), grpc.Context(options.Context), diff --git a/settings/pkg/server/http/server.go b/settings/pkg/server/http/server.go index 0e87f69d3..cfab78fda 100644 --- a/settings/pkg/server/http/server.go +++ b/settings/pkg/server/http/server.go @@ -18,7 +18,7 @@ func Server(opts ...Option) http.Service { service := http.NewService( http.Logger(options.Logger), http.Name(options.Name), - http.Version(version.String), + http.Version(options.Config.Service.Version), http.Address(options.Config.HTTP.Addr), http.Namespace(options.Config.HTTP.Namespace), http.Context(options.Context), diff --git a/settings/pkg/store/filesystem/store.go b/settings/pkg/store/filesystem/store.go index ddccbe734..cf2046b05 100644 --- a/settings/pkg/store/filesystem/store.go +++ b/settings/pkg/store/filesystem/store.go @@ -31,16 +31,16 @@ func New(cfg *config.Config) settings.Manager { ), } - if _, err := os.Stat(cfg.Storage.DataPath); err != nil { - s.Logger.Info().Msgf("creating container on %v", cfg.Storage.DataPath) - err := os.MkdirAll(cfg.Storage.DataPath, 0700) + if _, err := os.Stat(cfg.Service.DataPath); err != nil { + s.Logger.Info().Msgf("creating container on %v", cfg.Service.DataPath) + err := os.MkdirAll(cfg.Service.DataPath, 0700) if err != nil { - s.Logger.Err(err).Msgf("providing container on %v", cfg.Storage.DataPath) + s.Logger.Err(err).Msgf("providing container on %v", cfg.Service.DataPath) } } - s.dataPath = cfg.Storage.DataPath + s.dataPath = cfg.Service.DataPath return &s }