Not getting baggage and sentry-trace headers for fetch requests in React #8461
-
I'm trying to get a super simple app running that will call a backend server (could be written in anything) and will send through sentry-trace and baggage headers... so basically Sentry's browser tracing feature. I've been banging my head against this for a couple of hours now without any success though. Hoping someone here can help spot my mistake. Events are coming through to Sentry... I see error details and Replay sessions in there. However these aren't tied with any Trace to events in the backend application (which is also logging events/errors just fine... but obviously not correlated to anything on the front end, as there's no trace header that could be used for that). I must be making some noob mistake... index.js (less the imports)Sentry.init({
dsn: "...my dsn here (not this, obviously)",
debug: true,
integrations: [
new Sentry.BrowserTracing({
tracePropagationTargets: ["localhost", "localhost:59740", /.*/],
}),
new Sentry.Replay({
networkDetailAllowUrls: [
window.location.origin,
/.*/,
],
networkRequestHeaders: ["referrer", "sentry-trace", "baggage"],
networkResponseHeaders: ["Server"],
}),
],
tracesSampleRate: 1.0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 1.0,
});
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals(); App.js (less the imports)function App() {
const sayHello = async () => {
const transaction = Sentry.startTransaction({ name: "saying hello" });
var response;
try
{
response = await fetch('http://localhost:59740/hello', {
method: 'GET',
headers: {
Accept: 'application/json',
},
});
}
finally
{
transaction.finish();
}
if (!response.ok) {
alert(`Error! status: ${response.status}`);
return;
}
const result = await response.text();
alert(result);
}
return (
<div className="App">
<button onClick={sayHello}>Say gidday</button>
</div>
);
}
export default App; Relevant dependencies in "@sentry/react": "^7.57.0",
"react": "^18.2.0", |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
hey @jamescrosswell we rely on looking up a span/transaction on an internal storage we call the scope to determine if it's "active" or not. This is what we use to automatically attach spans generated from integrations as well as grab the active span to attach tracing headers. So what you need to do here is to const transaction = Sentry.startTransaction({ name: "saying hello" });
Sentry.configureScope(scope => {
scope.setSpan(transaction);
});
// ... do fetch request logic here Soon though we'll be officially releasing a helper to make this easy, in the meantime it's still exported so you can use it. function App() {
const sayHello = async () => {
const response = await Sentry.trace({ name: 'saying hello' }, () => {
return fetch('http://localhost:59740/hello', {
method: 'GET',
headers: {
Accept: 'application/json',
},
});
});
const result = await response.text();
alert(result);
}
return (
<div className="App">
<button onClick={sayHello}>Say gidday</button>
</div>
);
}
export default App; It's not a great experience - we are actively working on making it better! See the RFC (that I'm also working on) to propose this new callback API for all of our SDKs. getsentry/rfcs#101 |
Beta Was this translation helpful? Give feedback.
-
@AbhiPrasad I'm having the same issue described here (no baggage or sentry-trace headers going up with requests). Relevant things I can think of to mention: Package versions:
Relevant Config:
I'm not using BrowserTracing because i don't want automatic instrumentation, instead calling Finally, usage looks like:
I can clearly see the request for this transaction/span going out to sentry and the transaction appears in the Performance tab for our app, but no headers which I understand will mean distributed transactions wont work. I've tried all the variants of the new span APIs as well as the legacy approach with a transaction (including setting it as the active span for the scope and not doing that). Any thoughts? Also for what it's worth we had these headers sending in a previous version of the sentry client. I'm not sure if something meaningfully changed since |
Beta Was this translation helpful? Give feedback.
hey @jamescrosswell
we rely on looking up a span/transaction on an internal storage we call the scope to determine if it's "active" or not. This is what we use to automatically attach spans generated from integrations as well as grab the active span to attach tracing headers.
So what you need to do here is to
Soon though we'll be officially releasing a helper to make this easy, in the meantime it's still exported so you can use it.