Skip to content

Commit

Permalink
use async php runtime (open-telemetry#823)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
brettmc committed Apr 5, 2023
1 parent b4196bb commit cc0d1cf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,5 @@ significant modifications will be credited to OpenTelemetry Authors.
([#764](https://github.com/open-telemetry/opentelemetry-demo/pull/764))
* [chore] align memory limits with Helm chart
([#781](https://github.com/open-telemetry/opentelemetry-demo/pull/781))
* Use an async PHP runtime, bump versions to latest betas
([#823](https://github.com/open-telemetry/opentelemetry-demo/pull/823))
11 changes: 4 additions & 7 deletions src/quoteservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ RUN composer install \
--no-dev \
--prefer-dist

FROM php:8.2-apache
FROM php:8.2-cli

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

WORKDIR /var/www
COPY --from=build /tmp/vendor/ ./vendor/
COPY ./src/quoteservice/ /var/www

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

USER www-data
EXPOSE ${QUOTE_SERVICE_PORT}
3 changes: 1 addition & 2 deletions src/quoteservice/app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ function calculateQuote($jsonObject): float
$span = Span::getCurrent();
$span->addEvent('Received get quote request, processing it');

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

$data = calculateQuote($jsonObject);

Expand Down
11 changes: 6 additions & 5 deletions src/quoteservice/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
"require": {
"php": ">= 8.2",
"ext-json": "*",
"monolog/monolog": "2.8.0",
"open-telemetry/api": "1.0.0beta4",
"open-telemetry/sdk": "1.0.0beta3",
"open-telemetry/exporter-otlp": "1.0.0beta3",
"open-telemetry/opentelemetry-auto-slim": "1.0.0beta4",
"monolog/monolog": "3.3.1",
"open-telemetry/api": "1.0.0beta5",
"open-telemetry/sdk": "1.0.0beta6",
"open-telemetry/exporter-otlp": "1.0.0beta5",
"open-telemetry/opentelemetry-auto-slim": "1.0.0beta7",
"guzzlehttp/guzzle": "7.4.5",
"php-di/php-di": "6.4.0",
"php-di/slim-bridge": "3.2.0",
"php-http/guzzle7-adapter": "1.0.0",
"react/http": "v1.8.0",
"slim/psr7": "1.5",
"slim/slim": "4.10.0"
},
Expand Down
18 changes: 0 additions & 18 deletions src/quoteservice/public/.htaccess

This file was deleted.

28 changes: 26 additions & 2 deletions src/quoteservice/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OpenTelemetry\API\Common\Log\LoggerHolder;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LogLevel;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Socket\SocketServer;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';
Expand Down Expand Up @@ -59,6 +63,26 @@

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
Loop::get()->addSignal(SIGTERM, function() {
exit;
});

// Run App
$app->run();
$server = new HttpServer(function (ServerRequestInterface $request) use ($app) {
$response = $app->handle($request);
echo sprintf('[%s] "%s %s HTTP/%s" %d %d %s',
date('Y-m-d H:i:sP'),
$request->getMethod(),
$request->getUri()->getPath(),
$request->getProtocolVersion(),
$response->getStatusCode(),
$response->getBody()->getSize(),
PHP_EOL,
);

return $response;
});
$address = '0.0.0.0:' . getenv('QUOTE_SERVICE_PORT');
$socket = new SocketServer($address);
$server->listen($socket);

echo "Listening on: {$address}" . PHP_EOL;

0 comments on commit cc0d1cf

Please sign in to comment.