Skip to content

Commit

Permalink
use RunWithEnv instead of os.Setenv to avoid environment variable races
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 17, 2018
1 parent dbf09c5 commit 99f41bc
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions metrics/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -123,10 +122,8 @@ var pluginMetaHeadlineReg = regexp.MustCompile(`^#\s*mackerel-agent-plugin\b(.*)
// }
func (g *pluginGenerator) loadPluginMeta() error {
// Set environment variable to make the plugin command generate its configuration
os.Setenv(pluginConfigurationEnvName, "1")
defer os.Setenv(pluginConfigurationEnvName, "")

stdout, stderr, exitCode, err := g.Config.Command.Run()
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)
}
Expand Down Expand Up @@ -218,8 +215,8 @@ func makeCreateGraphDefsPayload(meta *pluginMeta) []mackerel.CreateGraphDefsPayl
}

func (g *pluginGenerator) collectValues() (Values, error) {
os.Setenv(pluginConfigurationEnvName, "")
stdout, stderr, _, err := g.Config.Command.Run()
pluginMetaEnv := pluginConfigurationEnvName + "="
stdout, stderr, _, err := g.Config.Command.RunWithEnv([]string{pluginMetaEnv})

if stderr != "" {
pluginLogger.Infof("command %s outputted to STDERR: %q", g.Config.Command.CommandString(), stderr)
Expand Down

0 comments on commit 99f41bc

Please sign in to comment.