Search code examples
javakotlinamazon-cloudwatchmetricsktor

How can I remove the child node name from the metrics sent to Cloudwatch?


I am working with a Java API built with Ktor. It posts metrics to CloudWatch using the MicrometerMetrics feature. My issue is that I have created an interceptor which is cluttering the route name in CloudWatch.

Here follows the interceptor:

fun Route.test(callback: Route.() -> Unit): Route {
    val routeTest = createChild(object : RouteSelector(1.0) {
        override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation =
            RouteSelectorEvaluation.Constant
    })
    routeTest.intercept(ApplicationCallPipeline.Call) {
        call.response.header("X-Next-Polling-In", pollTime)
    }
    callback(routeTest)
    return routeTest
}

And here is how the route for the request /users, intercepted by the previous interceptor, is shown in CloudWatch:

/users/my.package.TestInterceptor$test$routeTest$1@6cdas3a85

Is there any way to remove the child node name or hardcode an unique value instead of my.package.TestInterceptor$test$routeTest$1@6cdas3a85?


Solution

  • I managed to solve this issue by overridding the toString method on the RouteSelector as follows:

    fun Route.test(callback: Route.() -> Unit): Route {
        val routeTest = createChild(object : RouteSelector(1.0) {
            override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation =
                RouteSelectorEvaluation.Constant
    
            override fun toString() = "customName"
    
        })
        routeTest.intercept(ApplicationCallPipeline.Call) {
            call.response.header("X-Next-Polling-In", pollTime)
        }
        callback(routeTest)
        return routeTest
    }
    

    This leads to the following route name:

    /users/customName