Finding the module of a callsite in the stacktrace

I want to get an item returned from stacktrace() such that it is the first StackFrame for which getmodule(x) == m where m is some known module. This works:

getmodule(x) = nothing
getmodule(x::Base.StackTraces.StackFrame) = getmodule(x.linfo)
getmodule(x::Core.MethodInstance) = getmodule(x.def)
getmodule(x::Core.Method) = x.module
getmodule(x::Core.Module) = x

And I use it in my code like so

trace = stacktrace()
callindex = m === nothing ? trace[2] :
     findfirst(x -> getmodule(x) === m, trace)
callsite = isnothing(callindex) ? trace[2] : trace[callindex]

From what I can tell, linfo is a documented property of stacktrace items, and MethodInstance is documented under julia internals such that the above should work. But I’m guessing anything under internals is subject to change in future versions (right?)

Is there a more reliable way to do this that would have more guarantees to work with new Julia releases? CodeTracking has some related tooling, but it does not extract the module.