From 05e4eb503f0b1fc62774660f37a9a8906ff06fa3 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 5 Apr 2023 15:10:29 +1000 Subject: [PATCH] use async php runtime (#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 --- CHANGELOG.md | 2 ++ src/quoteservice/Dockerfile | 11 ++++------- src/quoteservice/app/routes.php | 3 +-- src/quoteservice/composer.json | 11 ++++++----- src/quoteservice/public/.htaccess | 18 ------------------ src/quoteservice/public/index.php | 28 ++++++++++++++++++++++++++-- 6 files changed, 39 insertions(+), 34 deletions(-) delete mode 100644 src/quoteservice/public/.htaccess diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7d80c7e9..eca7230926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/src/quoteservice/Dockerfile b/src/quoteservice/Dockerfile index 20d7040df2..ad9ffa8c71 100644 --- a/src/quoteservice/Dockerfile +++ b/src/quoteservice/Dockerfile @@ -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} diff --git a/src/quoteservice/app/routes.php b/src/quoteservice/app/routes.php index e06438393a..2f7d975229 100644 --- a/src/quoteservice/app/routes.php +++ b/src/quoteservice/app/routes.php @@ -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); diff --git a/src/quoteservice/composer.json b/src/quoteservice/composer.json index c598465f8a..bac9685d7d 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -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" }, diff --git a/src/quoteservice/public/.htaccess b/src/quoteservice/public/.htaccess deleted file mode 100644 index fcbd8f2690..0000000000 --- a/src/quoteservice/public/.htaccess +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -RewriteEngine On -RewriteCond %{REQUEST_FILENAME} !-f -RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^ index.php [QSA,L] diff --git a/src/quoteservice/public/index.php b/src/quoteservice/public/index.php index 36aa1e1642..1022ed445d 100644 --- a/src/quoteservice/public/index.php +++ b/src/quoteservice/public/index.php @@ -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'; @@ -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;