From d83f8e0219944e853cd59025a03865a245d08ae8 Mon Sep 17 00:00:00 2001 From: Evandro Poianas Date: Sat, 5 Oct 2024 11:24:53 +0200 Subject: [PATCH] Add peer.service attribute to psr18 spans --- src/Instrumentation/Psr18/src/Psr18Instrumentation.php | 1 + .../Psr18/tests/Integration/Psr18InstrumentationTest.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/Instrumentation/Psr18/src/Psr18Instrumentation.php b/src/Instrumentation/Psr18/src/Psr18Instrumentation.php index 0be3127a..bf78917c 100644 --- a/src/Instrumentation/Psr18/src/Psr18Instrumentation.php +++ b/src/Instrumentation/Psr18/src/Psr18Instrumentation.php @@ -49,6 +49,7 @@ public static function register(): void ->spanBuilder(sprintf('%s', $request->getMethod())) ->setParent($parentContext) ->setSpanKind(SpanKind::KIND_CLIENT) + ->setAttribute(TraceAttributes::PEER_SERVICE, $request->getUri()->getHost()) ->setAttribute(TraceAttributes::URL_FULL, (string) $request->getUri()) ->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->getMethod()) ->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion()) diff --git a/src/Instrumentation/Psr18/tests/Integration/Psr18InstrumentationTest.php b/src/Instrumentation/Psr18/tests/Integration/Psr18InstrumentationTest.php index f00e9d17..bd12e33f 100644 --- a/src/Instrumentation/Psr18/tests/Integration/Psr18InstrumentationTest.php +++ b/src/Instrumentation/Psr18/tests/Integration/Psr18InstrumentationTest.php @@ -80,6 +80,8 @@ public function test_send_request(string $method, string $uri, int $statusCode): $span = $this->storage[0]; $this->assertStringContainsString($method, $span->getName()); + $this->assertTrue($span->getAttributes()->has(TraceAttributes::PEER_SERVICE)); + $this->assertSame(parse_url($uri)['host'] ?? null, $span->getAttributes()->get(TraceAttributes::PEER_SERVICE)); $this->assertTrue($span->getAttributes()->has(TraceAttributes::URL_FULL)); $this->assertSame($uri, $span->getAttributes()->get(TraceAttributes::URL_FULL)); $this->assertTrue($span->getAttributes()->has(TraceAttributes::HTTP_REQUEST_METHOD));