Skip to content

Commit f3b83ad

Browse files
authored
do not lose span renames when the rename happens before returning actions in a play controller (#1412)
1 parent fed4630 commit f3b83ad

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

instrumentation/kamon-play/src/main/scala/kamon/instrumentation/play/PlayServerInstrumentation.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ object GenerateOperationNameOnFilterHandler {
291291
def enter(@Advice.Argument(0) request: RequestHeader): Unit = {
292292
request.attrs.get(Router.Attrs.HandlerDef).map(handler => {
293293
val span = Kamon.currentSpan()
294-
span.name(_routerNameGenerator.generateOperationName(handler))
294+
if (span.operationName() == "http.server.request") {
295+
span.name(_routerNameGenerator.generateOperationName(handler))
296+
}
295297
span.takeSamplingDecision()
296298
})
297299
}

instrumentation/kamon-play/src/test-common/scala/kamon/instrumentation/play/RequestInstrumentationSpec.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ abstract class RequestHandlerInstrumentationSpec extends PlaySpecShim with Guice
8686
case ("GET", "/not-found") => handler(action { NotFound })
8787
case ("GET", "/server") => handler(action { req => Ok(serverImplementationName(req)) })
8888
case ("GET", "/error") => handler(action(_ => sys.error("This page generates an error!")))
89+
case ("GET", "/rename-outside") => {
90+
Kamon.currentSpan().name("renamed-outside-action")
91+
handler(action { req => Ok(serverImplementationName(req)) })
92+
}
93+
case ("GET", "/rename-inside") => {
94+
handler(action { req =>
95+
Kamon.currentSpan().name("renamed-inside-action")
96+
Ok(serverImplementationName(req))
97+
})
98+
}
8999
}
90100
}
91101

@@ -146,6 +156,36 @@ abstract class RequestHandlerInstrumentationSpec extends PlaySpecShim with Guice
146156
}
147157
}
148158

159+
"do not assign operation names if the server span was renamed before returning an action" in {
160+
val wsClient = app.injector.instanceOf[WSClient]
161+
val endpoint = s"http://localhost:$port/rename-outside"
162+
val response = await(wsClient.url(endpoint).get())
163+
response.status mustBe 200
164+
165+
eventually(timeout(5 seconds)) {
166+
val span = testSpanReporter().nextSpan().value
167+
span.operationName mustBe "renamed-outside-action"
168+
span.metricTags.get(plain("component")) mustBe expectedServer
169+
span.metricTags.get(plain("http.method")) mustBe "GET"
170+
span.metricTags.get(plainLong("http.status_code")) mustBe 200L
171+
}
172+
}
173+
174+
"preserve operation names assigned inside actions" in {
175+
val wsClient = app.injector.instanceOf[WSClient]
176+
val endpoint = s"http://localhost:$port/rename-inside"
177+
val response = await(wsClient.url(endpoint).get())
178+
response.status mustBe 200
179+
180+
eventually(timeout(5 seconds)) {
181+
val span = testSpanReporter().nextSpan().value
182+
span.operationName mustBe "renamed-inside-action"
183+
span.metricTags.get(plain("component")) mustBe expectedServer
184+
span.metricTags.get(plain("http.method")) mustBe "GET"
185+
span.metricTags.get(plainLong("http.status_code")) mustBe 200L
186+
}
187+
}
188+
149189
"read headers case-insensitively" in {
150190
val wsClient = app.injector.instanceOf[WSClient]
151191
val endpoint = s"http://localhost:$port/request-id"

0 commit comments

Comments
 (0)