Skip to content

Commit 2a5f2a9

Browse files
authored
Merge pull request #246 from wafuwafu13/plugin-debug-log
Enable to debug env
2 parents 960e0d1 + 9c0d8ec commit 2a5f2a9

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ linux:
3434
docker:
3535
docker build -t $(BIN) -t $(BIN):$(VERSION) --target container-agent .
3636

37+
.PHONY: docker-with-plugins
38+
docker-with-plugins:
39+
docker build -t $(BIN) -t $(BIN):$(VERSION) --target container-agent-with-plugins .
40+
3741
.PHONY: version
3842
version:
3943
echo $(VERSION)

check/plugin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package check
22

33
import (
44
"context"
5+
"strings"
56
"time"
67

78
mackerel "github.com/mackerelio/mackerel-client-go"
@@ -28,6 +29,13 @@ func (g *pluginGenerator) Config() mackerel.CheckConfig {
2829
// Generate generates check report
2930
func (g *pluginGenerator) Generate(ctx context.Context) (*Result, error) {
3031
now := time.Now()
32+
var masked_env []string
33+
for _, v := range g.Env {
34+
key := strings.Split(v, "=")[0]
35+
value := strings.Split(v, "=")[1]
36+
masked_env = append(masked_env, key+"="+config.MaskEnvValue(value))
37+
}
38+
logger.Debugf("plugin %s command: %s env: %+v", g.Name, g.Command, masked_env)
3139
stdout, stderr, exitCode, err := cmdutil.RunCommand(ctx, g.Command, g.User, g.Env, g.Timeout)
3240

3341
if stderr != "" {

cmd/mackerel-container-agent/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/mackerelio/golib/logging"
77

88
"github.com/mackerelio/mackerel-container-agent/agent"
9+
"github.com/mackerelio/mackerel-container-agent/config"
910
)
1011

1112
const cmdName = "mackerel-container-agent"
@@ -33,6 +34,28 @@ func main() {
3334
logging.SetLogLevel(logging.INFO)
3435
}
3536

37+
logger.Debugf("MACKEREL_APIBASE=%s", config.MaskEnvValue(os.Getenv("MACKEREL_APIBASE")))
38+
logger.Debugf("MACKEREL_APIKEY=%s", config.MaskEnvValue(os.Getenv("MACKEREL_APIKEY")))
39+
40+
env := []string{
41+
"MACKEREL_AGENT_CONFIG_POLLING_DURATION_MINUTES",
42+
"MACKEREL_AGENT_CONFIG",
43+
"MACKEREL_AGENT_PLUGIN_META",
44+
"MACKEREL_CONTAINER_PLATFORM",
45+
"MACKEREL_HOST_STATUS_ON_START",
46+
"MACKEREL_IGNORE_CONTAINER",
47+
"MACKEREL_KUBERNETES_KUBELET_HOST",
48+
"MACKEREL_KUBERNETES_KUBELET_INSECURE_TLS",
49+
"MACKEREL_KUBERNETES_KUBELET_READ_ONLY_PORT",
50+
"MACKEREL_KUBERNETES_NAMESPACE",
51+
"MACKEREL_KUBERNETES_POD_NAME",
52+
"MACKEREL_LOG_LEVEL",
53+
"MACKEREL_ROLES",
54+
}
55+
for _, v := range env {
56+
logger.Debugf("%s=%s", v, os.Getenv(v))
57+
}
58+
3659
os.Exit(run(os.Args[1:]))
3760
}
3861

config/env.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ func buildEnv(envMap map[string]string) ([]string, error) {
3939
sort.Strings(env)
4040
return env, nil
4141
}
42+
43+
// MaskEnvValue return masked env value ex) FOOBARBAZ -> FOOB***
44+
func MaskEnvValue(s string) string {
45+
if len(s) < 4 {
46+
return s
47+
} else {
48+
return s[:4] + "***"
49+
}
50+
}

config/env_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMaskEnvValue(t *testing.T) {
8+
testCases := []struct {
9+
envValue string
10+
expect string
11+
}{
12+
{
13+
envValue: "AAA",
14+
expect: "AAA",
15+
},
16+
{
17+
envValue: "BBBBBBBBBBB",
18+
expect: "BBBB***",
19+
},
20+
{
21+
envValue: "CCC CC",
22+
expect: "CCC ***",
23+
},
24+
}
25+
26+
for _, tc := range testCases {
27+
if MaskEnvValue(tc.envValue) != tc.expect {
28+
t.Fatalf("expect %s, actual %s", tc.expect, MaskEnvValue(tc.envValue))
29+
}
30+
}
31+
}

metric/plugin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func NewPluginGenerator(p *config.MetricPlugin) Generator {
3131
// Generate generates metric values
3232
func (g *pluginGenerator) Generate(ctx context.Context) (Values, error) {
3333
env := append(g.Env, pluginMetaEnvName+"=")
34+
var masked_env []string
35+
for _, v := range env {
36+
key := strings.Split(v, "=")[0]
37+
value := strings.Split(v, "=")[1]
38+
masked_env = append(masked_env, key+"="+config.MaskEnvValue(value))
39+
}
40+
logger.Debugf("plugin %s command: %s env: %+v", g.Name, g.Command, masked_env)
3441
stdout, stderr, _, err := cmdutil.RunCommand(ctx, g.Command, g.User, env, g.Timeout)
3542

3643
if stderr != "" {

0 commit comments

Comments
 (0)