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:41:02 +02:00
parent cae6a1c3ac
commit 116ba347e9
14 changed files with 129 additions and 20 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),
},
}
+8 -2
View File
@@ -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) {
+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/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
},
}
}