From 6849a1dc00fbc73fa7d740aaa3e2fcee79612ae3 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Sat, 16 Sep 2023 20:35:01 +1000 Subject: [PATCH] update quoteservice for php Release Candidate (#1114) * update quoteservice for php Release Candidate - update dependencies to use RC1 and latest versions of other packages - update code to work with RC1 - refactor Dockerfile to allow local dev, and document how * update changelog * markdown lint * adding container detector * adding logging, enabling internal metrics * Add env var to docker-compose.minimal --------- Co-authored-by: Juliano Costa Co-authored-by: Juliano Costa --- CHANGELOG.md | 3 +++ docker-compose.minimal.yml | 1 + docker-compose.yml | 1 + kubernetes/opentelemetry-demo.yaml | 2 ++ src/quoteservice/Dockerfile | 35 ++++++++++++++------------- src/quoteservice/README.md | 18 ++++++++++++++ src/quoteservice/app/dependencies.php | 20 ++++++--------- src/quoteservice/app/routes.php | 8 ++++-- src/quoteservice/composer.json | 20 ++++++++------- src/quoteservice/public/index.php | 10 +------- 10 files changed, 69 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e3a8d11f..212c3a12bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ release. ## Unreleased +* update PHP quoteservice to use RC1 + ([#1114](https://github.com/open-telemetry/opentelemetry-demo/pull/1114)) + ## 1.5.0 * update trace-based tests to test stream events diff --git a/docker-compose.minimal.yml b/docker-compose.minimal.yml index 4461810b6a..b958a4b2ce 100644 --- a/docker-compose.minimal.yml +++ b/docker-compose.minimal.yml @@ -375,6 +375,7 @@ services: - QUOTE_SERVICE_PORT - OTEL_RESOURCE_ATTRIBUTES - OTEL_SERVICE_NAME=quoteservice + - OTEL_PHP_INTERNAL_METRICS_ENABLED=true depends_on: otelcol: condition: service_started diff --git a/docker-compose.yml b/docker-compose.yml index 1eddf24ce6..d6172a936a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -466,6 +466,7 @@ services: - QUOTE_SERVICE_PORT - OTEL_RESOURCE_ATTRIBUTES - OTEL_SERVICE_NAME=quoteservice + - OTEL_PHP_INTERNAL_METRICS_ENABLED=true depends_on: otelcol: condition: service_started diff --git a/kubernetes/opentelemetry-demo.yaml b/kubernetes/opentelemetry-demo.yaml index 2935446617..8de7fb8490 100644 --- a/kubernetes/opentelemetry-demo.yaml +++ b/kubernetes/opentelemetry-demo.yaml @@ -8276,6 +8276,8 @@ spec: value: "8080" - name: OTEL_PHP_AUTOLOAD_ENABLED value: "true" + - name: OTEL_PHP_INTERNAL_METRICS_ENABLED + value: "true" - name: OTEL_EXPORTER_OTLP_ENDPOINT value: http://$(OTEL_COLLECTOR_NAME):4318 - name: OTEL_RESOURCE_ATTRIBUTES diff --git a/src/quoteservice/Dockerfile b/src/quoteservice/Dockerfile index 60a4993b26..9f3079969a 100644 --- a/src/quoteservice/Dockerfile +++ b/src/quoteservice/Dockerfile @@ -2,7 +2,22 @@ # SPDX-License-Identifier: Apache-2.0 -FROM composer:2.5 AS build +FROM php:8.2-cli as base + +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 \ + opentelemetry + +WORKDIR /var/www +CMD php public/index.php +USER www-data +EXPOSE ${QUOTE_SERVICE_PORT} + +FROM composer:2.6 AS vendor WORKDIR /tmp/ COPY ./src/quoteservice/composer.json . @@ -15,21 +30,7 @@ RUN composer install \ --no-dev \ --prefer-dist -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 \ - opentelemetry-beta - -WORKDIR /var/www -COPY --from=build /tmp/vendor/ ./vendor/ +FROM base as final +COPY --from=vendor /tmp/vendor/ ./vendor/ COPY ./src/quoteservice/ /var/www -CMD php public/index.php - -USER www-data -EXPOSE ${QUOTE_SERVICE_PORT} diff --git a/src/quoteservice/README.md b/src/quoteservice/README.md index 7e555e69c1..e138442bd7 100644 --- a/src/quoteservice/README.md +++ b/src/quoteservice/README.md @@ -26,3 +26,21 @@ In order to get traffic into the service you have to deploy the whole opentelemetry-demo. Please follow the root README to do so. + +## Development + +To build and run the quote service locally: + +```sh +docker build src/quoteservice --target base -t quoteservice +cd src/quoteservice +docker run --rm -it -v $(pwd):/var/www -e QUOTE_SERVICE_PORT=8999 -p "8999:8999" quoteservice +``` + +Then, send some curl requests: + +```sh +curl --location 'http://localhost:8999/getquote' \ +--header 'Content-Type: application/json' \ +--data '{"numberOfItems":3}' +``` diff --git a/src/quoteservice/app/dependencies.php b/src/quoteservice/app/dependencies.php index 9acf2ba458..5d9ee1d0d9 100644 --- a/src/quoteservice/app/dependencies.php +++ b/src/quoteservice/app/dependencies.php @@ -8,27 +8,23 @@ use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; -use Monolog\Handler\StreamHandler; +use OpenTelemetry\API\Globals; +use OpenTelemetry\Contrib\Logs\Monolog\Handler; use Monolog\Logger; -use Monolog\Processor\UidProcessor; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; return function (ContainerBuilder $containerBuilder) { $containerBuilder->addDefinitions([ LoggerInterface::class => function (ContainerInterface $c) { $settings = $c->get(SettingsInterface::class); - $loggerSettings = $settings->get('logger'); - $logger = new Logger($loggerSettings['name']); - - $processor = new UidProcessor(); - $logger->pushProcessor($processor); - - $handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']); - $logger->pushHandler($handler); - - return $logger; + $handler = new Handler( + Globals::loggerProvider(), + LogLevel::INFO, + ); + return new Logger($loggerSettings['name'], [$handler]); }, ]); }; diff --git a/src/quoteservice/app/routes.php b/src/quoteservice/app/routes.php index 54a83fb0cc..126007ebd9 100644 --- a/src/quoteservice/app/routes.php +++ b/src/quoteservice/app/routes.php @@ -4,11 +4,12 @@ -use OpenTelemetry\API\Common\Instrumentation\Globals; +use OpenTelemetry\API\Globals; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Log\LoggerInterface; use Slim\App; function calculateQuote($jsonObject): float @@ -40,7 +41,7 @@ function calculateQuote($jsonObject): float } return function (App $app) { - $app->post('/getquote', function (Request $request, Response $response) { + $app->post('/getquote', function (Request $request, Response $response, LoggerInterface $logger) { $span = Span::getCurrent(); $span->addEvent('Received get quote request, processing it'); @@ -54,6 +55,9 @@ function calculateQuote($jsonObject): float $span->addEvent('Quote processed, response sent back', [ 'app.quote.cost.total' => $data ]); + $logger->info('Calculated quote', [ + 'total' => $data, + ]); return $response ->withHeader('Content-Type', 'application/json'); diff --git a/src/quoteservice/composer.json b/src/quoteservice/composer.json index a44bcd95f8..0956252e11 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -7,18 +7,20 @@ "php": ">= 8.2", "ext-json": "*", "ext-pcntl": "*", - "monolog/monolog": "3.3.1", - "open-telemetry/api": "1.0.0beta10", - "open-telemetry/sdk": "1.0.0beta14", - "open-telemetry/exporter-otlp": "1.0.0beta12", - "open-telemetry/opentelemetry-auto-slim": "1.0.0beta9", - "guzzlehttp/guzzle": "7.5.1", - "php-di/php-di": "7.0.3", - "php-di/slim-bridge": "3.3.0", + "monolog/monolog": "3.4.0", + "open-telemetry/api": "1.0.0RC1", + "open-telemetry/sdk": "1.0.0RC1", + "open-telemetry/exporter-otlp": "1.0.0RC1", + "open-telemetry/opentelemetry-auto-slim": "1.0.0beta10", + "open-telemetry/detector-container": "0.0.3", + "open-telemetry/opentelemetry-logger-monolog": "0.0.4", + "guzzlehttp/guzzle": "7.8.0", + "php-di/php-di": "7.0.5", + "php-di/slim-bridge": "3.4.0", "php-http/guzzle7-adapter": "1.0.0", "react/http": "v1.9.0", "slim/psr7": "1.6.1", - "slim/slim": "4.11.0" + "slim/slim": "4.12.0" }, "autoload": { "psr-4": { diff --git a/src/quoteservice/public/index.php b/src/quoteservice/public/index.php index 6030b9e40a..c5c7e775ce 100644 --- a/src/quoteservice/public/index.php +++ b/src/quoteservice/public/index.php @@ -8,16 +8,12 @@ use DI\Bridge\Slim\Bridge; use DI\ContainerBuilder; -use Monolog\Handler\StreamHandler; -use Monolog\Logger; -use OpenTelemetry\API\Common\Instrumentation\Globals; -use OpenTelemetry\API\Common\Log\LoggerHolder; +use OpenTelemetry\API\Globals; use OpenTelemetry\SDK\Common\Configuration\Configuration; use OpenTelemetry\SDK\Common\Configuration\Variables; use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Trace\TracerProviderInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Log\LogLevel; use React\EventLoop\Loop; use React\Http\HttpServer; use React\Socket\SocketServer; @@ -25,10 +21,6 @@ require __DIR__ . '/../vendor/autoload.php'; -LoggerHolder::set( - new Logger('otel-php', [new StreamHandler('php://stdout', LogLevel::DEBUG)]) -); - // Instantiate PHP-DI ContainerBuilder $containerBuilder = new ContainerBuilder();