From 5498b7a0c61df3c471580026a6312ee5ea0c60fe Mon Sep 17 00:00:00 2001 From: Spazzy Date: Wed, 29 May 2024 09:57:17 +0200 Subject: [PATCH] fix: plugin logging With go-plugin it is hard to debug the plugins, therefore this change should allow for more robust logging. We should in the future make this more confiurable and aligined with the internal logging system Signed-off-by: Spazzy --- cmd/exporter/main.go | 8 ++++++-- go.mod | 2 +- pkg/plugin/exporter.go | 7 +++++++ pkg/plugin/source.go | 7 +++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index 12eb849..8821ce4 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -74,7 +74,7 @@ func main() { setLogLevel(lvl, config.AppConfig().LogLevel) // Load exporter plugins - logger.Info("loading plugins") + logger.Info("loading exporters", "dir", config.AppConfig().Plugins.ExporterDir) pluginsystem := &plugin.ExportPluginSystem{ Dir: config.AppConfig().Plugins.ExporterDir, } @@ -84,9 +84,11 @@ func main() { logger.Error("failed to load exporter plugin system", "error", err) os.Exit(1) } + logger.Info("finished loading exporters", "output", + fmt.Sprintf("%d loaded", len(pluginsystem.Plugins))) // Load source plugins - logger.Info("loading sources") + logger.Info("loading sources", "dir", config.AppConfig().Plugins.SourceDir) sourcePluginSystem := &plugin.SourcePluginSystem{ Dir: config.AppConfig().Plugins.SourceDir, } @@ -96,6 +98,8 @@ func main() { logger.Error("failed to load exporter plugin system", "error", err) os.Exit(1) } + logger.Info("finished loading sources", "output", + fmt.Sprintf("%d loaded", len(sourcePluginSystem.Plugins))) // Init the application bus b := bus.New() diff --git a/go.mod b/go.mod index a406a43..89eb1a6 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/go-yaml/yaml v2.1.0+incompatible github.com/google/go-cmp v0.6.0 github.com/gorilla/mux v1.8.1 + github.com/hashicorp/go-hclog v1.5.0 github.com/hashicorp/go-plugin v1.6.0 github.com/prometheus/client_golang v1.17.0 github.com/prometheus/common v0.45.0 @@ -68,7 +69,6 @@ require ( github.com/google/s2a-go v0.1.7 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect - github.com/hashicorp/go-hclog v1.5.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect diff --git a/pkg/plugin/exporter.go b/pkg/plugin/exporter.go index 34a38e2..7db0c78 100644 --- a/pkg/plugin/exporter.go +++ b/pkg/plugin/exporter.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" "google.golang.org/grpc" @@ -133,6 +134,12 @@ func (e *ExportPluginSystem) Load(ctx context.Context) error { }, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, Cmd: exec.Command(pluginPath), + Logger: hclog.New(&hclog.LoggerOptions{ + Name: "exporter", + Output: os.Stdout, + Level: hclog.Debug, + JSONFormat: true, + }), }) // Start the plugin process. diff --git a/pkg/plugin/source.go b/pkg/plugin/source.go index 4cfba26..3aa23cb 100644 --- a/pkg/plugin/source.go +++ b/pkg/plugin/source.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" "google.golang.org/grpc" @@ -162,6 +163,12 @@ func (s *SourcePluginSystem) Load(ctx context.Context) error { }, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, Cmd: exec.Command(pluginPath), + Logger: hclog.New(&hclog.LoggerOptions{ + Name: "source", + Output: os.Stdout, + Level: hclog.Debug, + JSONFormat: true, + }), }) // Start the plugin process.