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!"
}'
相关推荐
天上的光14 小时前
3.python——数据类型转换
python
钱彬 (Qian Bin)14 小时前
一文掌握工业缺陷检测项目实战(Pytorch算法训练、部署、C++ DLL制作、Qt集成)
c++·pytorch·python·qt·实战·工业缺陷检测·faster rcnn
圆山猫14 小时前
[VF2] Boot Ubuntu和Debian发行版
ubuntu·postgresql·debian
feifeigo12314 小时前
在Ubuntu上配置Nginx实现开机自启功能
数据库·nginx·ubuntu
Python×CATIA工业智造14 小时前
Python迭代协议完全指南:从基础到高并发系统实现
python·pycharm
THMAIL14 小时前
机器学习从入门到精通 - Transformer颠覆者:BERT与预训练模型实战解析
python·随机森林·机器学习·分类·bootstrap·bert·transformer
0wioiw015 小时前
Python基础(①⑧Queue)
windows·python
Source.Liu15 小时前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
urhero15 小时前
Python 制作的一个小说在线阅读工具
python·免费·小说·应用软件·小说在线阅读·无广告
kaikaile199515 小时前
在Ubuntu平台搭建RTMP直播服务器使用SRS简要指南
linux·服务器·ubuntu