Skip to content

Commit 05e4eb5

Browse files
authored
use async php runtime (open-telemetry#823)
* use async php runtime To better demonstrate PHP's capabilities, use an async runtime (react/http). This means that batch exporters (traces and metrics) are long-lived and more efficient, and they can now use export delays to only send batches after the configured time has elapsed. Update auto-instrumentation extension to install from PECL (the preferred mechanism, which we've just set up), and bump other dependencies to their latest beta versions. * update changelog
1 parent b234f7e commit 05e4eb5

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,5 @@ significant modifications will be credited to OpenTelemetry Authors.
299299
([#764](https://github.com/open-telemetry/opentelemetry-demo/pull/764))
300300
* [chore] align memory limits with Helm chart
301301
([#781](https://github.com/open-telemetry/opentelemetry-demo/pull/781))
302+
* Use an async PHP runtime, bump versions to latest betas
303+
([#823](https://github.com/open-telemetry/opentelemetry-demo/pull/823))

src/quoteservice/Dockerfile

+4-7
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,21 @@ RUN composer install \
2525
--no-dev \
2626
--prefer-dist
2727

28-
FROM php:8.2-apache
28+
FROM php:8.2-cli
2929

3030
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
3131
RUN chmod +x /usr/local/bin/install-php-extensions \
3232
&& install-php-extensions \
3333
opcache \
34+
pcntl \
3435
protobuf \
35-
open-telemetry/opentelemetry-php-instrumentation@1.0.0beta2
36+
opentelemetry-1.0.0beta3
3637

3738
WORKDIR /var/www
3839
COPY --from=build /tmp/vendor/ ./vendor/
3940
COPY ./src/quoteservice/ /var/www
4041

41-
ENV APACHE_DOCUMENT_ROOT /var/www/public
42-
RUN sed -ri -e 's|/var/www/html|${APACHE_DOCUMENT_ROOT}|g' /etc/apache2/sites-available/*.conf \
43-
&& a2enmod rewrite \
44-
&& echo "ServerName quoteservice" >> /etc/apache2/apache2.conf \
45-
&& sed -i "s/80/\$\{QUOTE_SERVICE_PORT\}/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
42+
CMD php public/index.php
4643

4744
USER www-data
4845
EXPOSE ${QUOTE_SERVICE_PORT}

src/quoteservice/app/routes.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ function calculateQuote($jsonObject): float
5353
$span = Span::getCurrent();
5454
$span->addEvent('Received get quote request, processing it');
5555

56-
$body = $request->getBody()->getContents();
57-
$jsonObject = json_decode($body, true);
56+
$jsonObject = $request->getParsedBody();
5857

5958
$data = calculateQuote($jsonObject);
6059

src/quoteservice/composer.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
"require": {
77
"php": ">= 8.2",
88
"ext-json": "*",
9-
"monolog/monolog": "2.8.0",
10-
"open-telemetry/api": "1.0.0beta4",
11-
"open-telemetry/sdk": "1.0.0beta3",
12-
"open-telemetry/exporter-otlp": "1.0.0beta3",
13-
"open-telemetry/opentelemetry-auto-slim": "1.0.0beta4",
9+
"monolog/monolog": "3.3.1",
10+
"open-telemetry/api": "1.0.0beta5",
11+
"open-telemetry/sdk": "1.0.0beta6",
12+
"open-telemetry/exporter-otlp": "1.0.0beta5",
13+
"open-telemetry/opentelemetry-auto-slim": "1.0.0beta7",
1414
"guzzlehttp/guzzle": "7.4.5",
1515
"php-di/php-di": "6.4.0",
1616
"php-di/slim-bridge": "3.2.0",
1717
"php-http/guzzle7-adapter": "1.0.0",
18+
"react/http": "v1.8.0",
1819
"slim/psr7": "1.5",
1920
"slim/slim": "4.10.0"
2021
},

src/quoteservice/public/.htaccess

-18
This file was deleted.

src/quoteservice/public/index.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
use Monolog\Handler\StreamHandler;
2121
use Monolog\Logger;
2222
use OpenTelemetry\API\Common\Log\LoggerHolder;
23+
use Psr\Http\Message\ServerRequestInterface;
2324
use Psr\Log\LogLevel;
25+
use React\EventLoop\Loop;
26+
use React\Http\HttpServer;
27+
use React\Socket\SocketServer;
2428
use Slim\Factory\AppFactory;
2529

2630
require __DIR__ . '/../vendor/autoload.php';
@@ -59,6 +63,26 @@
5963

6064
// Add Error Middleware
6165
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
66+
Loop::get()->addSignal(SIGTERM, function() {
67+
exit;
68+
});
6269

63-
// Run App
64-
$app->run();
70+
$server = new HttpServer(function (ServerRequestInterface $request) use ($app) {
71+
$response = $app->handle($request);
72+
echo sprintf('[%s] "%s %s HTTP/%s" %d %d %s',
73+
date('Y-m-d H:i:sP'),
74+
$request->getMethod(),
75+
$request->getUri()->getPath(),
76+
$request->getProtocolVersion(),
77+
$response->getStatusCode(),
78+
$response->getBody()->getSize(),
79+
PHP_EOL,
80+
);
81+
82+
return $response;
83+
});
84+
$address = '0.0.0.0:' . getenv('QUOTE_SERVICE_PORT');
85+
$socket = new SocketServer($address);
86+
$server->listen($socket);
87+
88+
echo "Listening on: {$address}" . PHP_EOL;

0 commit comments

Comments
 (0)