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

python 复制代码
#!/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!"
}'
相关推荐
花好月圆春祺夏安6 分钟前
基于odoo17的设计模式详解---装饰模式
数据库·python·设计模式
萧鼎2 小时前
深度探索 Py2neo:用 Python 玩转图数据库 Neo4j
数据库·python·neo4j
华子w9089258592 小时前
基于 Python Django 和 Spark 的电力能耗数据分析系统设计与实现7000字论文实现
python·spark·django
kikikidult2 小时前
(2025.07)解决——ubuntu20.04系统开机黑屏,左上角光标闪烁
笔记·ubuntu
ldj20202 小时前
2025 Centos 安装PostgreSQL
linux·postgresql·centos
Rockson2 小时前
使用Ruby接入实时行情API教程
javascript·python
G皮T3 小时前
【Elasticsearch】检索排序 & 分页
大数据·elasticsearch·搜索引擎·排序·分页·检索·深度分页
Tipriest_3 小时前
Python关键字梳理
python·关键字·keyword
码上淘金4 小时前
【Prometheus 】通过 Pushgateway 上报指标数据
prometheus
im_AMBER5 小时前
学习日志05 python
python·学习