【问题标题】:Grafana with Elasticsearch - Does not show data when setting Group By AverageGrafana 与 Elasticsearch - 设置按平均值分组时不显示数据
【发布时间】:2021-01-17 04:08:45
【问题描述】:

使用 Grafana 7.2 和 Elasticsearch 7.5.1。

一切都在 Openshift 中启动并运行。 Elasticsearch 数据源已正确配置并创建了一个非常简单的仪表板。

从同样在 Openshift 中运行的 Springboot 服务,我使用 Fluentd 将日志发送到 Elasticsearch。

Elasticsearch 中存储的文档是这样的(取自 Grafana “Logs”结果面板):

已编辑:根据@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] 值。

问题一:

我需要做的(但我没有得到)是棘手的部分:我需要得到一个直方图,显示每个间隔的 executionTime 字段值的平均值。

按照官方文档,特别是this official video from Grafana,我应该能够将 de Group By 字段更改为 Average 并从字段选择器中选择 @value。不幸的是,那个@value 值没有出现在那里(_source = [object Object] 字段可能有什么事情要做吗?)

问题 2:

另一个疑问是查询字段在该格式中是否有效,或者访问 executionTime 字段的方式是什么,该字段位于 message 字段内弹性搜索文档。在某种层次结构中message -> onpay -> executionTime

Fluentd 配置文件:

  <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>

【问题讨论】:

    标签: elasticsearch logstash grafana fluentd


    【解决方案1】:

    目前您所拥有的是消息字段中的整个 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 索引管理中更改字段类型和格式。

    【讨论】:

    • 感谢@karan 的回答。刚刚用原始日志行编辑了问题。只是为了确定一个问题:你的前两段确实回答了两个不同的事情,对吧?第一个指向解决方案,第二个只是回答我对 _source 字段的看法。但是主要故事的解决方案将仅包含在您的主要解释(第一段)中,对吗?现在弄清楚在 Fluentd 中配置什么以使我的消息中的字段成为主要的 Elasticsearch 文档字段
    • 是的@EIPiter,第一点回答了你的主要问题,第二点只是从理解的角度来看。查看您的源日志,我认为它只是通过 fluentd 正确获取您的摄取。根据您的操作方式,您将在弹性文档中获得 onpay.executionTimeexecutionTime,之后可以从 Grafana 访问它。
    • 非常感谢。相信我,我正在阅读很多关于如何告诉 Fluentd 在我的 Elastic 文档中获取 executionTime 的官方文档。但仍然没有得到它:(。我会继续努力......但如果你有线索,将不胜感激:)
    • 你能分享你的流利配置吗?我已经在 docker 中设置了 Elastic,将启动一个 fluentd 容器,看看我是否可以帮助您确定确切的配置。
    • 嗨,我已经简化了配置文件,删除了一些不必要的字段。无论如何,结果还是一样的:(。有什么线索吗?谢谢!!
    猜你喜欢
    • 2020-02-09
    • 2018-05-12
    • 1970-01-01
    • 2018-10-18
    • 2021-06-17
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多