目前您所拥有的是消息字段中的整个 json 作为字符串。因此,Elastic 无法对其应用任何数学运算。您需要做的是使用 fluentd 将日志行解析为 json,因此在 Elastic 文档中,该 json 中的每个字段(如记录器和级别)都是弹性文档的一部分。
一旦你有了 Elastic,大部分情况下会自动解释 executionTime 是数字并使其可用于聚合。之后,您将在 Grafana 下拉菜单中看到该字段。
Here你可以在_source字段上了解更多。
将您的原始日志行也添加到问题中,我认为这可能有助于了解您想要摄取的内容,以便可以就可能的 fluentd 配置提出建议。
根据提供的其他信息更新答案
为简单起见,我使用 docker setup 来运行和解析问题中提供的日志模式。
Fluentd 配置
我使用了 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>
Fluent 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 编写
您可以选择只运行一个 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
- cluster.initial_master_nodes=es01,es02,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,es02,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 索引管理中更改字段类型和格式。