From a4df08e0abe77a05f0e6a409146bc51de12b16a9 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 27 Apr 2023 15:18:31 +0200 Subject: [PATCH] add debug to audit Signed-off-by: Christian Richter --- changelog/unreleased/add-debug-to-audit.md | 6 +++ services/audit/pkg/command/server.go | 51 ++++++++++++++++--- services/audit/pkg/config/config.go | 13 ++++- .../pkg/config/defaults/defaultconfig.go | 16 +++++- 4 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 changelog/unreleased/add-debug-to-audit.md diff --git a/changelog/unreleased/add-debug-to-audit.md b/changelog/unreleased/add-debug-to-audit.md new file mode 100644 index 00000000000..8023c06aa6f --- /dev/null +++ b/changelog/unreleased/add-debug-to-audit.md @@ -0,0 +1,6 @@ +Enhancement: Add debug server to audit + +We added a debug server to audit. + +https://github.com/owncloud/ocis/pull/6178 +https://github.com/owncloud/ocis/issues/5002 \ No newline at end of file diff --git a/services/audit/pkg/command/server.go b/services/audit/pkg/command/server.go index ac10a5b683a..f869ce29b15 100644 --- a/services/audit/pkg/command/server.go +++ b/services/audit/pkg/command/server.go @@ -10,8 +10,12 @@ import ( "github.com/cs3org/reva/v2/pkg/events" "github.com/cs3org/reva/v2/pkg/events/stream" "github.com/go-micro/plugins/v4/events/natsjs" + "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" ociscrypto "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/version" "github.com/owncloud/ocis/v2/services/audit/pkg/config" "github.com/owncloud/ocis/v2/services/audit/pkg/config/parser" "github.com/owncloud/ocis/v2/services/audit/pkg/logging" @@ -30,13 +34,17 @@ func Server(cfg *config.Config) *cli.Command { return configlog.ReturnFatal(parser.ParseConfig(cfg)) }, Action: func(c *cli.Context) error { - logger := logging.Configure(cfg.Service.Name, cfg.Log) + var ( + gr = run.Group{} + logger = logging.Configure(cfg.Service.Name, cfg.Log) - ctx := cfg.Context - if ctx == nil { - ctx = context.Background() - } - ctx, cancel := context.WithCancel(ctx) + ctx, cancel = func() (context.Context, context.CancelFunc) { + if cfg.Context == nil { + return context.WithCancel(context.Background()) + } + return context.WithCancel(cfg.Context) + }() + ) defer cancel() evtsCfg := cfg.Events @@ -76,8 +84,35 @@ func Server(cfg *config.Config) *cli.Command { return err } - svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger) - return nil + gr.Add(func() error { + svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger) + return nil + }, func(err error) { + logger.Error(). + Err(err). + Msg("Shutting down server") + 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() + }) + } + return gr.Run() }, } } diff --git a/services/audit/pkg/config/config.go b/services/audit/pkg/config/config.go index e485028be72..b6a340d6fcc 100644 --- a/services/audit/pkg/config/config.go +++ b/services/audit/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"` Events Events `yaml:"events"` Auditlog Auditlog `yaml:"auditlog"` @@ -38,3 +39,11 @@ type Auditlog struct { FilePath string `yaml:"filepath" env:"AUDIT_FILEPATH" desc:"Filepath to the logfile. Mandatory if LogToFile is true."` Format string `yaml:"format" env:"AUDIT_FORMAT" desc:"Log format. Using json is advised."` } + +// Tracing defines the available tracing configuration. +type Tracing struct { + Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;AUDIT_TRACING_ENABLED" desc:"Activates tracing."` + Type string `yaml:"type" env:"OCIS_TRACING_TYPE;AUDIT_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;AUDIT_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` + Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;AUDIT_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/audit/pkg/config/defaults/defaultconfig.go b/services/audit/pkg/config/defaults/defaultconfig.go index 51d0140dd09..a5bd5712376 100644 --- a/services/audit/pkg/config/defaults/defaultconfig.go +++ b/services/audit/pkg/config/defaults/defaultconfig.go @@ -16,7 +16,9 @@ func FullDefaultConfig() *config.Config { func DefaultConfig() *config.Config { return &config.Config{ Debug: config.Debug{ - Addr: "127.0.0.1:9234", + Addr: "127.0.0.1:9234", + Zpages: false, + Pprof: false, }, Service: config.Service{ Name: "audit", @@ -47,6 +49,18 @@ func EnsureDefaults(cfg *config.Config) { } else if cfg.Log == nil { 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{} + } } // Sanitize sanitized the configuration