add version command and add build information to metrics

Signed-off-by: David Christofas <dchristofas@owncloud.com>
This commit is contained in:
David Christofas
2020-09-25 16:51:43 +02:00
parent cae6a1c3ac
commit e77aea4c44
12 changed files with 116 additions and 10 deletions
+2
View File
@@ -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),
},
}
+2
View File
@@ -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
View 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
},
}
}
+9 -3
View File
@@ -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.
+27 -1
View File
@@ -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,
},
}
}
+13
View File
@@ -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
}
+2 -3
View File
@@ -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),
+3 -2
View File
@@ -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),
+1 -1
View File
@@ -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,
}