-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[APM] Get service name from context for alert flyout #104103
Conversation
Pinging @elastic/apm-ui (Team:apm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
It seems we have at least 4 ways to access serviceName
(and probably other path params):
- Via
useServiceName
- As a route prop:
const { serviceName } = props.match.params; - Via
useParams
:const { serviceName } = useParams<{ serviceName?: string }>(); - Via
useUrlParams
:kibana/x-pack/plugins/apm/public/components/alerting/error_count_alert_trigger/index.tsx
Line 39 in 7cc112d
const { urlParams } = useUrlParams();
Can we make this more consistent? I'm hoping that going forward we can at least get rid of useUrlParams
when we tackle #51963
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
It seems we have at least 4 ways to access serviceName
(and probably other path params):
- Via
useServiceName
- As a route prop:
const { serviceName } = props.match.params; - Via
useParams
:const { serviceName } = useParams<{ serviceName?: string }>(); - Via
useUrlParams
:kibana/x-pack/plugins/apm/public/components/alerting/error_count_alert_trigger/index.tsx
Line 39 in 7cc112d
const { urlParams } = useUrlParams();
Can we make this more consistent going forward? I'm hoping that we can at least get rid of useUrlParams
when we tackle #51963
export function useServiceName(): string | undefined { | ||
const history = useHistory(); | ||
for (const config of apmRouteConfig) { | ||
const match = matchPath<{ serviceName?: string }>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use useRouteMatch
? I don't think we would need to iterate through the routes if we did that. It's independent of any route component and just uses the current path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you have to give it a path like useRouteMatch('/app/apm/services/:serviceName/*')
? I thought that's how it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case we only know the actual pathname (history.location.pathname
), not the route since it's a generic hook that can be called anywhere in the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... but seeing your example, if it allows wildcards it might be different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm. It seems like useRouteMatch
simply uses https://github.com/pillarjs/path-to-regexp under the hood. What @smith is suggesting makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it to useRouteMatch('/services/:serviceName')
, which seems to work fine.
Yeah, I'd prefer to do it there and do the minimal changes now, as we're past FF. |
💚 Build SucceededMetrics [docs]Module Count
Async chunks
History
To update your PR or re-run it, just comment with: |
Closes #101837.
Instead of using
useParams
to get the service name, introduce auseServiceName
hook that is used byuseApmServiceContext
, in whichmatchPath
andapmRouteConfig
are used. This removes the necessity of the component callinguseApmServiceContext
to be a child of a service view route.