Ruby异常发生:无法将LogStash::Timestamp隐式转换为字符串。

huangapple go评论69阅读模式
英文:

ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String

问题

以下是您要翻译的内容:

"I have a file stored every 5 mins in elastic DB index, and each line in the file contains a start and end date, I made the configuration to handle the dates and calculate 1 day on the end date, so when I added a day I faced this error."

"The logstash configurations"

"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 {}"

英文:

I have a file stored every 5 mins in elastic DB index, and each line in the file contains a start and end date, I made the configuration to handle the dates and calculate 1 day on the end date, so when I added a day I faced this error.

[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 

The logstash configurations

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

答案1

得分: 1

If the grok does not match, then the [End_Time] field will not exist. When the source field does not exist the date filter is a no-op, so the [enddate] field will not be created.

如果 grok 不匹配,那么 [End_Time] 字段将不存在。当源字段不存在时,日期过滤器不起作用,因此不会创建 [enddate] 字段。

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

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

Date.parse expects a string. If the [enddate] field does not exist then event.get will return nil, and the error message is telling you that Ruby is not willing to convert that nil to a string for you.

Date.parse 需要一个字符串。如果 [enddate] 字段不存在,那么 event.get 将返回 nil,错误消息告诉您 Ruby 不愿意为您将 nil 转换为字符串。

If the [enddate] field does exist then event.get will return a LogStash::Timestamp object, and again there is no implicit conversion of that.

如果 [enddate] 字段存在,则 event.get 将返回一个 LogStash::Timestamp 对象,同样没有隐式的转换。

You do not need to use Date.parse at all.

您根本不需要使用 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
'
}

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

英文:

If the grok does not match, then the [End_Time] field will not exist. When the source field does not exist the date filter is a no-op, so the [enddate] field will not be created.

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

Date.parse expects a string. If the [enddate] field does not exist then event.get will return nil, and the error message is telling you that Ruby is not willing to convert that nil to a string for you.

If the [enddate] field does exist then event.get will return a LogStash::Timestamp object, and again there is no implicit conversion of that.

You do not need to use Date.parse at all.

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

huangapple
  • 本文由 发表于 2023年4月13日 19:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定