Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

prometheus - InfluxDB 2.0 - How to convert double to dateTime:RFC3339

I'm trying https://github.com/percona/mongodb_exporter with InfluxDB scraper. I found out it's inserted as double.

My goal is to get last data point and convert it to dateTime:RFC3339.

ENV:

  • influxdb:v2.0.1
  • mongodb_exporter:v0.20.1

mongodb_exporter metrics

# HELP mongodb_mongod_replset_member_election_date The timestamp the node was elected as replica leader
# TYPE mongodb_mongod_replset_member_election_date gauge
mongodb_mongod_replset_member_election_date{name="mongo-sh0-0.mongo-sh0.default.svc.cluster.local:27017",set="rs0",state="PRIMARY"} 1.611023449e+09

I tried many ways but all of them don't work.

Try 1

from(bucket: "mongo-swag")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mongodb_mongod_replset_member_election_date")
  |> filter(fn: (r) => r["_field"] == "gauge")
  |> last()
  |> toInt()
  |> toTime()

Wrong. Get 1970-01-01T00:00:01.611023449Z. It should be 2021-01-19T02:30:49+00:00 in this case.

Try 2

from(bucket: "mongo-swag")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mongodb_mongod_replset_member_election_date")
  |> filter(fn: (r) => r["_field"] == "gauge")
  |> last()
  |> toString()
  |> toTime()

error:

 runtime error @8:6-8:14: toTime: failed to evaluate map function: parsing time "1611023449" as "2006-01-02": cannot parse "023449" as "-"

Try 3

from(bucket: "mongo-swag")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mongodb_mongod_replset_member_election_date")
  |> filter(fn: (r) => r["_field"] == "gauge")
  |> last()
  |> map(fn:(r) => ({ r with _value: time(v: r._value) }))

error:

 runtime error @6:6-6:59: map: failed to evaluate map function: cannot convert float to time
question from:https://stackoverflow.com/questions/65899429/influxdb-2-0-how-to-convert-double-to-datetimerfc3339

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/type-conversions/totime/

toTime() assumes all numeric input values are nanosecond epoch timestamps.

Because epoch time is second, need to be transformed to nanosecond.

map(fn: (r) => ({ r with _value: r._value * 1000 * 1000 * 1000 }))

Full Query

from(bucket: "mongo-swag")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mongodb_mongod_replset_member_election_date")
  |> last()
  |> toInt()
  |> map(fn: (r) => ({ r with _value: r._value * 1000 * 1000 * 1000 }))
  |> toTime()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...