add config key reporting

This commit is contained in:
Matthew Hooker 2017-11-02 23:45:01 -07:00
parent 3e3768ec3e
commit a5197840df
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 61 additions and 10 deletions

View File

@ -91,16 +91,11 @@ func (c *CheckpointTelemetry) AddSpan(name, pluginType string, options interface
} }
log.Printf("[INFO] (telemetry) Starting %s %s", pluginType, name) 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{ ts := &TelemetrySpan{
Name: name, Name: name,
Type: pluginType, Options: flattenConfigKeys(options),
StartTime: time.Now().UTC(), StartTime: time.Now().UTC(),
Type: pluginType,
} }
c.spans = append(c.spans, ts) c.spans = append(c.spans, ts)
return ts return ts
@ -123,6 +118,8 @@ func (c *CheckpointTelemetry) Finalize(command string, errCode int, err error) e
extra.Error = err.Error() extra.Error = err.Error()
} }
params.Payload = extra params.Payload = extra
// b, _ := json.MarshalIndent(params, "", " ")
// log.Println(string(b))
ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
defer cancel() defer cancel()
@ -132,12 +129,12 @@ func (c *CheckpointTelemetry) Finalize(command string, errCode int, err error) e
} }
type TelemetrySpan struct { type TelemetrySpan struct {
Name string `json:"name"`
Type string `json:"type"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"` EndTime time.Time `json:"end_time"`
Error string `json:"error"` Error string `json:"error"`
Name string `json:"name"`
Options []string `json:"options"` Options []string `json:"options"`
StartTime time.Time `json:"start_time"`
Type string `json:"type"`
} }
func (s *TelemetrySpan) End(err error) { 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()) 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)
}

32
packer/telemetry_test.go Normal file
View File

@ -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.",
)
}