Skip to content

Commit

Permalink
Merge pull request #614 from owncloud/ocs-version-command
Browse files Browse the repository at this point in the history
add version command and add build information to metrics
  • Loading branch information
butonic authored Sep 28, 2020
2 parents b0d281f + be71b84 commit 369b8d3
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 14 deletions.
5 changes: 5 additions & 0 deletions ocis/pkg/command/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package command

import (
"github.com/micro/cli/v2"
"github.com/owncloud/ocis/ocis/pkg/version"
"github.com/owncloud/ocis/ocs/pkg/command"
svcconfig "github.com/owncloud/ocis/ocs/pkg/config"
"github.com/owncloud/ocis/ocs/pkg/flagset"
Expand All @@ -18,6 +19,9 @@ func OCSCommand(cfg *config.Config) *cli.Command {
Usage: "Start ocs server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.OCS),
Subcommands: []*cli.Command{
command.PrintVersion(cfg.OCS),
},
Action: func(ctx *cli.Context) error {
ocsCommand := command.Server(configureOCS(cfg))

Expand All @@ -34,6 +38,7 @@ func configureOCS(cfg *config.Config) *svcconfig.Config {
cfg.OCS.Log.Level = cfg.Log.Level
cfg.OCS.Log.Pretty = cfg.Log.Pretty
cfg.OCS.Log.Color = cfg.Log.Color
cfg.OCS.Service.Version = version.String

if cfg.Tracing.Enabled {
cfg.OCS.Tracing.Enabled = cfg.Tracing.Enabled
Expand Down
6 changes: 6 additions & 0 deletions ocs/changelog/unreleased/add-version-command.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions ocs/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down
2 changes: 2 additions & 0 deletions ocs/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func Server(cfg *config.Config) *cli.Command {

defer cancel()

metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)

{
server, err := http.Server(
http.Logger(logger),
Expand Down
45 changes: 45 additions & 0 deletions ocs/pkg/command/version.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
11 changes: 9 additions & 2 deletions ocs/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ type Debug struct {

// HTTP defines the available http configuration.
type HTTP 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.
Expand All @@ -44,6 +50,7 @@ type Config struct {
HTTP HTTP
Tracing Tracing
TokenManager TokenManager
Service Service
}

// New initializes a new configuration with or without defaults.
Expand Down
29 changes: 28 additions & 1 deletion ocs/pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
Value: "com.owncloud.web",
Usage: "Set the base namespace for the http 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{
Name: "http-root",
Expand All @@ -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,
},
}
}
13 changes: 13 additions & 0 deletions ocs/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

var (
// Namespace defines the namespace for the defines metrics.
Namespace = "ocis"
Expand All @@ -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.
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions ocs/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"io"
"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/ocs/pkg/config"
)

// Server initializes the debug service and server.
Expand All @@ -15,8 +14,8 @@ func Server(opts ...Option) (*http.Server, error) {

return debug.NewService(
debug.Logger(options.Logger),
debug.Name("ocs"),
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),
Expand Down
13 changes: 6 additions & 7 deletions ocs/pkg/server/http/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package http

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/service/http"
svc "github.com/owncloud/ocis/ocs/pkg/service/v0"
)

// Server initializes the http service and server.
Expand All @@ -13,9 +12,9 @@ func Server(opts ...Option) (http.Service, error) {

service := http.NewService(
http.Logger(options.Logger),
http.Name("ocs"),
http.Version(version.String),
http.Namespace(options.Config.HTTP.Namespace),
http.Name(options.Config.Service.Name),
http.Version(options.Config.Service.Version),
http.Namespace(options.Config.Service.Namespace),
http.Address(options.Config.HTTP.Addr),
http.Context(options.Context),
http.Flags(options.Flags...),
Expand All @@ -31,8 +30,8 @@ func Server(opts ...Option) (http.Service, error) {
middleware.Cors,
middleware.Secure,
middleware.Version(
"ocs",
version.String,
options.Config.Service.Name,
options.Config.Service.Version,
),
middleware.Logger(
options.Logger,
Expand Down

0 comments on commit 369b8d3

Please sign in to comment.