-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing the trace name #25
Comments
After a night, it also could be possible to just add hooks to the different calls |
Hey @Fenntasy! Glad this package has been helpful so far. This package doesn’t actually instrument the express routes, it only emits Let me know if that helps. If you ARE trying to change any of the span names from this package I’m happy to discuss possible ways to configure that. |
I understand it doesn't intrument the express routes, my goal is to avoid having traces with Here's what I've done at the moment for my needs (look for START CHANGES): import { getRPCMetadata } from "@opentelemetry/core";
import { matchServerRoutes } from "@remix-run/server-runtime/dist/routeMatching";
import { createRoutes } from "@remix-run/server-runtime/dist/routes";
//...
private _patchCreateRequestHandler(): (
original: typeof remixRunServerRuntime.createRequestHandler,
) => any {
const tracer = this.tracer;
return function createRequestHandler(original) {
return function patchCreateRequestHandler(
this: any,
): remixRunServerRuntime.RequestHandler {
const [{ routes }] = arguments as unknown as Parameters<
typeof remixRunServerRuntime.createRequestHandler
>;
const originalRequestHandler: remixRunServerRuntime.RequestHandler =
original.apply(this, arguments as any);
return (
request: Request,
loadContext?: remixRunServerRuntime.AppLoadContext,
) => {
// START CHANGES
const url = new URL(request.url);
const matches = matchServerRoutes(createRoutes(routes), url.pathname);
if (matches && matches.length > 0) {
const route = matches[matches.length - 1].route;
const rpcMetadata = getRPCMetadata(opentelemetry.context.active());
if (rpcMetadata) {
rpcMetadata.span.updateName(
`${request.method} ${displayRoute(route.id)}`,
);
rpcMetadata.route = displayRoute(route.id);
}
}
// END CHANGES
const span = tracer.startSpan(
`remix.request`,
{
attributes: {
[SemanticAttributes.CODE_FUNCTION]: "requestHandler",
},
},
opentelemetry.context.active(),
);
addRequestAttributesToSpan(span, request);
const originalResponsePromise = opentelemetry.context.with(
opentelemetry.trace.setSpan(opentelemetry.context.active(), span),
() => originalRequestHandler(request, loadContext),
);
return originalResponsePromise
.then((response) => {
addResponseAttributesToSpan(span, response);
return response;
})
.catch((error) => {
addErrorAttributesToSpan(span, error);
throw error;
})
.finally(() => {
span.end();
});
};
};
};
} with const displayRoute = (route: string): string => {
if (route === "root") {
return "/";
}
return route
.replace("routes/", "/")
.replace(/\/__[^\/]*\/?/, "/")
.replace(".", "/")
.replace("$", ":");
}; The meat is it is what's also done by the Express intrumentation: const rpcMetadata = getRPCMetadata(opentelemetry.context.active());
if (rpcMetadata) {
rpcMetadata.span.updateName(
`${request.method} ${displayRoute(route.id)}`,
);
rpcMetadata.route = displayRoute(route.id);
}
|
Ah, okay, so you want to update the parent active span name from the express instrumentation plugin based on information from remix. I'm not sure updating another instrumentation's context is advised, as it can rewrite history a bit.. but perhaps there are use cases where it is helpful for organizing your data how you need. In this case, your code to get / modify the active span is good, but thinking through how to configure this there are a few options:
I'm not keen on (1) since I don't like baking in other framework knowledge into the code. I'm open to (2) or (3) as a way to make it framework-agnostic and more flexible, without adding too much work on the client side. Option (4) is probably too much complexity back to the user for right now. Leaning a bit more towards (3) since it's just a line more for the client, but opens up a lot more flexibility down the road if people need. Only part I'm not sure of is if instead of the What do you think? |
Hi there,
I'm opening this issue to discuss a work I've begun to work on my Remix app.
Here's my starting point, your instrumentation is a great work but it's annoying to have all traces be named like
GET *
.After investigating this is due to the underlying Express implementation since the
*
is theRequest.Route
and since Remix is working with anapp.all("*", createRequestHandler)
, it's logical that it uses it.So I've now a work in progress version where for this kind of file structure:
I can have traces name like:
I would prefer, for my taste to also be able to have
because I'm using express for other parts of my app so keeping a consistency with Express would be cool.
This could be done with options like so:
Would you be open to a PR with all that?
Should the whole renaming of the trace be optional?
Thanks for your work in any case it's been a great help!
The text was updated successfully, but these errors were encountered: