Skip to content

Commit

Permalink
update quoteservice for php Release Candidate (open-telemetry#1114)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Juliano Costa <[email protected]>
  • Loading branch information
3 people authored and Dylan Myers committed Sep 19, 2023
1 parent c6a917b commit 2bdd8bf
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docker-compose.minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions kubernetes/opentelemetry-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions src/quoteservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand All @@ -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}
18 changes: 18 additions & 0 deletions src/quoteservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
```
20 changes: 8 additions & 12 deletions src/quoteservice/app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
]);
};
8 changes: 6 additions & 2 deletions src/quoteservice/app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');

Expand All @@ -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');
Expand Down
20 changes: 11 additions & 9 deletions src/quoteservice/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 1 addition & 9 deletions src/quoteservice/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@

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;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

LoggerHolder::set(
new Logger('otel-php', [new StreamHandler('php://stdout', LogLevel::DEBUG)])
);

// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();

Expand Down

0 comments on commit 2bdd8bf

Please sign in to comment.