From 6a7bc01ede57bbf633b181bda00cb9d7b801bbcf Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 26 Apr 2023 15:45:44 +0200 Subject: [PATCH] add debug server to notifications Signed-off-by: Christian Richter --- .../add-notifications-debug-server.md | 6 +++ services/notifications/pkg/command/server.go | 42 ++++++++++++++++++- services/notifications/pkg/config/config.go | 13 +++++- .../pkg/config/defaults/defaultconfig.go | 16 ++++++- 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/add-notifications-debug-server.md diff --git a/changelog/unreleased/add-notifications-debug-server.md b/changelog/unreleased/add-notifications-debug-server.md new file mode 100644 index 00000000000..b87ce43c584 --- /dev/null +++ b/changelog/unreleased/add-notifications-debug-server.md @@ -0,0 +1,6 @@ +Enhancement: Open Debug endpoint for Notifications + +We added a debug server to the notifications service + +https://github.com/owncloud/ocis/issues/5002 +https://github.com/owncloud/ocis/pull/6155 \ No newline at end of file diff --git a/services/notifications/pkg/command/server.go b/services/notifications/pkg/command/server.go index 2bacb06619d..f1b623c2b9c 100644 --- a/services/notifications/pkg/command/server.go +++ b/services/notifications/pkg/command/server.go @@ -1,6 +1,7 @@ package command import ( + "context" "crypto/tls" "crypto/x509" "fmt" @@ -10,9 +11,13 @@ import ( "github.com/cs3org/reva/v2/pkg/events/stream" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/go-micro/plugins/v4/events/natsjs" + "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" "github.com/owncloud/ocis/v2/ocis-pkg/crypto" + "github.com/owncloud/ocis/v2/ocis-pkg/handlers" + "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" + "github.com/owncloud/ocis/v2/ocis-pkg/version" settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" "github.com/owncloud/ocis/v2/services/notifications/pkg/channels" "github.com/owncloud/ocis/v2/services/notifications/pkg/config" @@ -34,6 +39,36 @@ func Server(cfg *config.Config) *cli.Command { Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) + gr := run.Group{} + + ctx, cancel := func() (context.Context, context.CancelFunc) { + if cfg.Context == nil { + return context.WithCancel(context.Background()) + } + return context.WithCancel(cfg.Context) + }() + + defer cancel() + + { + server := debug.NewService( + debug.Logger(logger), + debug.Name(cfg.Service.Name), + debug.Version(version.GetString()), + debug.Address(cfg.Debug.Addr), + debug.Token(cfg.Debug.Token), + debug.Pprof(cfg.Debug.Pprof), + debug.Zpages(cfg.Debug.Zpages), + debug.Health(handlers.Health), + debug.Ready(handlers.Ready), + ) + + gr.Add(server.ListenAndServe, func(_ error) { + _ = server.Shutdown(ctx) + cancel() + }) + } + // evs defines a list of events to subscribe to evs := []events.Unmarshaller{ events.ShareCreated{}, @@ -97,7 +132,12 @@ func Server(cfg *config.Config) *cli.Command { } valueService := settingssvc.NewValueService("com.owncloud.api.settings", grpc.DefaultClient()) svc := service.NewEventsNotifier(evts, channel, logger, gwclient, valueService, cfg.Notifications.MachineAuthAPIKey, cfg.Notifications.EmailTemplatePath, cfg.WebUIURL) - return svc.Run() + + gr.Add(svc.Run, func(error) { + cancel() + }) + + return gr.Run() }, } } diff --git a/services/notifications/pkg/config/config.go b/services/notifications/pkg/config/config.go index 34ce4a26aa1..4e4fb269f4c 100644 --- a/services/notifications/pkg/config/config.go +++ b/services/notifications/pkg/config/config.go @@ -12,8 +12,9 @@ type Config struct { Service Service `yaml:"-"` - Log *Log `yaml:"log"` - Debug Debug `yaml:"debug"` + Tracing *Tracing `yaml:"tracing"` + Log *Log `yaml:"log"` + Debug Debug `yaml:"debug"` WebUIURL string `yaml:"ocis_url" env:"OCIS_URL;NOTIFICATIONS_WEB_UI_URL" desc:"The public facing URL of the oCIS Web UI, used e.g. when sending notification eMails"` @@ -54,3 +55,11 @@ type Events struct { TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"NOTIFICATIONS_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided NOTIFICATIONS_EVENTS_TLS_INSECURE will be seen as false."` EnableTLS bool `yaml:"enable_tls" env:"OCIS_EVENTS_ENABLE_TLS;NOTIFICATIONS_EVENTS_ENABLE_TLS" desc:"Enable TLS for the connection to the events broker. The events broker is the ocis service which receives and delivers events between the services.."` } + +// Tracing defines the available tracing configuration. +type Tracing struct { + Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;IDM_TRACING_ENABLED" desc:"Activates tracing."` + Type string `yaml:"type" env:"OCIS_TRACING_TYPE;IDM_TRACING_TYPE" desc:"The type of tracing. Defaults to \"\", which is the same as \"jaeger\". Allowed tracing types are \"jaeger\" and \"\" as of now."` + Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;IDM_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` + Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;IDM_TRACING_COLLECTOR" desc:"The HTTP endpoint for sending spans directly to a collector, i.e. http://jaeger-collector:14268/api/traces. Only used if the tracing endpoint is unset."` +} diff --git a/services/notifications/pkg/config/defaults/defaultconfig.go b/services/notifications/pkg/config/defaults/defaultconfig.go index 20cd4733042..5f3e1961c11 100644 --- a/services/notifications/pkg/config/defaults/defaultconfig.go +++ b/services/notifications/pkg/config/defaults/defaultconfig.go @@ -21,7 +21,9 @@ func FullDefaultConfig() *config.Config { func DefaultConfig() *config.Config { return &config.Config{ Debug: config.Debug{ - Addr: "127.0.0.1:9174", + Addr: "127.0.0.1:9174", + Zpages: false, + Pprof: false, }, Service: config.Service{ Name: "notifications", @@ -60,6 +62,18 @@ func EnsureDefaults(cfg *config.Config) { cfg.Log = &config.Log{} } + // provide with defaults for shared tracing, since we need a valid destination address for "envdecode". + if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { + cfg.Tracing = &config.Tracing{ + Enabled: cfg.Commons.Tracing.Enabled, + Type: cfg.Commons.Tracing.Type, + Endpoint: cfg.Commons.Tracing.Endpoint, + Collector: cfg.Commons.Tracing.Collector, + } + } else if cfg.Tracing == nil { + cfg.Tracing = &config.Tracing{} + } + if cfg.Notifications.MachineAuthAPIKey == "" && cfg.Commons != nil && cfg.Commons.MachineAuthAPIKey != "" { cfg.Notifications.MachineAuthAPIKey = cfg.Commons.MachineAuthAPIKey }