# Calculate time difference between 2 events with unique id

**URL:** https://discuss.elastic.co/t/calculate-time-difference-between-2-events-with-unique-id/321388
**Category:** Logstash
**Created:** [December 16, 2022, 8:40am UTC](https://discuss.elastic.co/t/calculate-time-difference-between-2-events-with-unique-id/321388 "2022-12-16T08:40:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)
#### Post date: [December 16, 2022, 8:40am UTC](https://discuss.elastic.co/t/calculate-time-difference-between-2-events-with-unique-id/321388/1 "2022-12-16T08:40:58Z")

</div>

Hi,

I have two events with same field idRda like the following, I need to calculate the difference between the timestamp

```auto
"message": "2022-12-13 14:52:00.399 {[ACTIVE] ExecuteThread: 5 for queue: weblogic.kernel.Default (self-tuning)} INFO idRda:[4040477] timestamp:[1670939520399]",
"message": "2022-12-13 14:52:06.203 {[ACTIVE] ExecuteThread: 12 for queue: weblogic.kernel.Default (self-tuning)} INFO idRda:[4040477] timestamp:[1670939526203]", 

```

I try to use aggregate filter but for the moment it doesn't work.

```auto
filter {

  aggregate {
    task_id => "%{idreport}"
    code => '
    map["firstEvent"] ||= event.get("timestamp")
    map["lastEvent"] = event.get("timestamp")'
    timeout_code => '
    require "time"
    starttime = Time.iso8601(event.get("firstEvent").to_s).to_f
    endtime = Time.iso8601(event.get("lastEvent").to_s).to_f
    event.set("overallTime", endtime - starttime)'
  }
}

```

Thank you  
Regards.

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [December 16, 2022, 5:48pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-2-events-with-unique-id/321388/2 "2022-12-16T17:48:28Z")

</div>

> [@yago82](#):
>
> ```auto
> starttime = Time.iso8601(event.get("firstEvent").to_s).to_f
> endtime = Time.iso8601(event.get("lastEvent").to_s).to_f
> 
> ```

Unless you are setting push\_map\_as\_event\_on\_timeout the event will not have those fields.

---

<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: [January 13, 2023, 5:48pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-2-events-with-unique-id/321388/3 "2023-01-13T17:48:53Z")

</div>

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