InfluxDB 2.0 - 如何将双精度转换为 dateTime:RFC3339

InfluxDB 2.0 - How to convert double to dateTime:RFC3339

我正在尝试 https://github.com/percona/mongodb_exporter 使用 InfluxDB 抓取工具。我发现它被插入为 double.

我的目标是获取最后一个数据点并将其转换为 dateTime:RFC3339

环境:

mongodb_exporter 指标

# 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

我试了很多方法,但都不行。

尝试 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()

错了。得到1970-01-01T00:00:01.611023449Z。在这种情况下应该是 2021-01-19T02:30:49+00:00

尝试 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()

错误:

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

尝试 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) }))

错误:

 runtime error @6:6-6:59: map: failed to evaluate map function: cannot convert float to time

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.

因为纪元时间是second,需要转换成nanosecond

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

完整查询

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()