From 71f31b43545a4911cd07eb50ceea42b155bf6bca Mon Sep 17 00:00:00 2001 From: David Christofas Date: Wed, 29 Jul 2020 11:02:03 +0200 Subject: [PATCH] add metrics endpoint Signed-off-by: David Christofas --- changelog/unreleased/serve-metrics.md | 5 ++++ pkg/command/server.go | 35 ++++++++++++++++++++++ pkg/server/debug/option.go | 42 ++++++++++++++++++++++++++ pkg/server/debug/server.go | 43 +++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 changelog/unreleased/serve-metrics.md create mode 100644 pkg/server/debug/option.go create mode 100644 pkg/server/debug/server.go diff --git a/changelog/unreleased/serve-metrics.md b/changelog/unreleased/serve-metrics.md new file mode 100644 index 000000000..72c003beb --- /dev/null +++ b/changelog/unreleased/serve-metrics.md @@ -0,0 +1,5 @@ +Enhancement: serve the metrics endpoint + +Added the metrics endpoint to be able to monitor this service like the other services + +https://github.com/owncloud/ocis-thumbnails/issues/37 diff --git a/pkg/command/server.go b/pkg/command/server.go index 833aaf5fe..44a89a79f 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -17,6 +17,7 @@ import ( "github.com/owncloud/ocis-thumbnails/pkg/config" "github.com/owncloud/ocis-thumbnails/pkg/flagset" "github.com/owncloud/ocis-thumbnails/pkg/metrics" + "github.com/owncloud/ocis-thumbnails/pkg/server/debug" "github.com/owncloud/ocis-thumbnails/pkg/server/grpc" "go.opencensus.io/stats/view" "go.opencensus.io/trace" @@ -145,6 +146,40 @@ func Server(cfg *config.Config) *cli.Command { cancel() }) + server, err := debug.Server( + debug.Logger(logger), + debug.Config(cfg), + ) + + if err != nil { + logger.Info(). + Err(err). + Str("transport", "debug"). + Msg("Failed to initialize server") + + return err + } + + gr.Add(func() error { + return server.ListenAndServe() + }, func(_ error) { + ctx, timeout := context.WithTimeout(ctx, 5*time.Second) + + defer timeout() + defer cancel() + + if err := server.Shutdown(ctx); err != nil { + logger.Info(). + Err(err). + Str("transport", "debug"). + Msg("Failed to shutdown server") + } else { + logger.Info(). + Str("transport", "debug"). + Msg("Shutting down server") + } + }) + { stop := make(chan os.Signal, 1) diff --git a/pkg/server/debug/option.go b/pkg/server/debug/option.go new file mode 100644 index 000000000..3349a7875 --- /dev/null +++ b/pkg/server/debug/option.go @@ -0,0 +1,42 @@ +package debug + +import ( + "github.com/owncloud/ocis-pkg/v2/log" + "github.com/owncloud/ocis-thumbnails/pkg/config" +) + +// Option defines a single option function. +type Option func(o *Options) + +// Options defines the available options for this package. +type Options struct { + Name string + Address string + Logger log.Logger + Config *config.Config +} + +// newOptions initializes the available default options. +func newOptions(opts ...Option) Options { + opt := Options{} + + for _, o := range opts { + o(&opt) + } + + return opt +} + +// Logger provides a function to set the logger option. +func Logger(val log.Logger) Option { + return func(o *Options) { + o.Logger = val + } +} + +// Config provides a function to set the config option. +func Config(val *config.Config) Option { + return func(o *Options) { + o.Config = val + } +} diff --git a/pkg/server/debug/server.go b/pkg/server/debug/server.go new file mode 100644 index 000000000..cb3603536 --- /dev/null +++ b/pkg/server/debug/server.go @@ -0,0 +1,43 @@ +package debug + +import ( + "io" + "net/http" + + "github.com/owncloud/ocis-pkg/v2/service/debug" + "github.com/owncloud/ocis-thumbnails/pkg/config" + "github.com/owncloud/ocis-thumbnails/pkg/version" +) + +// Server initializes the debug service and server. +func Server(opts ...Option) (*http.Server, error) { + options := newOptions(opts...) + + return debug.NewService( + debug.Logger(options.Logger), + debug.Name(options.Config.Server.Name), + debug.Version(version.String), + debug.Address(options.Config.Debug.Addr), + debug.Token(options.Config.Debug.Token), + debug.Pprof(options.Config.Debug.Pprof), + debug.Zpages(options.Config.Debug.Zpages), + debug.Health(health(options.Config)), + debug.Ready(ready(options.Config)), + ), nil +} + +func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + io.WriteString(w, http.StatusText(http.StatusOK)) + } +} + +func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + io.WriteString(w, http.StatusText(http.StatusOK)) + } +}