json_exporter icon indicating copy to clipboard operation
json_exporter copied to clipboard

Parse date type value

Open pinpox opened this issue 3 years ago • 4 comments

I saw there have been related issues to this, but wasn't able to find a working solution.

I'd like to extract a date as value, not as label. The date is ISO formatted, but with the correct parsing it should be possible to show it as a strictly numerical value, i.e. in unix time (seconds integer).

My input json looks like this:

      "committer": {
        "name": "GitHub",
        "email": "[email protected]",
        "date": "2022-01-19T23:45:15Z"
      }

How can I get the value of {.date} in seconds?

I currently see the error:

json_exporter[139234]: level=error ts=2022-01-21T11:04:04.015Z caller=collector.go:63 msg="Failed to convert extracted value to float64" path="{ .commit.commit.committer.date }" value=2022-01-19T23:45:15Z err="strconv.ParseFloat: parsing \"2022-01-19T23:45:15Z\": invalid syntax; strconv.ParseBool: parsing \"2022-01-19T23:45:15Z\": invalid syntax"

pinpox avatar Jan 21 '22 11:01 pinpox

I have a similar problem here:

My dataset are:

[{
	"plan": "backup",
	"next_run": "2022-02-03T18:10:00Z",
	"last_run": "2022-02-02T18:10:00.000226728Z",
	"last_run_status": "200",
}]

And the error:

caller=collector.go:95 level=error msg="Failed to convert extracted value to float64" path={[].last_run} value=2022-02-02T18:10:00.000226728Z err="strconv.ParseFloat: parsing \"2022-02-02T18:10:00.000226728Z\": invalid syntax; strconv.ParseFloat: parsing \"2022-02-02T18:10:00.000226728Z\": invalid syntax

bodsch avatar Feb 03 '22 08:02 bodsch

This seems like a major limitation, parsing dates would be really useful and probably not that hard to implement?

Would it be possible to extend this function to also check for dates? It would just have to check after the other types if it can parse a date from the input and return it as unix time seconds

https://github.com/prometheus-community/json_exporter/blob/621139e1b25db96e37a4bfd41513d6dc2e178f8b/exporter/util.go#L41-L63

For parsing the date without knowing the format in advance there is: https://github.com/araddon/dateparse

I'd try to make a PR if you are interested in merging

pinpox avatar Feb 03 '22 08:02 pinpox

I'd try to make a PR if you are interested in merging

@pinpox imho, it will be really useful. I faced with similar issue with the following input date with timezones:

    "triggers": [
        {
            "group": "aggregator",
            "name": "aggregator_trigger",
            "previousFireTime": "2022-04-06T08:36:00.000+00:00",
            "nextFireTime": "2022-04-06T08:38:00.000+00:00",
            "priority": 5
        }
    ]

avegera avatar Apr 06 '22 08:04 avegera

// 使用 araddon/dateparse 库解析日期字符串 t, err := dateparse.ParseAny(s) if err == nil { return float64(t.Unix()), nil } resultErr = resultErr + "; " + fmt.Sprintf("%s", err)

1

ilanni2460 avatar Jun 30 '23 10:06 ilanni2460