From 3b90c462e733b04237d9dd762393fb552656802f Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 14 Sep 2023 10:11:41 +1000 Subject: [PATCH 1/6] 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 --- src/quoteservice/Dockerfile | 35 ++++++++++++++++--------------- src/quoteservice/README.md | 17 +++++++++++++++ src/quoteservice/app/routes.php | 2 +- src/quoteservice/composer.json | 18 ++++++++-------- src/quoteservice/public/index.php | 10 +-------- 5 files changed, 46 insertions(+), 36 deletions(-) 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..3918d21774 100644 --- a/src/quoteservice/README.md +++ b/src/quoteservice/README.md @@ -26,3 +26,20 @@ 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/routes.php b/src/quoteservice/app/routes.php index 54a83fb0cc..50788d4659 100644 --- a/src/quoteservice/app/routes.php +++ b/src/quoteservice/app/routes.php @@ -4,7 +4,7 @@ -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; diff --git a/src/quoteservice/composer.json b/src/quoteservice/composer.json index a44bcd95f8..d7d102edfb 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -7,18 +7,18 @@ "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", + "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(); From a1db01b51f89a1c2166f7fdf2ee22a981b88fe78 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 14 Sep 2023 10:17:58 +1000 Subject: [PATCH 2/6] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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 From 33f48b0bbd66eda4f2c20eb8e55ef77f74ace2ea Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 14 Sep 2023 10:20:32 +1000 Subject: [PATCH 3/6] markdown lint --- src/quoteservice/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/quoteservice/README.md b/src/quoteservice/README.md index 3918d21774..e138442bd7 100644 --- a/src/quoteservice/README.md +++ b/src/quoteservice/README.md @@ -38,6 +38,7 @@ docker run --rm -it -v $(pwd):/var/www -e QUOTE_SERVICE_PORT=8999 -p "8999:8999" ``` Then, send some curl requests: + ```sh curl --location 'http://localhost:8999/getquote' \ --header 'Content-Type: application/json' \ From a3aa368490f4e95952dfc7edc433de18b4efb4c7 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 15 Sep 2023 15:49:50 +1000 Subject: [PATCH 4/6] adding container detector --- src/quoteservice/composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/quoteservice/composer.json b/src/quoteservice/composer.json index d7d102edfb..39c3cfe8ed 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -12,6 +12,7 @@ "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", "guzzlehttp/guzzle": "7.8.0", "php-di/php-di": "7.0.5", "php-di/slim-bridge": "3.4.0", From e2afa015190531f954ecc617f1549294cd313284 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 15 Sep 2023 18:14:38 +1000 Subject: [PATCH 5/6] adding logging, enabling internal metrics --- docker-compose.yml | 1 + kubernetes/opentelemetry-demo.yaml | 2 ++ src/quoteservice/app/dependencies.php | 20 ++++++++------------ src/quoteservice/app/routes.php | 6 +++++- src/quoteservice/composer.json | 1 + 5 files changed, 17 insertions(+), 13 deletions(-) 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/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 50788d4659..126007ebd9 100644 --- a/src/quoteservice/app/routes.php +++ b/src/quoteservice/app/routes.php @@ -9,6 +9,7 @@ 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 39c3cfe8ed..0956252e11 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -13,6 +13,7 @@ "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", From daa522b31432e320cc96616059ec3a139f4883a3 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Fri, 15 Sep 2023 11:07:17 +0200 Subject: [PATCH 6/6] Add env var to docker-compose.minimal --- docker-compose.minimal.yml | 1 + 1 file changed, 1 insertion(+) 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