From e77aea4c4408302231d901d210b160b6bccb2d30 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Fri, 25 Sep 2020 16:51:43 +0200 Subject: [PATCH] add version command and add build information to metrics Signed-off-by: David Christofas --- ocis/pkg/command/store.go | 5 +++ .../unreleased/add-version-command.md | 6 +++ store/go.sum | 1 + store/pkg/command/root.go | 2 + store/pkg/command/server.go | 2 + store/pkg/command/version.go | 45 +++++++++++++++++++ store/pkg/config/config.go | 12 +++-- store/pkg/flagset/flagset.go | 28 +++++++++++- store/pkg/metrics/metrics.go | 13 ++++++ store/pkg/server/debug/server.go | 5 +-- store/pkg/server/grpc/server.go | 5 ++- store/pkg/service/v0/service.go | 2 +- 12 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 store/changelog/unreleased/add-version-command.md create mode 100644 store/pkg/command/version.go diff --git a/ocis/pkg/command/store.go b/ocis/pkg/command/store.go index 15e84f24b..0e22742ed 100644 --- a/ocis/pkg/command/store.go +++ b/ocis/pkg/command/store.go @@ -4,6 +4,7 @@ package command import ( "github.com/micro/cli/v2" + "github.com/owncloud/ocis/ocis/pkg/version" "github.com/owncloud/ocis/store/pkg/command" svcconfig "github.com/owncloud/ocis/store/pkg/config" "github.com/owncloud/ocis/store/pkg/flagset" @@ -18,6 +19,9 @@ func StoreCommand(cfg *config.Config) *cli.Command { Usage: "Start a go-micro store", Category: "Extensions", Flags: flagset.ServerWithConfig(cfg.Store), + Subcommands: []*cli.Command{ + command.PrintVersion(cfg.Store), + }, Action: func(ctx *cli.Context) error { storeCommand := command.Server(configureStore(cfg)) @@ -34,6 +38,7 @@ func configureStore(cfg *config.Config) *svcconfig.Config { cfg.Store.Log.Level = cfg.Log.Level cfg.Store.Log.Pretty = cfg.Log.Pretty cfg.Store.Log.Color = cfg.Log.Color + cfg.Store.Service.Version = version.String if cfg.Tracing.Enabled { cfg.Store.Tracing.Enabled = cfg.Tracing.Enabled diff --git a/store/changelog/unreleased/add-version-command.md b/store/changelog/unreleased/add-version-command.md new file mode 100644 index 000000000..6c30717a0 --- /dev/null +++ b/store/changelog/unreleased/add-version-command.md @@ -0,0 +1,6 @@ +Enhancement: Add version command + +Added a command to print the versions of all running ocis-store service instances. +Also added a field in the metrics showing some build information. + +https://github.com/owncloud/product/issues/226 diff --git a/store/go.sum b/store/go.sum index ff3f773c3..6709a7703 100644 --- a/store/go.sum +++ b/store/go.sum @@ -889,6 +889,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 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= diff --git a/store/pkg/command/root.go b/store/pkg/command/root.go index d85c76963..83d1e40a6 100644 --- a/store/pkg/command/root.go +++ b/store/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/store/pkg/command/server.go b/store/pkg/command/server.go index b08473286..261c6dc06 100644 --- a/store/pkg/command/server.go +++ b/store/pkg/command/server.go @@ -129,6 +129,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() + metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1) + { server := grpc.Server( grpc.Logger(logger), diff --git a/store/pkg/command/version.go b/store/pkg/command/version.go new file mode 100644 index 000000000..0e90015aa --- /dev/null +++ b/store/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/store/pkg/config" + "github.com/owncloud/ocis/store/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.ListStoreWithConfig(cfg), + Action: func(c *cli.Context) error { + reg := mdns.NewRegistry() + services, err := reg.GetService(cfg.Service.Namespace + "." + cfg.Service.Name) + if err != nil { + fmt.Println(fmt.Errorf("could not get store services from the registry: %v", err)) + return err + } + + if len(services) == 0 { + fmt.Println("No running store 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/store/pkg/config/config.go b/store/pkg/config/config.go index eb21fac6f..30ca9ffdb 100644 --- a/store/pkg/config/config.go +++ b/store/pkg/config/config.go @@ -17,9 +17,15 @@ type Debug struct { // GRPC defines the available grpc configuration. type GRPC struct { - Addr string + Addr string + Root string +} + +// Service defines the available service configuration. +type Service struct { + Name string Namespace string - Root string + Version string } // Tracing defines the available tracing configuration. @@ -39,7 +45,7 @@ type Config struct { GRPC GRPC Tracing Tracing Datapath string - Name string + Service Service } // New initializes a new configuration with or without defaults. diff --git a/store/pkg/flagset/flagset.go b/store/pkg/flagset/flagset.go index 2f81e419f..b731319da 100644 --- a/store/pkg/flagset/flagset.go +++ b/store/pkg/flagset/flagset.go @@ -120,7 +120,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { Value: "com.owncloud.api", Usage: "Set the base namespace for the grpc namespace", EnvVars: []string{"STORE_GRPC_NAMESPACE"}, - Destination: &cfg.GRPC.Namespace, + Destination: &cfg.Service.Namespace, + }, + &cli.StringFlag{ + Name: "name", + Value: "store", + Usage: "Service name", + EnvVars: []string{"STORE_NAME"}, + Destination: &cfg.Service.Name, }, &cli.StringFlag{ Name: "data-path", @@ -131,3 +138,22 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, } } + +// ListStoreWithConfig applies the config to the list commands flags. +func ListStoreWithConfig(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{"STORE_GRPC_NAMESPACE"}, + Destination: &cfg.Service.Namespace, + }, + &cli.StringFlag{ + Name: "name", + Value: "store", + Usage: "Service name", + EnvVars: []string{"STORE_NAME"}, + Destination: &cfg.Service.Name, + }, + } +} diff --git a/store/pkg/metrics/metrics.go b/store/pkg/metrics/metrics.go index 0f5f77cac..67c46e77f 100644 --- a/store/pkg/metrics/metrics.go +++ b/store/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/store/pkg/server/debug/server.go b/store/pkg/server/debug/server.go index 9ca21c53c..e3812b25d 100644 --- a/store/pkg/server/debug/server.go +++ b/store/pkg/server/debug/server.go @@ -6,7 +6,6 @@ import ( "github.com/owncloud/ocis/ocis-pkg/service/debug" "github.com/owncloud/ocis/store/pkg/config" - "github.com/owncloud/ocis/store/pkg/version" ) // Server initializes the debug service and server. @@ -15,8 +14,8 @@ func Server(opts ...Option) (*http.Server, error) { return debug.NewService( debug.Logger(options.Logger), - debug.Name("store"), - debug.Version(version.String), + debug.Name(options.Config.Service.Name), + debug.Version(options.Config.Service.Version), debug.Address(options.Config.Debug.Addr), debug.Token(options.Config.Debug.Token), debug.Pprof(options.Config.Debug.Pprof), diff --git a/store/pkg/server/grpc/server.go b/store/pkg/server/grpc/server.go index 8e230665a..543b9566c 100644 --- a/store/pkg/server/grpc/server.go +++ b/store/pkg/server/grpc/server.go @@ -11,8 +11,9 @@ func Server(opts ...Option) grpc.Service { options := newOptions(opts...) service := grpc.NewService( - grpc.Namespace("com.owncloud.api"), - grpc.Name("store"), + grpc.Namespace(options.Config.Service.Namespace), + grpc.Name(options.Config.Service.Name), + grpc.Version(options.Config.Service.Version), grpc.Context(options.Context), grpc.Address(options.Config.GRPC.Addr), grpc.Logger(options.Logger), diff --git a/store/pkg/service/v0/service.go b/store/pkg/service/v0/service.go index 501cd9a27..c773cc66c 100644 --- a/store/pkg/service/v0/service.go +++ b/store/pkg/service/v0/service.go @@ -49,7 +49,7 @@ func New(opts ...Option) (s *Service, err error) { indexMapping.DefaultAnalyzer = keyword.Name s = &Service{ - id: cfg.GRPC.Namespace + "." + "store", + id: cfg.Service.Namespace + "." + cfg.Service.Name, log: logger, Config: cfg, }