ruby -发生Ruby异常:没有LogStash::Timestamp到String的隐式转换

e3bfsja2  于 2023-04-18  发布在  Logstash
关注(0)|答案(1)|浏览(277)

我有一个文件每5分钟存储在弹性数据库索引中,文件中的每一行都包含一个开始和结束日期,我做了配置来处理日期并计算结束日期的1天,所以当我添加一天时,我遇到了这个错误。

[ERROR] 2023-04-13 13:52:37.082 [[main]>worker10] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker4] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker7] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker0] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker12] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker5] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker13] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.083 [[main]>worker6] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.084 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.084 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.086 [[main]>worker9] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.086 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker11] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of NilClass into String

logstash配置

input {
  file {
    path => "/path"
    start_position => "beginning"
    mode => "read"
    sincedb_path => "/dev/null"
  }
}
filter {
    grok {
      match => { "message" => "%{DATA}%{NUMBER:SN}%{DATA}%{NUMBER:ID}%{DATA}%{WORD:Source}%{DATA}%{TIMESTAMP_ISO8601:Start_Time}%{DATA}%{TIMESTAMP_ISO8601:End_Time}%{DATA}%{WORD:Status}" }
    }
      if "FINISHED" in [message] {
          mutate {add_field  => { "number" => 1 } } }
      if "RUNNING" in [message] {
          mutate { add_field => { "number" => 3 } }}
    date {
      match => [ "End_Time", "yyyy-MM-dd HH:mm" ]
      target => "enddate"}
    date {
      match => [ "@timestamp", "yyyy-MM-dd HH:mm" ]
      target => "mytimestamp" }
   # mutate { rename => { "mytimestamp" => "@timestamp" } }
    mutate { convert => { "mytimestamp" => "string" } }
    ruby {
       code => "
          require 'time'
          current_time = Date.parse(event.get('enddate'))
          new_time = current_time + 86400
          event.set('enddateone', new_time.iso8601(3))"
      }
      if "ABORTED" in [message]  {
        if [enddateone] >= [mytimestamp] {
          mutate { add_field => { "number" => 4 } }}
        else {
          mutate { add_field => { "number" => 2 } }}
  }
}
output {
  elasticsearch {}
p4rjhz4m

p4rjhz4m1#

如果grok不匹配,则[End_Time]字段将不存在。当源字段不存在时,日期过滤器是无操作的,因此不会创建[enddate]字段。

current_time = Date.parse(event.get('enddate'))

parse需要一个字符串。如果[enddate]字段不存在,那么event.get将返回nil,并且错误消息告诉你Ruby不愿意将nil转换为字符串。
如果[enddate]字段确实存在,那么event.get将返回一个LogStash::Timestamp对象,并且同样没有隐式转换。
您根本不需要使用Date.parse。

ruby {
  code => '
    enddate = event.get("enddate")
    if enddate
      new_time = Time.at(enddate.to_f + 86400)
      event.set("enddateone", new_time.iso8601(3))
    end
  '
}

相关问题