Fixing nil crash on json stats print
@@ -192,17 +192,25 @@ func printCallback(stats clistats.StatisticsClient) {
func printCallbackJSON(stats clistats.StatisticsClient) {
builder := &strings.Builder{}
- _ = json.NewEncoder(builder).Encode(metricsMap(stats))
+ if err := json.NewEncoder(builder).Encode(metricsMap(stats)); err == nil {
fmt.Fprintf(os.Stderr, "%s", builder.String())
}
+}
func metricsMap(stats clistats.StatisticsClient) map[string]interface{} {
results := make(map[string]interface{})
- startedAt, _ := stats.GetStatic("startedAt")
- duration := time.Since(startedAt.(time.Time))
+ var (
+ startedAt time.Time
+ duration time.Duration
+ )
+
+ if stAt, ok := stats.GetStatic("startedAt"); ok {
+ startedAt = stAt.(time.Time)
+ duration = time.Since(startedAt)
+ }
- results["startedAt"] = startedAt.(time.Time)
+ results["startedAt"] = startedAt
results["duration"] = fmtDuration(duration)
templates, _ := stats.GetStatic("templates")
results["templates"] = clistats.String(templates)
