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 (
|
import (
|
||||||
"github.com/micro/cli/v2"
|
"github.com/micro/cli/v2"
|
||||||
|
"github.com/owncloud/ocis/ocis/pkg/version"
|
||||||
"github.com/owncloud/ocis/ocs/pkg/command"
|
"github.com/owncloud/ocis/ocs/pkg/command"
|
||||||
svcconfig "github.com/owncloud/ocis/ocs/pkg/config"
|
svcconfig "github.com/owncloud/ocis/ocs/pkg/config"
|
||||||
"github.com/owncloud/ocis/ocs/pkg/flagset"
|
"github.com/owncloud/ocis/ocs/pkg/flagset"
|
||||||
@@ -18,6 +19,9 @@ func OCSCommand(cfg *config.Config) *cli.Command {
|
|||||||
Usage: "Start ocs server",
|
Usage: "Start ocs server",
|
||||||
Category: "Extensions",
|
Category: "Extensions",
|
||||||
Flags: flagset.ServerWithConfig(cfg.OCS),
|
Flags: flagset.ServerWithConfig(cfg.OCS),
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
command.PrintVersion(cfg.OCS),
|
||||||
|
},
|
||||||
Action: func(ctx *cli.Context) error {
|
Action: func(ctx *cli.Context) error {
|
||||||
ocsCommand := command.Server(configureOCS(cfg))
|
ocsCommand := command.Server(configureOCS(cfg))
|
||||||
|
|
||||||
@@ -34,6 +38,7 @@ func configureOCS(cfg *config.Config) *svcconfig.Config {
|
|||||||
cfg.OCS.Log.Level = cfg.Log.Level
|
cfg.OCS.Log.Level = cfg.Log.Level
|
||||||
cfg.OCS.Log.Pretty = cfg.Log.Pretty
|
cfg.OCS.Log.Pretty = cfg.Log.Pretty
|
||||||
cfg.OCS.Log.Color = cfg.Log.Color
|
cfg.OCS.Log.Color = cfg.Log.Color
|
||||||
|
cfg.OCS.Service.Version = version.String
|
||||||
|
|
||||||
if cfg.Tracing.Enabled {
|
if cfg.Tracing.Enabled {
|
||||||
cfg.OCS.Tracing.Enabled = cfg.Tracing.Enabled
|
cfg.OCS.Tracing.Enabled = cfg.Tracing.Enabled
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
Enhancement: Add version command
|
||||||
|
|
||||||
|
Added a command to list the currently running services with their respective version.
|
||||||
|
Also added a metrics entry for build information which includes the service version.
|
||||||
|
|
||||||
|
https://github.com/owncloud/product/issues/226
|
||||||
@@ -32,12 +32,14 @@ func Execute() error {
|
|||||||
Flags: flagset.RootWithConfig(cfg),
|
Flags: flagset.RootWithConfig(cfg),
|
||||||
|
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
|
cfg.Service.Version = version.String
|
||||||
return ParseConfig(c, cfg)
|
return ParseConfig(c, cfg)
|
||||||
},
|
},
|
||||||
|
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
Server(cfg),
|
Server(cfg),
|
||||||
Health(cfg),
|
Health(cfg),
|
||||||
|
PrintVersion(cfg),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,6 +135,8 @@ func Server(cfg *config.Config) *cli.Command {
|
|||||||
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
|
||||||
|
|
||||||
{
|
{
|
||||||
server, err := http.Server(
|
server, err := http.Server(
|
||||||
http.Logger(logger),
|
http.Logger(logger),
|
||||||
|
|||||||
@@ -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/ocs/pkg/config"
|
||||||
|
"github.com/owncloud/ocis/ocs/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.ListOcsWithConfig(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 ocs services from the registry: %v", err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(services) == 0 {
|
||||||
|
fmt.Println("No running ocs 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 {
|
|||||||
|
|
||||||
// HTTP defines the available http configuration.
|
// HTTP defines the available http configuration.
|
||||||
type HTTP struct {
|
type HTTP struct {
|
||||||
Addr string
|
Addr string
|
||||||
|
Root string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service defines the available service configuration.
|
||||||
|
type Service struct {
|
||||||
|
Name string
|
||||||
Namespace string
|
Namespace string
|
||||||
Root string
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tracing defines the available tracing configuration.
|
// Tracing defines the available tracing configuration.
|
||||||
@@ -44,6 +50,7 @@ type Config struct {
|
|||||||
HTTP HTTP
|
HTTP HTTP
|
||||||
Tracing Tracing
|
Tracing Tracing
|
||||||
TokenManager TokenManager
|
TokenManager TokenManager
|
||||||
|
Service Service
|
||||||
}
|
}
|
||||||
|
|
||||||
// New initializes a new configuration with or without defaults.
|
// New initializes a new configuration with or without defaults.
|
||||||
|
|||||||
@@ -128,7 +128,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
|||||||
Value: "com.owncloud.web",
|
Value: "com.owncloud.web",
|
||||||
Usage: "Set the base namespace for the http namespace",
|
Usage: "Set the base namespace for the http namespace",
|
||||||
EnvVars: []string{"OCS_NAMESPACE"},
|
EnvVars: []string{"OCS_NAMESPACE"},
|
||||||
Destination: &cfg.HTTP.Namespace,
|
Destination: &cfg.Service.Namespace,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "name",
|
||||||
|
Value: "ocs",
|
||||||
|
Usage: "Service name",
|
||||||
|
EnvVars: []string{"OCS_NAME"},
|
||||||
|
Destination: &cfg.Service.Name,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "http-root",
|
Name: "http-root",
|
||||||
@@ -147,3 +154,23 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListOcsWithConfig applies the config to the list commands flagset.
|
||||||
|
func ListOcsWithConfig(cfg *config.Config) []cli.Flag {
|
||||||
|
return []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "http-namespace",
|
||||||
|
Value: "com.owncloud.web",
|
||||||
|
Usage: "Set the base namespace for the http namespace",
|
||||||
|
EnvVars: []string{"OCS_NAMESPACE"},
|
||||||
|
Destination: &cfg.Service.Namespace,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "name",
|
||||||
|
Value: "ocs",
|
||||||
|
Usage: "Service name",
|
||||||
|
EnvVars: []string{"OCS_NAME"},
|
||||||
|
Destination: &cfg.Service.Name,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
|
import "github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Namespace defines the namespace for the defines metrics.
|
// Namespace defines the namespace for the defines metrics.
|
||||||
Namespace = "ocis"
|
Namespace = "ocis"
|
||||||
@@ -11,6 +13,7 @@ var (
|
|||||||
// Metrics defines the available metrics of this service.
|
// Metrics defines the available metrics of this service.
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
// Counter *prometheus.CounterVec
|
// Counter *prometheus.CounterVec
|
||||||
|
BuildInfo *prometheus.GaugeVec
|
||||||
}
|
}
|
||||||
|
|
||||||
// New initializes the available metrics.
|
// New initializes the available metrics.
|
||||||
@@ -22,11 +25,21 @@ func New() *Metrics {
|
|||||||
// Name: "greet_total",
|
// Name: "greet_total",
|
||||||
// Help: "How many greeting requests processed",
|
// Help: "How many greeting requests processed",
|
||||||
// }, []string{}),
|
// }, []string{}),
|
||||||
|
BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||||
|
Namespace: Namespace,
|
||||||
|
Subsystem: Subsystem,
|
||||||
|
Name: "build_info",
|
||||||
|
Help: "Build Information",
|
||||||
|
}, []string{"version"}),
|
||||||
}
|
}
|
||||||
|
|
||||||
// prometheus.Register(
|
// prometheus.Register(
|
||||||
// m.Counter,
|
// m.Counter,
|
||||||
// )
|
// )
|
||||||
|
|
||||||
|
_ = prometheus.Register(
|
||||||
|
m.BuildInfo,
|
||||||
|
)
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/owncloud/ocis/ocs/pkg/config"
|
|
||||||
"github.com/owncloud/ocis/ocs/pkg/version"
|
|
||||||
"github.com/owncloud/ocis/ocis-pkg/service/debug"
|
"github.com/owncloud/ocis/ocis-pkg/service/debug"
|
||||||
|
"github.com/owncloud/ocis/ocs/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server initializes the debug service and server.
|
// Server initializes the debug service and server.
|
||||||
@@ -15,8 +14,8 @@ func Server(opts ...Option) (*http.Server, error) {
|
|||||||
|
|
||||||
return debug.NewService(
|
return debug.NewService(
|
||||||
debug.Logger(options.Logger),
|
debug.Logger(options.Logger),
|
||||||
debug.Name("ocs"),
|
debug.Name(options.Config.Service.Name),
|
||||||
debug.Version(version.String),
|
debug.Version(options.Config.Service.Version),
|
||||||
debug.Address(options.Config.Debug.Addr),
|
debug.Address(options.Config.Debug.Addr),
|
||||||
debug.Token(options.Config.Debug.Token),
|
debug.Token(options.Config.Debug.Token),
|
||||||
debug.Pprof(options.Config.Debug.Pprof),
|
debug.Pprof(options.Config.Debug.Pprof),
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
svc "github.com/owncloud/ocis/ocs/pkg/service/v0"
|
|
||||||
"github.com/owncloud/ocis/ocs/pkg/version"
|
|
||||||
"github.com/owncloud/ocis/ocis-pkg/middleware"
|
"github.com/owncloud/ocis/ocis-pkg/middleware"
|
||||||
"github.com/owncloud/ocis/ocis-pkg/service/http"
|
"github.com/owncloud/ocis/ocis-pkg/service/http"
|
||||||
|
svc "github.com/owncloud/ocis/ocs/pkg/service/v0"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server initializes the http service and server.
|
// Server initializes the http service and server.
|
||||||
@@ -13,9 +12,9 @@ func Server(opts ...Option) (http.Service, error) {
|
|||||||
|
|
||||||
service := http.NewService(
|
service := http.NewService(
|
||||||
http.Logger(options.Logger),
|
http.Logger(options.Logger),
|
||||||
http.Name("ocs"),
|
http.Name(options.Config.Service.Name),
|
||||||
http.Version(version.String),
|
http.Version(options.Config.Service.Version),
|
||||||
http.Namespace(options.Config.HTTP.Namespace),
|
http.Namespace(options.Config.Service.Namespace),
|
||||||
http.Address(options.Config.HTTP.Addr),
|
http.Address(options.Config.HTTP.Addr),
|
||||||
http.Context(options.Context),
|
http.Context(options.Context),
|
||||||
http.Flags(options.Flags...),
|
http.Flags(options.Flags...),
|
||||||
@@ -31,8 +30,8 @@ func Server(opts ...Option) (http.Service, error) {
|
|||||||
middleware.Cors,
|
middleware.Cors,
|
||||||
middleware.Secure,
|
middleware.Secure,
|
||||||
middleware.Version(
|
middleware.Version(
|
||||||
"ocs",
|
options.Config.Service.Name,
|
||||||
version.String,
|
options.Config.Service.Version,
|
||||||
),
|
),
|
||||||
middleware.Logger(
|
middleware.Logger(
|
||||||
options.Logger,
|
options.Logger,
|
||||||
|
|||||||
Reference in New Issue
Block a user