# Logstash filter ruby create new event?

**URL:** https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377
**Category:** Logstash
**Created:** [August 14, 2015, 7:33am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377 "2015-08-14T07:33:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![quejinho](https://avatars.discourse-cdn.com/v4/letter/q/3bc359/32.png) [@quejinho](https://discuss.elastic.co/u/quejinho)
#### Post date: [August 14, 2015, 7:33am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/1 "2015-08-14T07:33:55Z")

</div>

I am planning to create a time difference betwen one logstash and another so in second one i made a ruby code to make a time difference. Although i am quite there i need to not add a field to an event but to create a new event with that how could i do it ?

I tried

code =\> '["delta\_time"] = Time.now.to\_i - event["@timestamp"].to\_i '

but it gives an error.

---

<div class="post-metadata">

### Author: ![Anmol\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Anmol\_Gupta](https://discuss.elastic.co/u/Anmol_Gupta)
#### Post date: [October 19, 2015, 5:31am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/2 "2015-10-19T05:31:05Z")

</div>

So were you able to do it ? i have a similar usecase can i get your help?

---

<div class="post-metadata">

### Author: ![german23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/german23/32/11052_2.png) [@german23](https://discuss.elastic.co/u/german23)
#### Post date: [October 19, 2015, 5:45am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/3 "2015-10-19T05:45:17Z")

</div>

hi, try:

ruby {  
code =\> "event['delta\_time'] = Time.now.to\_i - event['@timestamp'].to\_i"  
}

that should be at least the right logstash syntax

---

<div class="post-metadata">

### Author: ![Anmol\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Anmol\_Gupta](https://discuss.elastic.co/u/Anmol_Gupta)
#### Post date: [October 19, 2015, 5:47am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/4 "2015-10-19T05:47:41Z")

</div>

"Time.now.to\_i " what does it tell? can you share your config file? actually i want calculate time difference between each event so that i can see which even had maximum gap and should look into it.  
Please help

---

<div class="post-metadata">

### Author: ![Anmol\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Anmol\_Gupta](https://discuss.elastic.co/u/Anmol_Gupta)
#### Post date: [October 19, 2015, 7:06am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/5 "2015-10-19T07:06:50Z")

</div>

ok i got it what it does. can we subtract present event with last event time difference?

---

<div class="post-metadata">

### Author: ![Anmol\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Anmol\_Gupta](https://discuss.elastic.co/u/Anmol_Gupta)
#### Post date: [October 20, 2015, 7:43am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/6 "2015-10-20T07:43:34Z")

</div>

i am facing an issue that when i take timestamp (one of the field in my logs) when i use to\_i with it it takes just year .  
"time" =\> 2015,  
when my config was : event['time'] = event['timestamp'].to\_i

so result is coming wrong

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 5:26am UTC](https://discuss.elastic.co/t/logstash-filter-ruby-create-new-event/27377/7 "2017-07-06T05:26:05Z")

</div>


