4use GuzzleHttp\Psr7\Utils;
5use Psr\Http\Client\ClientExceptionInterface;
6use Psr\Http\Client\ClientInterface;
7use Psr\Http\Message\RequestFactoryInterface;
8use Psr\Log\LoggerInterface;
17 private ClientInterface $client;
18 private RequestFactoryInterface $requestFactory;
19 private LoggerInterface $logger;
25 private string $endpoint;
31 private string $serviceName;
37 private string $hostName;
40 ClientInterface $client,
41 RequestFactoryInterface $requestFactory,
42 LoggerInterface $logger,
47 $this->client = $client;
48 $this->requestFactory = $requestFactory;
49 $this->logger = $logger;
50 $this->endpoint = $uri;
51 $this->serviceName = $serviceName;
52 $this->hostName = $hostName;
57 $spanContexts = $tracerState->getSpanContexts();
58 if ( count( $spanContexts ) === 0 ) {
62 $resourceInfo = array_filter( [
63 'service.name' => $this->serviceName,
64 'host.name' => $this->hostName,
65 "server.socket.address" => $_SERVER[
'SERVER_ADDR'] ??
null,
77 'name' =>
'org.wikimedia.telemetry',
79 'spans' => $spanContexts
86 $request = $this->requestFactory->createRequest(
'POST', $this->endpoint )
87 ->withHeader(
'Content-Type',
'application/json' )
88 ->withBody( Utils::streamFor( json_encode( $data ) ) );
91 $response = $this->client->sendRequest( $request );
92 if ( $response->getStatusCode() !== 200 ) {
93 $this->logger->error(
'Failed to export trace data' );
95 }
catch ( ClientExceptionInterface $e ) {
96 $this->logger->error(
'Failed to connect to exporter', [
'exception' => $e ] );