Closed jc-akkodis closed 1 month ago
When I do an fetch and I get e.g. 400 back and I catch the exception
Fetch API itself doesn't throw on 4xx/5xx, this is one of the many DX convenience things axios does (which yeah makes a lot of sense when actually creating something, but fetch is just a lower level API). So otel's instrumentation is correct in this as in there's no error (fetch only throws errors when there's something like a network issue, domain name didn't resolve, ...)
It looks like that I can't get the span for the actual fetch call or is there something else?
P.s. I have the same issue if I try to set an attribute to the active span within catch section.
Yeah generally by the time your code can process the response the span has already ended since the span represents the raw http call. If you want to do more, your options are:
applyCustomAttributesOnSpan
option on fetch (& xhr) instrumentation(s). Note that you are limited to fields on the Response object, parsing the body would be an async operation and the span's ended by thengetEmail
function itself
import {trace, context} from '@opentelemetry/api';
const tracer = trace.getTracer('my-application', '1.0.0');
public function getEmail(email: string): Promise<IAccountModel> {
const span = tracer.startSpan('getEmail');
return context.with(trace.setSpan(context.active(), span), async () => {
try {
const { data } = await axios.get<IAccountModel>(this.backEndUrl + '/api/GetUserEmail?email=' + email, {
withCredentials: true,
});
// do stuff with span & data
return data;
} catch (err) {
// do stuff with span & err
throw err;
} finally {
span.end();
}
});
}
Thanks for answering this @t2t2. This is correct, there's no exception to record here as fetch does not throw. Closing this issue.
I have a reactjs application where I use @opentelemetry/auto-instrumentations-web + @opentelemetry/instrumentation-fetch with the latest version. When I do an fetch and I get e.g. 400 back and I catch the exception, then I would assue to see the exception event in the span of the fetch request, but it is not recorded.
This is my current implementation:
Call:
I tried to force the record of the exception in the catch section but it does not work since the span which I get is enden (https://github.com/open-telemetry/opentelemetry-js/blob/5c1ae0aa5b686aae3e7511f1eb9cfcc4d7ab2326/packages/opentelemetry-sdk-trace-base/src/Span.ts#L144):
If i create an other span then the exception would be recorded but I would like to avoid this:
It looks like that I can't get the span for the actual fetch call or is there something else?
P.s. I have the same issue if I try to set an attribute to the active span within catch section.
Please help!