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 17:05:51 +02:00
parent cae6a1c3ac
commit c05b5a3a81
12 changed files with 103 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ package command
import (
"github.com/micro/cli/v2"
"github.com/owncloud/ocis/ocis/pkg/version"
"github.com/owncloud/ocis/thumbnails/pkg/command"
"github.com/owncloud/ocis/thumbnails/pkg/flagset"
"github.com/owncloud/ocis/ocis/pkg/config"
@@ -19,6 +20,9 @@ func ThumbnailsCommand(cfg *config.Config) *cli.Command {
Usage: "Start thumbnails server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Thumbnails),
Subcommands: []*cli.Command{
command.PrintVersion(cfg.Thumbnails),
},
Action: func(c *cli.Context) error {
thumbnailsCommand := command.Server(configureThumbnails(cfg))
@@ -35,6 +39,7 @@ func configureThumbnails(cfg *config.Config) *svcconfig.Config {
cfg.Thumbnails.Log.Level = cfg.Log.Level
cfg.Thumbnails.Log.Pretty = cfg.Log.Pretty
cfg.Thumbnails.Log.Color = cfg.Log.Color
cfg.Thumbnails.Server.Version = version.String
if cfg.Tracing.Enabled {
cfg.Thumbnails.Tracing.Enabled = cfg.Tracing.Enabled

View File

@@ -0,0 +1,6 @@
Enhancement: Add version command
Added a command to print the versions of all running ocis-thumbnails service instances.
Also added an entry in the metrics which shows build information like the version.
https://github.com/owncloud/product/issues/226

View File

@@ -26,6 +26,7 @@ require (
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/ogier/pflag v0.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
github.com/pkg/errors v0.9.1

View File

@@ -723,6 +723,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
@@ -845,6 +846,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=
@@ -1018,6 +1020,7 @@ github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTd
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.3.4 h1:8q6vk3hthlpb2SouZcnBVKboxWQWMDNF38bwholZrJc=
github.com/spf13/afero v1.3.4/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
@@ -1364,6 +1367,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -1565,6 +1569,7 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -32,12 +32,14 @@ func Execute() error {
Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
cfg.Server.Version = version.String
return ParseConfig(c, cfg)
},
Commands: []*cli.Command{
Server(cfg),
Health(cfg),
PrintVersion(cfg),
},
}

View File

@@ -128,6 +128,8 @@ func Server(cfg *config.Config) *cli.Command {
defer cancel()
metrics.BuildInfo.WithLabelValues(cfg.Server.Version).Set(1)
service := grpc.NewService(
grpc.Logger(logger),
grpc.Context(ctx),

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/thumbnails/pkg/config"
"github.com/owncloud/ocis/thumbnails/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.ListThumbnailsWithConfig(cfg),
Action: func(c *cli.Context) error {
reg := mdns.NewRegistry()
services, err := reg.GetService(cfg.Server.Namespace + "." + cfg.Server.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get thumbnails services from the registry: %v", err))
return err
}
if len(services) == 0 {
fmt.Println("No running thumbnails 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
},
}
}

View File

@@ -20,6 +20,7 @@ type Server struct {
Name string
Namespace string
Address string
Version string
}
// Tracing defines the available tracing configuration.

View File

@@ -168,3 +168,23 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
},
}
}
// ListThumbnailsWithConfig applies the config to the flagset for listing thumbnails services.
func ListThumbnailsWithConfig(cfg *config.Config) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "grpc-name",
Value: "thumbnails",
Usage: "Name of the service",
EnvVars: []string{"THUMBNAILS_GRPC_NAME"},
Destination: &cfg.Server.Name,
},
&cli.StringFlag{
Name: "grpc-namespace",
Value: "com.owncloud.api",
Usage: "Set the base namespace for the grpc namespace",
EnvVars: []string{"THUMBNAILS_GRPC_NAMESPACE"},
Destination: &cfg.Server.Namespace,
},
}
}

View File

@@ -12,9 +12,10 @@ var (
// Metrics defines the available metrics of this service.
type Metrics struct {
Counter *prometheus.CounterVec
Latency *prometheus.SummaryVec
Duration *prometheus.HistogramVec
Counter *prometheus.CounterVec
Latency *prometheus.SummaryVec
Duration *prometheus.HistogramVec
BuildInfo *prometheus.GaugeVec
}
// New initializes the available metrics.
@@ -38,6 +39,12 @@ func New() *Metrics {
Name: "getthumbnail_duration_seconds",
Help: "GetThumbnail method requests time in seconds",
}, []string{}),
BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: Subsystem,
Name: "build_info",
Help: "Build information",
}, []string{"version"}),
}
_ = prometheus.Register(
@@ -52,5 +59,9 @@ func New() *Metrics {
m.Duration,
)
_ = prometheus.Register(
m.BuildInfo,
)
return m
}

View File

@@ -6,7 +6,6 @@ import (
"github.com/owncloud/ocis/ocis-pkg/service/debug"
"github.com/owncloud/ocis/thumbnails/pkg/config"
"github.com/owncloud/ocis/thumbnails/pkg/version"
)
// Server initializes the debug service and server.
@@ -16,7 +15,7 @@ func Server(opts ...Option) (*http.Server, error) {
return debug.NewService(
debug.Logger(options.Logger),
debug.Name(options.Config.Server.Name),
debug.Version(version.String),
debug.Version(options.Config.Server.Version),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),

View File

@@ -21,6 +21,7 @@ func NewService(opts ...Option) grpc.Service {
grpc.Address(options.Address),
grpc.Context(options.Context),
grpc.Flags(options.Flags...),
grpc.Version(options.Config.Server.Version),
)
var thumbnail proto.ThumbnailServiceHandler