# Calculate the time difference between 2 xml log lines

**URL:** <https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458>\
**Category:** Logstash\
**Created:** [April 28, 2021, 4:30am UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458 "2021-04-28T04:30:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![roua.B](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@roua.B](https://discuss.elastic.co/u/roua.B)\
**Post date:** [April 28, 2021, 4:30am UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/1 "2021-04-28T04:30:11Z")

</div>

Hello everyone,  
I have an XML log file that looks like this:

```auto
<TRACE timestamp="1612778642004" dateTimeFormat="yyyy.MM.dd kk:mm:ss z">
    <LOGIN user="BSCSWS" factory="117" timestamp="1612778642003"/>
    <SOCREATE soiName="CMI" soiVersion="2" name="144" timestamp="1612778642005" factory="117"/>

    <COMMAND name="SESSION.CHANGE" timestamp="1612778642014" so="144">
...
    </COMMAND>
    <COMMAND name="LANGUAGES.READ" timestamp="1612778642017" so="144">
...
    </COMMAND>
<SODISPOSE soiName="CMI" soiVersion="2" name="144" timestamp="1612779491299" factory="117"/>
</TRACE>

```

I would like to get the execution time of each command, that means I should calculate the difference between two successive timestamps. I tried to use the elapsed plugin, but nothing is displayed in output.  
Is there any other way to do this?

Thank you for your help 🙂

---

<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:** [April 28, 2021, 2:41pm UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/2 "2021-04-28T14:41:37Z")

</div>

How are you consuming the log? Is it a line at a time or are you consuming the entire TRACE element as a single event?

---

<div class="post-metadata">

**Author:** ![roua.B](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@roua.B](https://discuss.elastic.co/u/roua.B)\
**Post date:** [April 28, 2021, 3:06pm UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/3 "2021-04-28T15:06:45Z")

</div>

I'm consuming each COMMAND element as a single event.

---

<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:** [April 28, 2021, 3:28pm UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/4 "2021-04-28T15:28:51Z")

</div>

Assuming that each event has a [timestamp] field with a value like "1612778642014" I would do this in a ruby filter. You will need to set pipeline.workers to 1 and make sure pipeline.ordered is true (which it is by default in 7.x when pipeline.workers is 1)

```
ruby {
    code => '
        ts = event.get("timestamp").to_i
        if ts
            if @lastTimestamp
                event.set("duration", ts - @lastTimestamp)
            end
            @lastTimestamp = ts
        end
    '
}
```

---

<div class="post-metadata">

**Author:** ![roua.B](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@roua.B](https://discuss.elastic.co/u/roua.B)\
**Post date:** [April 28, 2021, 7:49pm UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/5 "2021-04-28T19:49:02Z")

</div>

This worked perfectly, thank you 🙂

---

<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:** [May 26, 2021, 7:49pm UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-2-xml-log-lines/271458/6 "2021-05-26T19:49:08Z")

</div>

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