Skip to content

Commit 9f96b59

Browse files
authored
Exclude ApiPlatform test HttpClient from instrumentation (#235)
1 parent 24f2151 commit 9f96b59

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/Instrumentation/Symfony/src/HttpClientInstrumentation.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818

1919
final class HttpClientInstrumentation
2020
{
21+
/**
22+
* These clients are not supported by this instrumentation, because
23+
* they are synchronous and do not support the on_progress option.
24+
*/
25+
const SYNCHRONOUS_CLIENTS = [
26+
/** @psalm-suppress UndefinedClass */
27+
'\ApiPlatform\Symfony\Bundle\Test\Client',
28+
];
29+
30+
public static function supportsProgress(string $class): bool
31+
{
32+
return false === in_array($class, self::SYNCHRONOUS_CLIENTS);
33+
}
34+
2135
public static function register(): void
2236
{
2337
$instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.symfony_http');
@@ -58,6 +72,16 @@ public static function register(): void
5872
$requestOptions['headers'] = [];
5973
}
6074

75+
/** @psalm-suppress UndefinedClass */
76+
if (false === self::supportsProgress($class)) {
77+
$context = $span->storeInContext($parent);
78+
$propagator->inject($requestOptions['headers'], ArrayAccessGetterSetter::getInstance(), $context);
79+
80+
Context::storage()->attach($context);
81+
82+
return $params;
83+
}
84+
6185
$previousOnProgress = $requestOptions['on_progress'] ?? null;
6286

6387
//As Response are lazy we end span when status code was received
@@ -75,7 +99,6 @@ public static function register(): void
7599
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $statusCode);
76100

77101
if ($statusCode >= 400 && $statusCode < 600) {
78-
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $statusCode);
79102
$span->setStatus(StatusCode::STATUS_ERROR);
80103
}
81104

@@ -110,10 +133,20 @@ public static function register(): void
110133
]);
111134
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
112135
$span->end();
136+
137+
return;
138+
}
139+
140+
if ($response !== null && false === self::supportsProgress(get_class($client))) {
141+
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode());
142+
143+
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
144+
$span->setStatus(StatusCode::STATUS_ERROR);
145+
}
113146
}
114147

115-
//As Response are lazy we end span after response is received,
116-
//it's added in on_progress callback, see line 63
148+
// As most Response are lazy we end span after response is received,
149+
// it's added in on_progress callback, see line 69
117150
},
118151
);
119152
}

0 commit comments

Comments
 (0)