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 分钟前
甘肃旅游网站研究与实现(阿龙原创设计,全网最新,旅游网站,甘肃旅游,全国旅游)✅
java·python·小程序·flask·毕业设计·课程设计·旅游
xiaoyi_devil16 分钟前
从blob 下载zip文件到本地并解压
服务器·网络·python
伊织code34 分钟前
python-pptx - Python 操作 PPT 幻灯片
开发语言·python·powerpoint
金灰36 分钟前
scrapy--子类CrawlSpider&中间件
开发语言·网络·python·scrapy·安全·中间件
Uncle 城42 分钟前
如何使用python抓包,附代码
开发语言·网络·python·ai
南柯一梦梦红尘1 小时前
java.io.FileNotFoundException open failed: EACCES (Permission denied)
java·开发语言·python
funcdefmain1 小时前
df保存为excel
python·excel·pandas
心死翼未伤1 小时前
python从入门到精通:数据可视化-图形开发
开发语言·数据结构·python·信息可视化·json·echarts·matplotlib
天涯幺妹2 小时前
Python网络爬虫模拟登录与验证解析
爬虫·python·scrapy·网络安全·pycharm·beautifulsoup·pygame
一禅(OneZen)2 小时前
【L1.第四章】 Appium Inspector 自动化用例录制
python·selenium·appium·自动化·pytest