grafana大坑,es找不到时间戳 | No date field named timestamp found

grafana大坑,es找不到时间戳。最近我这边的es重新装了一遍,结果发现grafana连不上elasticsearch了(以下简称es),排查问题查了好久一直以为是es没有装成功或者两边的版本不兼容,最后才发现是数值类型问题

一、【问题描述】

prometheus的监控方案一般会配合grafana使用,但是prometheus为了快速他只能推送数值,不能推送日志,

elasticsearch这个东西现在市场上用的比较多,用来监控日志或者推送一些字符串到grafana非常好用。

但是笔者前段时间踩了一个很大的坑,浪费了很多时间。

具体情况就是grafana配置数据源的时候,会出现如下图错误,但是es是很确定在运行的且有数据,也有timstamp这个项目

二、【问题原因】

这是因为当es的index没有创建的时候,第一次推送数据,es会根据你的数据来猜一个类型。

但是恰好,grafana的时间戳跟标准时间戳相比需要乘1000(毫秒时间戳),比如1721022514000。那么elasticsearch会把这个当作一个long类型,而不是date类型

但是grafana来找es里面的数据的时候,要求必须要是date类型,所以会出现es和grafana都正常运行的情况下,grafana不能正常添加数据源。

三、【解决方法】

写一个request,使用put方法更改指定的index的数据类型为date即可

或者保证创建index的第一条推送,是一个很容易辨别的date类型,比如2024-07-15 14:12:05

#!/bin/bash

# 设置Elasticsearch服务器URL
ES_URL="http://localhost:9200"

# 删除现有的模板(如果存在)
curl -X DELETE "$ES_URL/_template/helloworld_template" -H 'Content-Type: application/json'

# 创建新的模板,强制将timestamp字段设为date类型
curl -X PUT "$ES_URL/_template/helloworld_template" -H 'Content-Type: application/json' -d'
{
  "index_patterns": ["helloworld*"],
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date"
      }
    }
  }
}'

# 创建一个新的索引(根据模板自动应用映射设置)
curl -X PUT "$ES_URL/helloworld-0001" -H 'Content-Type: application/json'

# 添加一条文档
curl -X POST "$ES_URL/helloworld-0001/_doc" -H 'Content-Type: application/json' -d'
{
  "timestamp": "2024-07-15T12:34:56Z",
  "message": "Hello, World!"
}'

相关推荐

  1. habitat中的(一):训练模型的时候数据

    2024-07-17 20:50:03       39 阅读
  2. js怎样获取时间

    2024-07-17 20:50:03       32 阅读
  3. el-date-picker>时间单位

    2024-07-17 20:50:03       57 阅读
  4. 【Unity】Unity项目启动时报Git

    2024-07-17 20:50:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 20:50:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 20:50:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 20:50:03       57 阅读
  4. Python语言-面向对象

    2024-07-17 20:50:03       68 阅读

热门阅读

  1. 【工具使用】EMACS的verilog_mode脚本

    2024-07-17 20:50:03       20 阅读
  2. python初学之路

    2024-07-17 20:50:03       20 阅读
  3. 用C语言写的一个扫雷小游戏

    2024-07-17 20:50:03       19 阅读
  4. C#中处理Socket粘包

    2024-07-17 20:50:03       24 阅读