diff --git a/pkg/logentry/stages/util.go b/pkg/logentry/stages/util.go index ee340e4bfe5e629951669fa4c1a8e5f5074b9f32..95a94b6e37198e06f25ad9f0f69b5f4480ce6d1f 100644 --- a/pkg/logentry/stages/util.go +++ b/pkg/logentry/stages/util.go @@ -85,9 +85,9 @@ func getString(unk interface{}) (string, error) { switch i := unk.(type) { case float64: - return fmt.Sprintf("%f", i), nil + return strconv.FormatFloat(i, 'f', -1, 64), nil case float32: - return fmt.Sprintf("%f", i), nil + return strconv.FormatFloat(float64(i), 'f', -1, 32), nil case int64: return strconv.FormatInt(i, 10), nil case int32: diff --git a/pkg/logentry/stages/util_test.go b/pkg/logentry/stages/util_test.go index 26bd2f76834dffeff92c275ea2818a892f536480..06f8f46cd0644476141d666669669f433b48ff94 100644 --- a/pkg/logentry/stages/util_test.go +++ b/pkg/logentry/stages/util_test.go @@ -37,3 +37,31 @@ func assertLabels(t *testing.T, expect map[string]string, got model.LabelSet) { assert.Equal(t, model.LabelValue(v), gotV, "mismatch label value") } } + +// Verify the formatting of float conversion to make sure there are not any trailing zeros, +// and also make sure unix timestamps are converted properly +func TestGetString(t *testing.T) { + var f64, f64_1 float64 + var f32 float32 + f64 = 1 + f64_1 = 1562723913000 + f32 = 2.02 + s64, err := getString(f64) + if err != nil { + t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err) + return + } + s64_1, err := getString(f64_1) + if err != nil { + t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err) + return + } + s32, err := getString(f32) + if err != nil { + t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err) + return + } + assert.Equal(t, "1", s64) + assert.Equal(t, "2.02", s32) + assert.Equal(t, "1562723913000", s64_1) +}