# Prevent transaction from getting added after a http(Rest) call

**URL:** <https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344>\
**Category:** APM\
**Tags:** rum\
**Created:** [June 12, 2019, 7:04am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344 "2019-06-12T07:04:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkat6/32/47922_2.png) [@venkat6](https://discuss.elastic.co/u/venkat6)\
**Post date:** [June 12, 2019, 7:04am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344/1 "2019-06-12T07:04:12Z")

</div>

My code

```
var transaction = apm.startTransaction("Add Random Hero from ui", "Person");
    var span = apm.startSpan('Adding data');
    var hero: Person = new Person();
    hero.id = Math.floor(Math.random() * 100);
    hero.name = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
    apm.addTags(hero);
    apm.setCustomContext(hero);
    this.heroService.addH(hero)
      .subscribe(hero => {
        console.log(hero['_body']);
        if (span) span.end();
        var span = apm.startSpan('fetching data');
        this.heroService.getHeroes()
          .subscribe(heroes => {
            console.log(heroes['_body']);
            this.heroes = JSON.parse(heroes['_body']) as Person[];
            if (span) span.end();
            var transaction = apm.getCurrentTransaction();
            if (transaction) transaction.end();
          });
      });

```

So, soon after I make a `post` call to add person object transaction gets added. But the other fetch `get` call isn't getting recorded, because the the transaction has already been added.

Logs:  
 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/8/2/82a13f73604b4ce2f64c2c8e3511de0270710612.jpeg)

---

<div class="post-metadata">

**Author:** ![Hamidreza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hamidreza/32/21369_2.png) [@Hamidreza](https://discuss.elastic.co/u/Hamidreza)\
**Post date:** [June 12, 2019, 7:43am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344/2 "2019-06-12T07:43:25Z")

</div>

Hi @venkat6,

Thanks for reaching out.

In the RUM agent we keep track of the tasks related to a transaction in order to end the transaction automatically once all of the tasks are finished. You can use the task API (still experimental) to manually add tasks as well. As long as there are tasks remaining the transaction would not end unless `transaction.end()` is called explicitly.

Something like:

```auto
const taskId = transaction.addTask()

...

/* This will end the transaction automatically if there are no tasks remaining */
transaction.removeTask(taskId) 

/* Or you can call transaction.end() if you know at this point transaction has to end. */

```

Let me know how this works for you.

Cheers,  
Hamid

---

<div class="post-metadata">

**Author:** ![venkat6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkat6/32/47922_2.png) [@venkat6](https://discuss.elastic.co/u/venkat6)\
**Post date:** [June 12, 2019, 9:46am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344/3 "2019-06-12T09:46:13Z")

</div>

Hi @Hamidreza,

Thanks for the quick reply.

As you have suggested, task api works like a charm.  
But as mentioned it's still in beta. So, is there any other way of preventing transaction from automatically ending.

Thank you.

---

<div class="post-metadata">

**Author:** ![Hamidreza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hamidreza/32/21369_2.png) [@Hamidreza](https://discuss.elastic.co/u/Hamidreza)\
**Post date:** [June 13, 2019, 7:46am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344/4 "2019-06-13T07:46:04Z")

</div>

Currently, using the task API is the only way to prevent the transaction from ending early. The reason for this is if a http request is executed after all the transaction tasks are finished then it is not considered part of that transaction. However, I have created this [issue](https://github.com/elastic/apm-agent-rum-js/issues/290) to address this issue for custom transactions.

---

<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 4, 2019, 3:46am UTC](https://discuss.elastic.co/t/prevent-transaction-from-getting-added-after-a-http-rest-call/185344/5 "2019-07-04T03:46:19Z")

</div>

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