Skip to content

Support profiling queries in Tracer#90574

Merged
DJRickyB merged 6 commits intoelastic:mainfrom
DJRickyB:query-tracing
Oct 4, 2022
Merged

Support profiling queries in Tracer#90574
DJRickyB merged 6 commits intoelastic:mainfrom
DJRickyB:query-tracing

Conversation

@DJRickyB
Copy link
Copy Markdown
Contributor

@DJRickyB DJRickyB commented Sep 30, 2022

This pull request adds the necessary support, and implementation, for profiling queries in the Tracer.

In order to use the APM Agent's inferred spans functionality, the active span's context has to be open in the current thread. This PR adds context-sensitive methods to the Tracer interface, implements them in APMTracer, and makes use of them in the private SearchService.executeQueryPhase(), which is on the stack for a lot of our most critical operations. Here is an example of the added functionality supported here (inferred spans in pink).

image

permission java.io.FilePermission "/proc/self/cgroup", "read";
permission java.io.FilePermission "/sys/fs/cgroup/-", "read";

// system memory on Linux systems affected by JDK bug (#66629)
Copy link
Copy Markdown
Contributor Author

@DJRickyB DJRickyB Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spotted this old permission, no longer relevant since we have minimum Java > 17

* Has to be declared as a header copied over for tasks.
*/
public static final String TRACE_ID = "trace.id";
public static final String TRACE_PARENT = "traceparent";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit this in my testing, added here to be in the headers allowlist

permission java.lang.RuntimePermission "loadLibrary.*";
permission java.lang.RuntimePermission "getClassLoader";
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
permission org.elasticsearch.secure_sm.ThreadPermission "modifyArbitraryThreadGroup";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added due to the following error:

2022-09-30 20:33:32,521 [elastic-apm-sampling-profiler] ERROR co.elastic.apm.agent.profiler.SamplingProfiler - Stopping profiler
java.security.AccessControlException: access denied ("org.elasticsearch.secure_sm.ThreadPermission" "modifyArbitraryThreadGroup")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:485) ~[?:?]
        at java.security.AccessController.checkPermission(AccessController.java:1068) ~[?:?]
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:411) ~[?:?]
        at org.elasticsearch.secure_sm.SecureSM.checkThreadGroupAccess(SecureSM.java:195) ~[elasticsearch-secure-sm-8.5.0-SNAPSHOT.jar:?]
        at org.elasticsearch.secure_sm.SecureSM.checkAccess(SecureSM.java:135) ~[elasticsearch-secure-sm-8.5.0-SNAPSHOT.jar:?]
        at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:335) ~[?:?]
        at java.lang.ThreadGroup.enumerate(ThreadGroup.java:441) ~[?:?]
        at co.elastic.apm.agent.profiler.ThreadMatcher.forEachThread(ThreadMatcher.java:41) ~[?:?]
        at co.elastic.apm.agent.profiler.SamplingProfiler.restoreFilterState(SamplingProfiler.java:433) ~[?:?]
        at co.elastic.apm.agent.profiler.SamplingProfiler.profile(SamplingProfiler.java:402) ~[?:?]
        at co.elastic.apm.agent.profiler.SamplingProfiler.run(SamplingProfiler.java:377) ~[?:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]

Required additionally permitting this to the "class permissions" for modules. In actuality, this permission is preventing the ProfilingSampler from doing ThreadGroup::enumerate in order to pass threads to async-profiler, not actually working to modify them. Anyway it doesn't seem so concerning as the already-permitted-for-modules <<ALL FILES>> | read,write so I've added it

@DJRickyB DJRickyB requested a review from ChrisHegarty October 3, 2022 17:23
@DJRickyB DJRickyB self-assigned this Oct 3, 2022
@DJRickyB DJRickyB added the :Core/Infra/Core Core issues without another label label Oct 3, 2022
@DJRickyB DJRickyB marked this pull request as ready for review October 3, 2022 17:24
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Oct 3, 2022
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

Copy link
Copy Markdown
Contributor

@ChrisHegarty ChrisHegarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Copy Markdown
Contributor

@pugnascotia pugnascotia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we'll want to have a releasable startTrace at some point for the non-thread context case, but we can look at that in the future.

@DJRickyB DJRickyB merged commit f7bb5e0 into elastic:main Oct 4, 2022
@DJRickyB DJRickyB deleted the query-tracing branch October 4, 2022 12:45
jtibshirani added a commit that referenced this pull request Oct 6, 2022
It's nice to avoid introducing too many constructors -- if we did this for all
arguments, we could have a lot of "telescoping" going on.

Follow up to #90574.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v8.6.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants