【问题标题】:Need Elastic APM support for Ktor backend serverKtor 后端服务器需要 Elastic APM 支持
【发布时间】:2023-03-03 11:03:02
【问题描述】:

尝试监控我们的 Ktor 后端应用程序的性能,并能够将 Elastic APM 代理附加到它。服务器在 Kibana 仪表板中作为服务可见。但它不会为每个传入请求自动创建事务。当我们手动启动事务并在特定路由中结束它时,它只会记录该请求的性能。有没有其他方法可以解决这种情况?

尝试了以下方法

  1. 在设置阶段拦截每个请求并启动事务,但在最后拦截相同调用时无法结束事务面临的问题。
  2. 对于在下面定义的控制器/路由中的每个请求,它都在工作。
    get("/api/path") {
        val transaction: Transaction = ElasticApm.startTransaction()
        try {
            transaction.setName("MyTransaction#getApi")
            transaction.setType(Transaction.TYPE_REQUEST)
            // do business logic and response
         } catch (e: java.lang.Exception) {
            transaction.captureException(e)
            throw e
         } finally {
            transaction.end()
         }  
    }
    

Adding below line for better search result for other developers.
How to add interceptor on starting and ending on each request in ktor. Example of ApplicationCallPipeline.Monitoring and proceed()

【问题讨论】:

    标签: kotlin elastic-stack ktor apm elastic-apm


    【解决方案1】:

    您可以使用执行管道其余部分的proceed 方法来捕获任何发生的异常并完成事务:

    intercept(ApplicationCallPipeline.Monitoring) {
        val transaction: Transaction = ElasticApm.startTransaction()
        try {
            transaction.setName("MyTransaction#getApi")
            transaction.setType(Transaction.TYPE_REQUEST)
            proceed() // This will call the rest of a pipeline
        } catch (e: Exception) {
            transaction.captureException(e)
            throw e
        } finally {
            transaction.end()
        }
    }
    

    此外,您可以使用attributes 来存储一个调用期间的事务(从请求开始时开始,到响应发送后结束)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-21
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2014-01-18
      • 2021-01-12
      • 2020-07-21
      相关资源
      最近更新 更多