首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何计算应用程序端生成的时间日志消息与弹性搜索所占用的时间之间的延迟?

如何计算应用程序端生成的时间日志消息与弹性搜索所占用的时间之间的延迟?
EN

Stack Overflow用户
提问于 2022-06-18 19:46:52
回答 1查看 185关注 0票数 0

Elasticsearch专家,需要您的帮助才能实现以下提到的目标。

目标:

为了寻找一种计算时间差的方法,日志消息是在应用程序端(@time戳字段)和时间之间生成的,它被吸收到弹性搜索(ingest_time字段)中。

电流设置:

  1. 我正在使用FluentD捕获日志并发送给Kafka。然后,我使用Kafka connector (Elasticsearch连接器)将日志发送到Elasticsearch。由于在FluentD和Elasticsearch之间有一层卡夫卡,所以我想计算日志消息生成时间和摄入时间之间的滞后。

  1. 日志消息生成时间存储在日志的时间戳字段中,并在应用程序生成日志时完成。PFB日志消息如何看待卡夫卡主题结束。

代码语言:javascript
运行
复制
{
  "message": "ServiceResponse - Throwing non 2xx response",
  "log_level": "ERROR",
  "thread_id": "http-nio-9033-exec-21",
  "trace_id": "86d39fbc237ef7f8",
  "user_id": "85355139",
  "tag": "feedaggregator-secondary",
  "@timestamp": "2022-06-18T23:30:06+0530"
}

  1. I创建了一个吞食管道,将ingest_time字段添加到插入到Elasticsearch索引.

的每个文档中。

代码语言:javascript
运行
复制
PUT _ingest/pipeline/ingest_time
{
  "description": "Add an ingest timestamp",
  "processors": [
    {
      "set": {
        "field": "_source.ingest_time",
        "value": "{{_ingest.timestamp}}"
      }
    }]
}

  1. 一旦使用Kafka连接(ES接收器连接器)从Kafka插入索引,这就是我在Kibana上的JSON格式的消息。

代码语言:javascript
运行
复制
{
  "_index": "feedaggregator-secondary-2022-06-18",
  "_type": "_doc",
  "_id": "feedaggregator-secondary-2022-06-18+2+7521337",
  "_version": 1,
  "_score": null,
  "_source": {
  "thread_id": "http-nio-9033-exec-21",
  "trace_id": "86d39fbc237ef7f8",
  "@timestamp": "2022-06-18T23:30:06+0530",
  "ingest_time": "2022-06-18T18:00:09.038032Z",
  "user_id": "85355139",
  "log_level": "ERROR",
  "tag": "feedaggregator-secondary",
  "message": "ServiceResponse - Throwing non 2xx response"
  },

  "fields": {
    "@timestamp": [
      "2022-06-18T18:00:06.000Z"
]
  },

"sort": [
  1655574126000
  ]
}

  1. 现在,我想计算@时间戳字段和ingest_time字段之间的差异。为此,我在ingest管道中添加了一个脚本,该脚本添加了一个字段lag_seconds,并将其设置为ingest_time和@时间戳字段之间的差异。

代码语言:javascript
运行
复制
PUT _ingest/pipeline/calculate_lag
{
  "description": "Add an ingest timestamp and calculate ingest lag",
  "processors": [
    {
      "set": {
        "field": "_source.ingest_time",
        "value": "{{_ingest.timestamp}}"
      }
    },
    {
      "script": {
        "lang": "painless",
        "source": """
            if(ctx.containsKey("ingest_time") && ctx.containsKey("@timestamp")) {
              ctx['lag_in_seconds'] = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@timestamp']), ZonedDateTime.parse(ctx['ingest_time']))/1000;
            }
        """
      }
    }
  ]
}

错误:,但是由于我的、ingest_time、@时间戳字段的格式不同,所以出现了错误DateTimeParseException。

代码语言:javascript
运行
复制
{
  "error": {
    "root_cause": [
      {
        "type": "exception",
        "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2022-06-18T23:30:06+0530' could not be parsed, unparsed text found at index 22];",
        "header": {
          "processor_type": "script"
        }
      }
    ],
    "type": "exception",
    "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2022-06-18T23:30:06+0530' could not be parsed, unparsed text found at index 22];",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "ScriptException[runtime error]; nested: DateTimeParseException[Text '2022-06-18T23:30:06+0530' could not be parsed, unparsed text found at index 22];",
      "caused_by": {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)",
          "java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)",
          "java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)",
          "java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)",
          "ctx['lag_in_seconds'] = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@timestamp']), ZonedDateTime.parse(ctx['ingest_time']))/1000;\n            }",
          "                                                                         ^---- HERE"
        ],
        "script": "            if(ctx.containsKey(\"ingest_time\") && ctx.containsKey(\"@timestamp\")) {\n              ctx['lag_in_seconds'] = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@timestamp']), ZonedDateTime.parse(ctx['ingest_time']))/1000;\n            }",
        "lang": "painless",
        "caused_by": {
          "type": "date_time_parse_exception",
          "reason": "Text '2022-06-18T23:30:06+0530' could not be parsed, unparsed text found at index 22"
        }
      }
    },
    "header": {
      "processor_type": "script"
    }
  },
  "status": 500
}

因此,需要您的帮助才能在@时间戳和ingest_time字段之间找到ingest_time。

使用AWS (Opensearch)的托管弹性搜索版本- 7.1

EN

回答 1

Stack Overflow用户

发布于 2022-06-19 07:47:39

我可以看到@时间戳字段的Java日期解析问题。ctx['@timestamp']将返回值"2022-06-18T23:30:06+0530",这是一个ISO_OFFSET_DATE_TIME。您需要使用OffsetDateTime.parse(ctx['@timestamp'])解析它。或者,您可以尝试从字段块访问@时间戳。您可以在https://howtodoinjava.com/java/date-time/zoneddatetime-parse/上阅读Java的日期解析。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72672283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档