From 295858e087883bce233294b6b14eab3fd38583f6 Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:02:53 +0100 Subject: [PATCH] Pass span to params helper in Koa hook When the Koa instrumentation request hook is executed, the current active span is the span from the _previous_ layer. This means that, for the router layer, the active span is the last middleware in the middleware chain. While this is fine functionally, it makes the behaviour of the hook hard to reason about. This change makes it so that the params are set in the span passed to the hook, instead of in the active span. The integration tests are also amended to check for the params in the router child span. --- src/client.ts | 4 ++-- test/koa-mysql/tests/spec/app_spec.rb | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/client.ts b/src/client.ts index 4d237b60..3c187972 100644 --- a/src/client.ts +++ b/src/client.ts @@ -244,10 +244,10 @@ export class Client { dbStatementSerializer: RedisDbStatementSerializer }, "@opentelemetry/instrumentation-koa": { - requestHook: function (_span, info) { + requestHook: function (span, info) { if (sendParams && info.layerType === KoaLayerType.ROUTER) { const queryParams = info.context.request.query - setParams(queryParams) + setParams(queryParams, span) } } }, diff --git a/test/koa-mysql/tests/spec/app_spec.rb b/test/koa-mysql/tests/spec/app_spec.rb index e236b7cd..a49b6aeb 100644 --- a/test/koa-mysql/tests/spec/app_spec.rb +++ b/test/koa-mysql/tests/spec/app_spec.rb @@ -16,17 +16,18 @@ end describe "GET /get with params" do - it "adds params to the HTTP root span" do + it "adds params to the router child span" do response = HTTP.get("#{@test_app_url}/get?param1=user¶m2=password") expect(response.status).to eq(200) - expect(Span.root!).to be_http_span_with_route("GET /get") + + router_span = Span.find_by_name!("router - /get") expected_request_parameters = { "param1" => "user", "param2" => "password" } - expect(Span.root!).to match_request_parameters(expected_request_parameters) + expect(router_span).to match_request_parameters(expected_request_parameters) end end