Skip to content

Commit

Permalink
Merge pull request #1024 from mackerelio/log-plugin-bug
Browse files Browse the repository at this point in the history
Write out error message when plugin seems have a bug
  • Loading branch information
rmatsuoka authored Nov 20, 2024
2 parents cf9842c + 79073bc commit f26b61c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"time"

"github.com/mackerelio/mackerel-agent/checks"
Expand Down Expand Up @@ -98,7 +99,11 @@ func (agent *Agent) CollectGraphDefsOfPlugins() []*mkr.GraphDefsParam {

for _, g := range agent.PluginGenerators {
p, err := g.PrepareGraphDefs()
if err != nil {

var faultError *metrics.PluginFaultError
if errors.As(err, &faultError) {
logger.Errorf("Failed to fetch meta information from plugin %v; seems that the plugin has a bug: %v", g, err)
} else if err != nil {
logger.Debugf("Failed to fetch meta information from plugin %v (non critical); seems that this plugin does not have meta information: %v", g, err)
}
if p != nil {
Expand Down
15 changes: 15 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ type PluginGenerator interface {
PrepareGraphDefs() ([]*mkr.GraphDefsParam, error)
CustomIdentifier() *string
}

// PluginFaultError may be returned by [PluginGenerator.PrepareGraphDefs].
// This error indicates a bug in a plugin and should be logged for a user.
// Note that [PluginGenerator.PrepareGraphDefs] can also return other error types.
type PluginFaultError struct {
Err error
}

func (e *PluginFaultError) Error() string {
return e.Err.Error()
}

func (e *PluginFaultError) Unwrap() error {
return e.Err
}
4 changes: 2 additions & 2 deletions metrics/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (g *pluginGenerator) loadPluginMeta() error {
pluginMetaEnv := pluginConfigurationEnvName + "=1"
stdout, stderr, exitCode, err := g.Config.Command.RunWithEnv([]string{pluginMetaEnv})
if err != nil {
return fmt.Errorf("running %s failed: %s, exit=%d stderr=%q", g.Config.Command.CommandString(), err, exitCode, stderr)
return &PluginFaultError{fmt.Errorf("running %s failed: %s, exit=%d stderr=%q", g.Config.Command.CommandString(), err, exitCode, stderr)}
}

outBuffer := bufio.NewReader(strings.NewReader(stdout))
Expand Down Expand Up @@ -170,7 +170,7 @@ func (g *pluginGenerator) loadPluginMeta() error {
err = json.NewDecoder(outBuffer).Decode(conf)

if err != nil {
return fmt.Errorf("while reading plugin configuration: %s", err)
return &PluginFaultError{fmt.Errorf("while reading plugin configuration: %s", err)}
}

g.Meta = conf
Expand Down

0 comments on commit f26b61c

Please sign in to comment.