如何解决具有Elasticsearch的Grafana-设置“按平均值分组”时不显示数据
使用Grafana 7.2和Elasticsearch 7.5.1。
一切都已在Openshift中启动并运行。 Elasticsearch数据源已正确配置,并创建了一个非常简单的仪表板。
从也在Openshift中运行的Springboot服务中,我正在使用Fluentd将日志发送到Elasticsearch。
Elasticsearch中存储的文档是这样的(取自Grafana“日志”结果面板):
编辑:按照@karan shah的建议,我添加了通过Fluentd发送到Elastichsearch的原始日志:
{
"onpay":{
"traceId":"9999","inout":"OUT","startTime":"2020-10-01T10:13:43.806+0200","finishTime":"2020-10-01T10:13:43.827+0200","executionTime":21.0,"entrySize":124.0,"exitSize":124.0,"differenceSize":0.0,"user":"pgallello","methodPath":"http://localhost:8083/api/serviceEntryPoint","errorMessage":null,"className":"com.myorganization.mypackage.MyController","methodName":"serviceTemplateEntryPoint"
}
}
这是一个Elasticsearch文档,其中包含“消息”字段,这是我要基于其仪表板的文档。 请注意两点:
- 该字段以红色标记: executionTime 。
- 字段_source仅有一个[object Object]值。
问题1:
我需要做的(但我没有得到)是棘手的部分:我需要获得一个直方图,以显示每个时间间隔的executeTime字段的平均值。
按照官方文档,尤其是this official video from Grafana,我应该能够将“分组依据”字段更改为“平均值”,并从字段选择器中选择@value。不幸的是,@value值没有出现在这里(_source = [object Object]
字段有事要做吗?)
问题2:
另一个疑问是,“查询”字段在该格式下是否有效,或者以何种方式访问 executionTime 字段,该字段位于消息字段的内部, Elasticsearch文件。在一种层次结构message -> onpay -> executionTime
中。
有效的配置文件:
<source>
@type forward
port 24224
bind "0.0.0.0"
</source>
<filter onpayapp.**>
@type parser
key_name "onpayapp"
reserve_data true
<parse>
@type "json"
</parse>
</filter>
<match onpay.**>
@type copy
<store>
@type "elasticsearch"
host "elasticdb"
port 9200
logstash_format true
logstash_prefix "applogs"
logstash_dateformat "%Y%m%d"
include_tag_key true
type_name "app_log"
tag_key "@log_name"
flush_interval 1s
<parse>
@type json
</parse>
<buffer>
flush_interval 1s
</buffer>
</store>
<store>
@type "stdout"
</store>
</match>
解决方法
目前,您所拥有的是整个json字符串,在消息字段中。因此,Elastic将无法对其应用任何数学运算。您需要做的是使用fluentd将日志行解析为json,因此在Elastic文档中,该json中的每个字段(例如logger和level)都是Elastic文档的一部分。 一旦有了弹性,Elastic就会自动将执行时间解释为数字并将其用于聚合。之后,您将在Grafana下拉列表中看到该字段。
Here,您可以在_source字段上了解更多信息。
在问题中添加原始日志行,我认为这可能有助于了解您要摄取的内容,以便对可能的流利配置提出建议。
根据提供的其他信息更新了答案
为简单起见,我使用docker setup运行并解析问题中提供的日志模式。
有效配置
我使用了HTTP输入,因此可以卷曲,但是您可以切换回转发器。 我已删除过滤器,因为我假设您的源已经是JSON,因此您无需将其解析为JSON。 如果您通过管道处理了多种类型的数据,则可以重新添加匹配模式。
<source>
@type http
port 9880
bind 0.0.0.0
</source>
<match *>
@type copy
<store>
@type "elasticsearch"
host "es01"
port 9200
logstash_format true
logstash_prefix "applogs"
logstash_dateformat "%Y%m%d"
include_tag_key true
type_name "app_log"
tag_key "@log_name"
flush_interval 1s
<parse>
@type json
</parse>
<buffer>
flush_interval 1s
</buffer>
</store>
<store>
@type "stdout"
</store>
</match>
流利的Docker映像
# fluentd/Dockerfile
FROM fluent/fluentd:v1.11-debian-1
USER root
RUN touch ~/.gemrc
RUN echo ':ssl_verify_mode: 0' >> ~/.gemrc
RUN buildDeps="sudo make gcc g++ libc-dev" \
&& apt-get update \
&& apt-get install -y --no-install-recommends $buildDeps \
&& sudo gem install fluent-plugin-elasticsearch \
&& sudo gem sources --clear-all \
&& SUDO_FORCE_REMOVE=yes \
apt-get purge -y --auto-remove \
-o APT::AutoRemove::RecommendsImportant=false \
$buildDeps \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem
USER fluent
Docker Compose 您可以选择仅运行elasticsearch的一个节点。我已经在运行此设置。
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
healthcheck:
interval: 20s
retries: 10
test: curl -s http://localhost:9200/_cluster/health | grep -vq '"status":"red"'
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
ports:
- 9201:9200
networks:
- elastic
healthcheck:
interval: 20s
retries: 10
test: curl -s http://localhost:9201/_cluster/health | grep -vq '"status":"red"'
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
ports:
- 9202:9200
networks:
- elastic
healthcheck:
interval: 20s
retries: 10
test: curl -s http://localhost:9202/_cluster/health | grep -vq '"status":"red"'
kib01:
image: docker.elastic.co/kibana/kibana:7.8.0
container_name: kib01
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
networks:
- elastic
healthcheck:
interval: 10s
retries: 20
test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:5601/api/status
fluentd:
build: ./fluentd
volumes:
- "./fluentd/conf/:/fluentd/etc/:ro"
networks:
- elastic
ports:
- "9880:9880"
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridge
测试卷毛
curl -X POST -d 'json={ "onpay": { "traceId": "9999","inout": "OUT","startTime": "2020-10-01T10:13:43.806+0200","finishTime": "2020-10-01T10:13:43.827+0200","executionTime": 21.0,"entrySize": 124.0,"exitSize": 124.0,"differenceSize": 0.0,"user": "pgallello","methodPath": "http://localhost:8083/api/serviceEntryPoint","errorMessage": null,"className": "com.myorganization.mypackage.MyController","methodName": "serviceTemplateEntryPoint" }}' http://localhost:9880/
一旦您像这样摄取了所有json键,Elastic就会自动映射大多数字段,并允许根据字段类型进行搜索,聚合等。您可以根据需要从kibana索引管理中更改字段类型和格式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。