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

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

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

  1. mutate { convert => { "mytimestamp" => "string" } }
  2. ruby {
  3. code => "
  4. require 'time'
  5. current_time = Date.parse(event.get('enddate'))
  6. new_time = current_time + 86400
  7. event.set('enddateone', new_time.iso8601(3))"
  8. }
  9. if "ABORTED" in [message] {
  10. if [enddateone] >= [mytimestamp] {
  11. mutate { add_field => { "number" => 4 } }}
  12. else {
  13. 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.

  1. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker10] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  2. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker4] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  3. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker7] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  4. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker0] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  5. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker12] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  6. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker5] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  7. [ERROR] 2023-04-13 13:52:37.082 [[main]>worker13] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  8. [ERROR] 2023-04-13 13:52:37.083 [[main]>worker6] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  9. [ERROR] 2023-04-13 13:52:37.084 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
  10. [ERROR] 2023-04-13 13:52:37.084 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  11. [ERROR] 2023-04-13 13:52:37.086 [[main]>worker9] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  12. [ERROR] 2023-04-13 13:52:37.086 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  13. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  14. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
  15. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker11] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  16. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
  17. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
  18. [ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of NilClass into String

The logstash configurations

  1. input {
  2. file {
  3. path => "/path"
  4. start_position => "beginning"
  5. mode => "read"
  6. sincedb_path => "/dev/null"
  7. }
  8. }
  9. filter {
  10. grok {
  11. match => { "message" => "%{DATA}%{NUMBER:SN}%{DATA}%{NUMBER:ID}%{DATA}%{WORD:Source}%{DATA}%{TIMESTAMP_ISO8601:Start_Time}%{DATA}%{TIMESTAMP_ISO8601:End_Time}%{DATA}%{WORD:Status}" }
  12. }
  13. if "FINISHED" in [message] {
  14. mutate {add_field => { "number" => 1 } } }
  15. if "RUNNING" in [message] {
  16. mutate { add_field => { "number" => 3 } }}
  17. date {
  18. match => [ "End_Time", "yyyy-MM-dd HH:mm" ]
  19. target => "enddate"}
  20. date {
  21. match => [ "@timestamp", "yyyy-MM-dd HH:mm" ]
  22. target => "mytimestamp" }
  23. # mutate { rename => { "mytimestamp" => "@timestamp" } }
  24. mutate { convert => { "mytimestamp" => "string" } }
  25. ruby {
  26. code => "
  27. require 'time'
  28. current_time = Date.parse(event.get('enddate'))
  29. new_time = current_time + 86400
  30. event.set('enddateone', new_time.iso8601(3))"
  31. }
  32. if "ABORTED" in [message] {
  33. if [enddateone] >= [mytimestamp] {
  34. mutate { add_field => { "number" => 4 } }}
  35. else {
  36. mutate { add_field => { "number" => 2 } }}
  37. }
  38. }
  39. output {
  40. 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.

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

  1. ruby {
  2. code => '
  3. enddate = event.get("enddate")
  4. if enddate
  5. new_time = Time.at(enddate.to_f + 86400)
  6. event.set("enddateone", new_time.iso8601(3))
  7. end
  8. '
  9. }

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:

确定