dd-trace-java icon indicating copy to clipboard operation
dd-trace-java copied to clipboard

Interceptor keeps sending errors even setting them to false.

Open ArtuoS opened this issue 9 months ago • 11 comments

Tracer Version(s)

1.48.1

Java Version(s)

11.0.26

JVM Vendor

Oracle JDK

Bug Report

I would like to ignore certain exceptions thrown by my application so they do not appear in 'Error Tracking'. Despite setting the Span error to false in TraceInterceptor, it continues to send them as errors to Datadog.

My class looks like this right now:

public class CustomTraceInterceptor implements TraceInterceptor {
    @Override
    public Collection<? extends MutableSpan> onTraceComplete(Collection<? extends MutableSpan> collection) {
        for (MutableSpan span : collection) {
            if (span.getTag("http.route").toString().contains("health-check")) {
                continue;
            }

            Throwable throwable = (Throwable) span.getTag("error.type");
            if (throwable instanceof NotFoundException) {
                span.setError(false);
                span.setTag(DDTags.ERROR_MSG, "");
                span.setTag(DDTags.ERROR_TYPE, "");
                span.setTag(DDTags.ERROR_STACK, "");
                span.setTag("error.object", "");
            }
        }
        return collection;
    }

    @Override
    public int priority() {
        return 0;
    }
}

Obs: the issue is not related to the if condition, I tried to intercept every exception and it still sending it as errors.

Expected Behavior

Exceptions set to false should not appear in Datadog Error Tracking.

Reproduction Code

No response

ArtuoS avatar Apr 23 '25 11:04 ArtuoS

I believe getTag("error.type") returns a String not a Throwable instance. It should contain the name of the exception type. So the cast to Throwable there is likely raising a ClassCastException and exiting before if the statement.

dougqh avatar Apr 28 '25 16:04 dougqh

I've tried removing the if statement before to see if thats the problem, and the code looked like this:

public class CustomTraceInterceptor implements TraceInterceptor {
    @Override
    public Collection<? extends MutableSpan> onTraceComplete(Collection<? extends MutableSpan> collection) {
        for (MutableSpan span : collection) {
            if (span.getTag("http.route").toString().contains("health-check")) {
                continue;
            }

            span.setError(false);
            span.setTag(DDTags.ERROR_MSG, "");
            span.setTag(DDTags.ERROR_TYPE, "");
            span.setTag(DDTags.ERROR_STACK, "");
            span.setTag("error.object", "");
        }
        return collection;
    }

    @Override
    public int priority() {
        return 0;
    }
}

However, it still kept sending errors.

ArtuoS avatar Apr 28 '25 18:04 ArtuoS

Hi @ArtuoS, TraceInterceptor#priority() needs to return a value that is not used by other interceptors. Can you try setting your priority to something like 100? You can find the priorities of standard interceptors here

Hi @ArtuoS, TraceInterceptor#priority() needs to return a value that is not used by other interceptors. Can you try setting your priority to something like 100? You can find the priorities of standard interceptors here

Hey, I tried some priority values:

@Override
public int priority() {
    return Integer.MAX_VALUE - 546;
}

Even though, it didn't work.

ArtuoS avatar May 05 '25 17:05 ArtuoS

@ArtuoS Did you solved the issue?

I want to do something similar.

esfomeado avatar May 20 '25 09:05 esfomeado

@ArtuoS Did you solved the issue?

I want to do something similar.

Not yet, issue persists.

ArtuoS avatar May 27 '25 19:05 ArtuoS

I´ve got a working solution but now for some reason the metrics are no longer sent to datadog

esfomeado avatar May 27 '25 20:05 esfomeado

I´ve got a working solution but now for some reason the metrics are no longer sent to datadog

Can you share your working solution? I'll be really thankful.

ArtuoS avatar May 29 '25 16:05 ArtuoS

@ArtuoS Sorry for the late response. I did as shown here:

https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/java/dd-api/#extending-tracers

esfomeado avatar Jun 03 '25 20:06 esfomeado

@ArtuoS Sorry for the late response. I did as shown here:

https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/java/dd-api/#extending-tracers

Thank you, I'll give it a try.

ArtuoS avatar Jun 05 '25 12:06 ArtuoS

Hey, is there an official or working solution to achieve this? (@ArtuoS )

juliocastrodev avatar Dec 01 '25 08:12 juliocastrodev