Logstash date parse failure - ruby exception

Hi,

I am trying to use timestamp for each document by the value present in file name but i am getting Ruby exception occurred: wrong argument type DateTime (expected LogStash::Timestamp)

when i run ruby code. it is working fine but logstash throwing an exception.

data in csv file:
column1|column2
abcd|25
abcd|26
abcd|27
abcd|28

input {
file {
path => ["D:/test/test_2324_20220312111008.csv"]
start_position => "beginning"
}
}
filter {
csv {
separator => "|"
skip_header => true
columns => ["column1","column2"]
}
ruby {

        code => '
        require "date"                        
        time_end=event.get("path").to_s
        arr1=time_end.split("_")
        tend=arr1[1]
        arr2=arr1[2].split(".")
        event_time=arr2[0]
        event.set("tracking_header",tend)
        event.set("event_time",event_time)
        
        event_created = event_time[6,2]+"-"+event_time[4,2]+"-"+event_time[0,4]+" "+event_time[8,2]+":"+event_time[10,2]+":"+event_time[12,2]
        event.set("reported_time",event_created)                  
        
        zz = DateTime.new(event_time[0,4].to_i,event_time[4,2].to_i,event_time[6,2].to_i,event_time[8,2].to_i,event_time[10,2].to_i,event_time[12,2].to_i)
        event.set("@timestamp",zz)

        '
    }
}

output {
stdout {
codec => rubydebug
}
}

exception:

Ruby exception occurred: wrong argument type DateTime (expected LogStash::Timestamp)

Could someone please help on this.

Thanks in advance.

If you want to event.set [@timestamp] then feed it a Time object, not a DateTime object. Here, for example. I believe the constructor will do the work for you in that case.

1 Like

Thanks @Badger . it's working now.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.