From 82fac990a1c757cc471e9d065b6dcdebedf6f919 Mon Sep 17 00:00:00 2001 From: Susana Ferreira Date: Thu, 3 Feb 2022 10:42:39 +0000 Subject: [PATCH] Update promtail to support duration string formats (#5290) * Update promtail to support duration string formats * Address comments --- CHANGELOG.md | 1 + clients/pkg/logentry/stages/metrics.go | 21 ++++++++++- clients/pkg/logentry/stages/metrics_test.go | 37 ++++++++++--------- .../clients/promtail/stages/metrics.md | 13 +++++++ 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 999630aa2a..a9d5366e76 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 2079dadfda..4626485bba 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 c5ed791527..dd151a9549 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