python - 如何使用时间字段将指标数据添加到 influxdb?

标签 python python-2.7 metrics influxdb grafana

我正在使用以下代码行在 influxDB 中添加指标数据。

    def add_job_influx_db_metrics(tags_dict={}, values_dict={}, measurement='test'):
       influxdb_client = InfluxDB.get_connection()
       db_name = "mydb"
       influxdb_client.switch_database(database=db_name)
       current_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',time.localtime(time.time()))
       json_body = [
        {
          "measurement": measurement,
          "tags": tags_dict,
          "time": current_time,
          "fields": values_dict
         }
       ]
      response = influxdb_client.write_points(points=json_body)
      print "write_operation response", response

当我将它与 Grafana 集成时,数据没有出现,但是当我在 127.0.0.1:8083 上检查它时,它显示时间是 1970-01-01T00: 00:00Z。可能默认为起始纪元时间。 我想采用 "2015-8-19T07:15:00Z" 形式的时间。 influxdb(python客户端)如何获取time字段,什么是timePrecision?

最佳答案

 from datetime import datetime
 current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

并在 json_body 中像这样使用“时间”-

"time": current_time

有效。

关于python - 如何使用时间字段将指标数据添加到 influxdb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090883/

相关文章:

python - 基本的python模块设计问题

python - 如何从包含日期和列表中其他字符串的字符串列表中解析月份?

django - (fields.E300) 字段定义与模型的关系,该模型要么未安装,要么是抽象的

mysql - 使用 Datadog 监控 Aurora 未显示查询量的变化

python - 从 Python 列表向 PySpark DataFrame 添加新列

python - emailmultialternatives 在 django 中附加文件和 html 内容

Python:填充空心物体

python - 不同数字列表中的第一个公共(public)元素

css - 跟踪 CSS 框架的使用

java - 用于测量代码行的 Eclipse 插件