mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2025-12-31 01:10:20 -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/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
|
||||
|
||||
6
settings/changelog/unreleased/add-version-command.md
Normal file
6
settings/changelog/unreleased/add-version-command.md
Normal file
@@ -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
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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,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
settings/pkg/command/version.go
Normal file
45
settings/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/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
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user