From a5197840dfa4fd3925b508614bc4b46bf01e9bef Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Thu, 2 Nov 2017 23:45:01 -0700 Subject: [PATCH] add config key reporting --- packer/telemetry.go | 39 +++++++++++++++++++++++++++++---------- packer/telemetry_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 packer/telemetry_test.go diff --git a/packer/telemetry.go b/packer/telemetry.go index 60005e606..f413dd34a 100644 --- a/packer/telemetry.go +++ b/packer/telemetry.go @@ -91,16 +91,11 @@ func (c *CheckpointTelemetry) AddSpan(name, pluginType string, options interface } log.Printf("[INFO] (telemetry) Starting %s %s", pluginType, name) - //strOpts := []string{} - if m, ok := options.(map[string]interface{}); ok { - for k, _ := range m { - log.Println("AddSpan options:", k) - } - } ts := &TelemetrySpan{ Name: name, - Type: pluginType, + Options: flattenConfigKeys(options), StartTime: time.Now().UTC(), + Type: pluginType, } c.spans = append(c.spans, ts) return ts @@ -123,6 +118,8 @@ func (c *CheckpointTelemetry) Finalize(command string, errCode int, err error) e extra.Error = err.Error() } params.Payload = extra + // b, _ := json.MarshalIndent(params, "", " ") + // log.Println(string(b)) ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond) defer cancel() @@ -132,12 +129,12 @@ func (c *CheckpointTelemetry) Finalize(command string, errCode int, err error) e } type TelemetrySpan struct { - Name string `json:"name"` - Type string `json:"type"` - StartTime time.Time `json:"start_time"` EndTime time.Time `json:"end_time"` Error string `json:"error"` + Name string `json:"name"` Options []string `json:"options"` + StartTime time.Time `json:"start_time"` + Type string `json:"type"` } func (s *TelemetrySpan) End(err error) { @@ -151,3 +148,25 @@ func (s *TelemetrySpan) End(err error) { log.Printf("[INFO] (telemetry) found error: %s", err.Error()) } } + +func flattenConfigKeys(options interface{}) []string { + var flatten func(string, interface{}) []string + + flatten = func(prefix string, options interface{}) (strOpts []string) { + if m, ok := options.(map[string]interface{}); ok { + for k, v := range m { + if prefix != "" { + k = prefix + "/" + k + } + if n, ok := v.(map[string]interface{}); ok { + strOpts = append(strOpts, flatten(k, n)...) + } else { + strOpts = append(strOpts, k) + } + } + } + return + } + + return flatten("", options) +} diff --git a/packer/telemetry_test.go b/packer/telemetry_test.go new file mode 100644 index 000000000..c4192f61f --- /dev/null +++ b/packer/telemetry_test.go @@ -0,0 +1,32 @@ +package packer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFlattenConfigKeys_nil(t *testing.T) { + f := flattenConfigKeys(nil) + assert.Zero(t, f, "Expected empty list.") +} + +func TestFlattenConfigKeys_nested(t *testing.T) { + inp := make(map[string]interface{}) + inp["A"] = "" + inp["B"] = []string{} + + c := make(map[string]interface{}) + c["X"] = "" + d := make(map[string]interface{}) + d["a"] = "" + + c["Y"] = d + inp["C"] = c + + assert.Equal(t, + []string{"A", "B", "C/X", "C/Y/a"}, + flattenConfigKeys(inp), + "Input didn't flatten correctly.", + ) +}