diff --git a/CHANGELOG.md b/CHANGELOG.md index 999630aa2a31..a9d5366e766e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Main * [5302](https://github.com/grafana/loki/pull/5302) **MasslessParticle** Update azure blobstore client to use new sdk. +* [5243](https://github.com/grafana/loki/pull/5290) **ssncferreira**: Update Promtail to support duration string formats. * [5266](https://github.com/grafana/loki/pull/5266) **jeschkies**: Write Promtail position file atomically on Unix. * [5280](https://github.com/grafana/loki/pull/5280) **jeschkies**: Fix Docker target connection loss. * [5243](https://github.com/grafana/loki/pull/5243) **owen-d**: moves `querier.split-queries-by-interval` to limits code only. diff --git a/clients/pkg/logentry/stages/metrics.go b/clients/pkg/logentry/stages/metrics.go index 2079dadfdaed..4626485bba4d 100644 --- a/clients/pkg/logentry/stages/metrics.go +++ b/clients/pkg/logentry/stages/metrics.go @@ -311,7 +311,7 @@ func getFloat(unk interface{}) (float64, error) { case uint: return float64(i), nil case string: - return strconv.ParseFloat(i, 64) + return getFloatFromString(i) case bool: if i { return float64(1), nil @@ -321,3 +321,22 @@ func getFloat(unk interface{}) (float64, error) { return math.NaN(), fmt.Errorf("can't convert %v to float64", unk) } } + +// getFloatFromString converts string into float64 +// Two types of string formats are supported: +// * strings that represent floating point numbers, e.g., "0.804" +// * duration format strings, e.g., "0.5ms", "10h". +// Valid time units are "ns", "us", "ms", "s", "m", "h". +// Values in this format are converted as a floating point number of seconds. +// E.g., "0.5ms" is converted to 0.0005 +func getFloatFromString(str string) (float64, error) { + dur, err := strconv.ParseFloat(str, 64) + if err != nil { + dur, err := time.ParseDuration(str) + if err != nil { + return 0, err + } + return dur.Seconds(), nil + } + return dur, nil +} diff --git a/clients/pkg/logentry/stages/metrics_test.go b/clients/pkg/logentry/stages/metrics_test.go index c5ed7915273e..dd151a9549b2 100644 --- a/clients/pkg/logentry/stages/metrics_test.go +++ b/clients/pkg/logentry/stages/metrics_test.go @@ -321,8 +321,9 @@ func TestMetricStage_Process(t *testing.T) { "contains_false": "contains(keys(@),'nope')", }, } + regexHTTPFixture := `11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] "GET /1986.js HTTP/1.1" 200 932ms"` regexConfig := map[string]interface{}{ - "expression": "(?P\"GET).*HTTP/1.1\" (?P\\d*) (?P