ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.99k stars 1.06k forks source link

Hooks in JettyEngine to facilitate NewRelic-Coroutine integration #1792

Open jeffr-usafacts opened 4 years ago

jeffr-usafacts commented 4 years ago

Subsystem Ktor JettyApplicationEngine

Is your feature request related to a problem? Please describe. The core issue is that the JettyKtorHandler invoked by the JettyApplicationEngine loses the NewRelic Transaction when it launches the pipeline execution coroutine thread. This causes a loss of monitoring/timing/metrics in the NewRelic RPM UI, leading to opaque timing values only at the Jetty level (which is instrumented by the NewRelic Agent java agent).

Describe the solution you'd like

In the specific case of the NewRelic Jetty Problem, I could use a way to hook into the JettyKtorHandler handle method such that:

  1. From within the handle method's execution thread, I can extract the NewRelic transaction installed by the NewRelic agent (using NewRelic.getAgent().transaction), and
  2. Have a way to wrap the pipeline execution thread (which results from a launch call) such that such that I can get a token off the transaction and link() it before the pipeline is executed, and expire() it after it has completed.

There are a number of ways I can think to accomplish this (e.g. having a way to register with the JettyApplicationEngine a code block where I can modify the CoroutineContext that the pipeline runs in); however, it is my hope that a Ktor engineer would have a more holistic proposal to give this functionality across many engines.

Motivation to include to ktor NewRelic is one of the premier suites for application monitoring, with many service developers across the industry using NewRelic to monitor their apps.

When enterprises select service frameworks, the ability to clearly and easily monitor them is often a key factor in decision making; the introduction of support for NewRelic transaction monitoring in Ktor would remove a hurdle for Ktor's selection as a primary production service framework in the tech stack of many businesses.

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.