mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-04 11:19:39 -06:00
add version command and add build information to metrics
Signed-off-by: David Christofas <dchristofas@owncloud.com>
This commit is contained in:
@@ -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
|
||||
|
||||
6
store/changelog/unreleased/add-version-command.md
Normal file
6
store/changelog/unreleased/add-version-command.md
Normal file
@@ -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
|
||||
@@ -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=
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
45
store/pkg/command/version.go
Normal file
45
store/pkg/command/version.go
Normal file
@@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user