# Weld (CDI) classes are also traced

**URL:** <https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595>\
**Category:** APM\
**Created:** [May 3, 2019, 11:00pm UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595 "2019-05-03T23:00:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxant](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@maxant](https://discuss.elastic.co/u/maxant)\
**Post date:** [May 3, 2019, 11:00pm UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595/1 "2019-05-03T23:00:28Z")

</div>

When using Java 8 + Payara (Micro) 5.191, I noticed that spans are automatically created for interceptor classes from Weld:

![image](https://us1.discourse-cdn.com/elastic/original/3X/3/d/3d719ffaf48b69e001b2086e372218650268bf1b.png)

They appear when debugging too:

![image](https://us1.discourse-cdn.com/elastic/original/3X/1/6/165aed0e0be71d9475d40b1112904c96cc8c629d.png)

It would be cool if they could be ignored. Unfortunately they are generated classes and have the same package name as the classes that I want to trace.

Also, is it possible that the APM agent causes "this" to not be viewable in the debugger?

![image](https://us1.discourse-cdn.com/elastic/original/3X/8/4/844f1c396ed8816137ae24b05e34db4dd924875b.png)

**Kibana version** :  
7.0.0  
**Elasticsearch version** :  
7.0.0  
**APM Server version** :  
7.0.0

**APM Agent language and version** :  
java latest  
**Browser version** :  
n/a  
**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:  
docker  
**Fresh install or upgraded from other version?**  
fresh

---

<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 5, 2019, 10:40am UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595/2 "2019-05-05T10:40:39Z")

</div>

Hi and welcome to the forum 🙂

> [@maxant](#):
>
> It would be cool if they could be ignored. Unfortunately they are generated classes and have the same package name as the classes that I want to trace.

There is a way to ignore specific classes, but it requires you to identify the class name patterns of what you want to include/exclude anyway, so you might as well use the [`trace_methods` config option](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-trace-methods) with more specific/narrowed-down patterns and not a very general root package. Note that limiting the number of classes/methods being traced is highly recommended in any case, as it may increase overhead significantly without necessarily providing a lot more value. If you can find the narrowest-informative context for tracing, you would get the best value of this. Typically, this feature is best used for tracing specific methods.

> [@maxant](#):
>
> They appear when debugging too:

What changed in debugging? The stack in the screenshot doesn't seem to be related to the agent.

> [@maxant](#):
>
> Also, is it possible that the APM agent causes "this" to not be viewable in the debugger?

Not sure, I will need the full context for that. What is the exact breakpoint related to this screenshot? If it's a static method, or a constructor, for example, `this` would not be available. Does it behave differently on the same breakpoint with and without the agent?

Thanks,  
Eyal.

---

<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 7, 2019, 7:16am UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595/3 "2019-05-07T07:16:37Z")

</div>

@maxant which version are you using? Also, please send your `trace_methods` configuration and any other tracing-related configuration you are using.  
Thanks.

---

<div class="post-metadata">

**Author:** ![suikast42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suikast42/32/29865_2.png) [@suikast42](https://discuss.elastic.co/u/suikast42)\
**Post date:** [May 7, 2019, 8:39am UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595/4 "2019-05-07T08:39:05Z")

</div>

I do the CDI tracing over an interceptor with custom spans.

```
   @AroundInvoke
public Object call(InvocationContext context) throws Throwable {
    // TODO: Enable over System property
    Method method = context.getMethod();
    String className = context.getMethod().getDeclaringClass().getCanonicalName();
    String methodName = method.getName();

    boolean isInTransaction = ElasticApmUtils.isInTransaction();
    final Transaction transaction = ElasticApmUtils.apmTransactionCdiContext(className, methodName);

    log.debug(className + "#" + methodName + " isInTransaction " + isInTransaction);
   // TODO vuru 29.04.2019: Make abstraction
    if (isInTransaction) {
        boolean errorOccured = false;
        final Span span = transaction
                .startSpan("Cdi-Nested", className, methodName)
                .setName(className + "#" + methodName);
        try (Scope scope = span.activate()){
            final Object proceed = context.proceed();
            return proceed;
        } catch (Exception e) {
            span.captureException(e);
            errorOccured = true;
            throw e;
        } finally {
            span.end();
            if(errorOccured ){
                transaction.setResult("Failed");
            } else {
                transaction.setResult("Ok");
            }
        }
    } else {
        boolean errorOccured = false;
        try (Scope scope = transaction.activate()) {
            final Object proceed = context.proceed();
            return proceed;
        } catch (Exception e) {
            transaction.captureException(e);
            errorOccured = true;
            throw e;
        } finally {
            transaction.end();
            if(errorOccured ){
                transaction.setResult("Failed");
            } else {
                transaction.setResult("Ok");
            }
        }
    }
}

```

You can use this as a prototype and filter out what ever you want

---

<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 28, 2019, 4:39am UTC](https://discuss.elastic.co/t/weld-cdi-classes-are-also-traced/179595/5 "2019-05-28T04:39:06Z")

</div>

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