# Support for getting API mapping in parameterized URLs

**URL:** <https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445>\
**Category:** APM\
**Tags:** java\
**Created:** [May 13, 2020, 1:30pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445 "2020-05-13T13:30:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishnavk](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@krishnavk](https://discuss.elastic.co/u/krishnavk)\
**Post date:** [May 13, 2020, 1:30pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/1 "2020-05-13T13:30:10Z")

</div>

**Elasticsearch version** :  
6.8.4  
**APM Server version** :  
7.6.0  
**APM Agent language and version** :  
Java - 1.16.0

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:  
Download page

**Fresh install or upgraded from other version?**  
Upgraded version

**Is there anything special in your setup?** For example, are you using the Logstash or Kafka outputs? Are you using a load balancer in front of the APM Servers? Have you changed index pattern, generated custom templates, changed agent configuration etc.  
No  
**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:  
We have a path parameterized API implemented in our Springboot application:

```auto
  @GetMapping("/owners/{ownerId}")
	public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
		ModelAndView mav = new ModelAndView("owners/ownerDetails");
		Owner owner = this.owners.findById(ownerId);
		for (Pet pet : owner.getPets()) {
			pet.setVisitsInternal(visits.findByPetId(pet.getId()));
		}
		mav.addObject(owner);
		return mav;
	}

```

Example calls to this API are like:

```auto
/owners/3
/owners/4

```

We would like to group all these transactions based on the API mapping defined. For the above-mentioned example, we would like to group the APIs by /owners/{ownerId}

For example, an extra field in the 'url' section that specifies the API mapping:

```auto
'url': {
    'path': '/owners/3',
    'api_mapping': '/owners/{ownerId}',
    'full': ....
    ....
   }

```

Is there any way we can implement this using the latest versions? If not, can we expect a feature like this soon?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [May 13, 2020, 1:58pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/2 "2020-05-13T13:58:33Z")

</div>

We currently don't capture the value of these annotations, we only do that for the core JAX-RS annotations.  
However, you should be able to achieve what you are looking for by setting [`use_path_as_transaction_name`](https://www.elastic.co/guide/en/apm/agent/java/current/config-http.html#config-use-path-as-transaction-name) to `true` in combination with [`url_groups`](https://www.elastic.co/guide/en/apm/agent/java/current/config-http.html#config-url-groups). Make sure you use the latter config for all those paths that contain path-parameters.  
I hope this helps.

---

<div class="post-metadata">

**Author:** ![krishnavk](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@krishnavk](https://discuss.elastic.co/u/krishnavk)\
**Post date:** [May 13, 2020, 2:28pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/3 "2020-05-13T14:28:26Z")

</div>

Thank you for your reply, Eyal!

This solution did not work well for us.  
We tried the combination of `use_path_as_transaction_name` and `url_groups`, but we find that the transaction name follows a priority order as mentioned here: [https://github.com/elastic/apm-agent-java/issues/673](https://github.com/elastic/apm-agent-java/issues/673)

As per my understanding, since we use a spring MVC framework, the transaction name comes from the servlet as this takes precedence over the `use_path_as_transaction_name` setting.

Is there a way we can override this priority order?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [May 14, 2020, 6:49am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/4 "2020-05-14T06:49:18Z")

</div>

Ohh, right... Since `use_path_as_transaction_name` is an opt-in, we may need to reconsider this prioritization..  
In the mean time, try setting [`disable_instrumentations`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-disable-instrumentations) to `spring-mvc`.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [May 14, 2020, 6:58am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/5 "2020-05-14T06:58:21Z")

</div>

Spring sets a request attribute containing the Spring MVC pattern: `org.springframework.web.servlet.HandlerMapping.bestMatchingPattern` and `org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping`. The former is not required to be supported by all `HandlerMapping` implementations.

You could either use a `HandlerInterceptor` in combination with the public API to set `ElasticApm.currentTransaction().setName(handlerMappingName)` or contribute out-of-the-box support for that in the agent. We already have a configuration option for JAX-RS to favor the path mapping over the class and method name (`use_jaxrs_path_as_transaction_name`).

---

<div class="post-metadata">

**Author:** ![krishnavk](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@krishnavk](https://discuss.elastic.co/u/krishnavk)\
**Post date:** [May 15, 2020, 3:06am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/6 "2020-05-15T03:06:51Z")

</div>

This achieves what we needed, but seems like disabling the instrumentation has other consequences. The custom spans which we set did not show up in our spans list.  
We are not clear on what else will be affected, so any help regarding this would be appreciated.

---

<div class="post-metadata">

**Author:** ![krishnavk](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@krishnavk](https://discuss.elastic.co/u/krishnavk)\
**Post date:** [May 15, 2020, 3:09am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/7 "2020-05-15T03:09:26Z")

</div>

Setting the name with the help of Public APIs will definitely work, but we were just trying to see if this could be done with any present configuration options so there is as little disruption to the source code as possible.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [May 15, 2020, 6:45am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/8 "2020-05-15T06:45:46Z")

</div>

Setting `disable_instrumentations=spring-mvc` should not influence other spans, only avoid the naming of transactions to be `ControllerName#method`.

Also note, that for path parameters, you'll have to set [`url_groups`](https://www.elastic.co/guide/en/apm/agent/java/current/config-http.html#config-url-groups), as otherwise, you'll end up with different transaction names for each path parameter.

If you don't want to change the application code, the best way would be to contribute support for out-of-the-box support in the agent by modifying `SpringTransactionNameInstrumentation`.

> <https://github.com/elastic/apm-agent-java/blob/544e3e719585a1aa8b18afe092baa77847db6ff4/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/spring/webmvc/SpringTransactionNameInstrumentation.java#L114>

Would you be interested in that?

---

<div class="post-metadata">

**Author:** ![krishnavk](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@krishnavk](https://discuss.elastic.co/u/krishnavk)\
**Post date:** [May 26, 2020, 12:08pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/9 "2020-05-26T12:08:45Z")

</div>

Yes, we would indeed be interested.  
I realize the tag for this topic says Java agent, but we would also like to get this feature for Django 2.2 and below with the help of `DJANGO_TRANSACTION_NAME_FROM_ROUTE` setting.  
If you can point me to the right place for the python agent as well, it would be of great help!

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [May 26, 2020, 1:56pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/10 "2020-05-26T13:56:00Z")

</div>

> [@krishnavk](#):
>
> If you can point me to the right place for the python agent as well, it would be of great help!

Best to create another topic with the `python` tag, referencing this topic.

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [May 26, 2020, 2:13pm UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/11 "2020-05-26T14:13:48Z")

</div>

> [@krishnavk](#):
>
> I realize the tag for this topic says Java agent, but we would also like to get this feature for Django 2.2 and below with the help of `DJANGO_TRANSACTION_NAME_FROM_ROUTE` setting.

I'm afraid this is only possible for [Django 2.2](https://docs.djangoproject.com/en/3.0/releases/2.2/#urls)+, as Django didn't make the route available before that.

---

<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:** [June 16, 2020, 10:13am UTC](https://discuss.elastic.co/t/support-for-getting-api-mapping-in-parameterized-urls/232445/12 "2020-06-16T10:13:59Z")

</div>

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