So that was, let's go back to kitty. There we go. This is how we make that little flag, that mark span. And we noticed that there is a finish method at the end. That means wrap everything up and append it to the actual transaction so it can be sent at the end. If we don't call the finish method, it won't appear into our trace. Okay, so that's how we created the first one.
Then we obtained the headers. And then we append the SentryTrace to the Sentry-TraceHeather. trace ID, that's the unique ID that I mentioned before, that we want to append or create for every request, so we can trace the execution of it, even though it's on the backend or even though it spans across 10 different microservices. The transaction has its own toTraceParents method that returns a string, and if we send this Sentry-trace header, the backend or the microservice that we're going to hit or whatever receives our requests, we'll know how to continue the trace from this point on, right? Because we are still in the hook. We're still in the browser. When we send off the request, here's what we started so the backend can be like, okay, I know, I see a trace. I'm just going to continue contributing spans to this trace and then I'm gonna get it back, or, like, send it off to even a different microservice. So this is how we connect the client side and the back end, right? The front end and the back end. Again, this is not something that you need to do. The Next.js SDK will just do this for you. But, for example, if you had a different project that might not have this built automatically into the same tree SDK, this is how you would do it. The actual trace parent returns the trace ID dash the last span ID dash a value, which is like zero or one to indicate whether the transaction is sampled or not. But that's basically it. It's just a string. So as long as we're able technically to transfer the string, we can continue the trace across all of the backend services even Chrome jobs like we can actually save this to a database retrieve it I don't know in a Chrome job at midnight grab this entry trace and append it to the basically append it to on the transaction so we can continue the trace that happened maybe a few days ago we can still contribute to it right so this is how we connect these services to contribute and to to contribute to the same trace cool so this happens and then we just create a little fetch method with our headers uh sets um that we created previously and this basically is the instance of, uh, automatically, like the Sentry SDK being able to automatically instrument it. And that's this span right here, the HTTP.client, cause we created the mark span, but we didn't create the HTTP client. And that's been done automatically by the Sentry SDK, because we can see see that a network event happens and the SDK itself is going to create that span for you. So that's why I mentioned you don't need to worry about this. But okay cool. We are sending this off and then we're continuing on the backend. And I can open the backend now, so that would be that would be pages slash api slash flashcards slash slack because that's where we send the HTTP requests. If we scroll down we're going to see This part right here get user from session. That is the first Span that we see there we go users find unique And then if we go down to the put request handler there we go You see the same thing happens here give me the current hub and I'll give me the current active scope but this time I'm not going to create a transaction. This time I'm going to try to get the transaction that already is created. And of course the Sentry Next.js SDK will create that for us because it's going to see that there is a header called Sentry-trace. So the SDK knows, all right, so we have a trace going on. I'm just going to create this transaction for you automatically because apparently you want to continue the trace. So here's the transaction. don't bother creating a new one. Okay, cool. We're getting that, and then we await this method, get category by ID. So if we go into this method, we're just going to see that we're just using Prisma to find a unique category where the ID matches the one that we passed from the outside. And there it is, category, find unique. That's exactly what we did. And this is possible because Sentry also has an integration that we can install to our server config. So our Prisma queries are automatically instrumented. So we don't need to create manually create spans for us. For example, if we go to Sentry-server.config.js and scroll down, there we go. into the integrations field, we can see that we have a new century.integrations.prisma, and we pass that Prisma client to the integration. So every time we execute an SQL query, the Prisma integration is going to create that span automatically for us. Again, the job for a century DevRel is hard because everything is automated and everything is just, it just works. But here we are. Okay, then we create that category checks function, span that we saw in our tree, and look what happens. We're creating the category checks span. We create or we perform the checks. If it doesn't exist, we set the status to Not Found and we finish the span. If it doesn't belong to this user, right, for example, I'm trying to edit Sarah's category. I should not be able to do that, right? Then it's not. We're setting the status to unauthenticated, and then we'll finish it. And if everything's okay, then I'm just going to finish this category check span. And basically, the checks, if you want it to know, they took 0.04 milliseconds, those two if conditions there.
Okay, moving on. Then we do the flash, get-flash part. which again, is just a... If we go into the body, it's just a Prisma query. Nothing fancy, automatically instrumented and there we go, flashcards, fine. First of that took about 80 milliseconds, right? That's how long it took for Prisma to get that flashcards back to our back-end from the database. Same thing happens here. Give me a new span, create a new span, check if the flash part is found or check if the data is okay or if it belongs to this user, etc. Otherwise just finish it if everything is alright and then do the actual updates. Again, automatically instrumented and there's this pen right here that takes 756 millisecond. At the end we just close the response, set the status, bring back the updated methods, sorry, the updated flashcards back to the client, but you might notice that we're not actually calling the finish to the transaction. That's because we didn't create it.
Comments