0
点赞
收藏
分享

微信扫一扫

Grafana :利用Explore方式实现多条件查询

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!"
}'

举报

相关推荐

0 条评论