Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHP quote service #345

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ PAYMENT_SERVICE_ADDR=paymentservice:${PAYMENT_SERVICE_PORT}
PRODUCT_CATALOG_SERVICE_PORT=3550
PRODUCT_CATALOG_SERVICE_ADDR=productcatalogservice:${PRODUCT_CATALOG_SERVICE_PORT}

QUOTE_SERVICE_PORT=8090
QUOTE_SERVICE_ADDR=quoteservice:${QUOTE_SERVICE_PORT}

RECOMMENDATION_SERVICE_PORT=9001
RECOMMENDATION_SERVICE_ADDR=recommendationservice:${RECOMMENDATION_SERVICE_PORT}

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ build
src/frontend/protos
next-env.d.ts
src/frontend/cypress/videos
src/frontend/cypress/screenshots
src/frontend/cypress/screenshots
vendor/
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,28 @@ services:
- otelcol
logging: *logging

quoteservice:
image: ${IMAGE_NAME}:${IMAGE_VERSION}-quoteservice
container_name: quoteservice
build:
context: ./
dockerfile: ./src/quoteservice/Dockerfile
ports:
# remove localhost port b4 merging
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
- "${QUOTE_SERVICE_PORT}:${QUOTE_SERVICE_PORT}"
environment:
# OTEL_EXPORTER_OTLP_TRACES_ENDPOINT # Not working for PHP
- QUOTE_SERVICE_PORT
- OTEL_SERVICE_NAME=quoteservice
- OTEL_EXPORTER_OTLP_ENDPOINT=otelcol:4317
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
- OTEL_TRACES_SAMPLER=parentbased_always_on
- OTEL_TRACES_EXPORTER=otlp
- OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc
- OTEL_PHP_TRACES_PROCESSOR=simple
depends_on:
- otelcol
logging: *logging

# RecommendationService
recommendationservice:
image: ${IMAGE_NAME}:${IMAGE_VERSION}-recommendationservice
Expand Down Expand Up @@ -293,6 +315,7 @@ services:
- "${SHIPPING_SERVICE_PORT}"
environment:
- SHIPPING_SERVICE_PORT
- QUOTE_SERVICE_ADDR
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- OTEL_SERVICE_NAME=shippingservice
depends_on:
Expand Down
4 changes: 4 additions & 0 deletions src/quoteservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dockerignore
.idea
Dockerfile
vendor
7 changes: 7 additions & 0 deletions src/quoteservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea/
.vscode/
/coverage/
/vendor/
/logs/*
!/logs/README.md
.phpunit.result.cache
19 changes: 19 additions & 0 deletions src/quoteservice/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Options All -Indexes

<Files .htaccess>
order allow,deny
deny from all
</Files>

<IfModule mod_rewrite.c>
# Redirect to the public folder
RewriteEngine On
# RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

# Redirect to HTTPS
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
26 changes: 26 additions & 0 deletions src/quoteservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM composer:2.4.1 AS build

WORKDIR /tmp/
COPY ./src/quoteservice/composer.json .

RUN composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \
--prefer-dist

FROM php:8.1-apache-buster
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved

# install GRPC (required for the OTel exporter)
RUN apt-get -y update && apt install -y --no-install-recommends zlib1g-dev && \
pecl install grpc && \
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
docker-php-ext-enable grpc

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

EXPOSE 8090
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved

ENTRYPOINT ["php", "-S", "0.0.0.0:8090", "-t", "public"]
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions src/quoteservice/app/dependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

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;
},
]);
};
27 changes: 27 additions & 0 deletions src/quoteservice/app/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

use OpenTelemetry\API\Trace\AbstractSpan;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;

return function (App $app) {
$app->get('/getquote', function (Request $request, Response $response) {
$span = AbstractSpan::getCurrent();
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved

# do the math here
$data = ['quote' => 32.50];

$span->addEvent('Received get quote request, processing it.');
$span->setAttribute('app.quote.cost.total', $data['quote']);

$payload = json_encode($data);
$response->getBody()->write($payload);

$span->addEvent('Quote processed, response sent back');

return $response
->withHeader('Content-Type', 'application/json');
});
};
25 changes: 25 additions & 0 deletions src/quoteservice/app/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
SettingsInterface::class => function () {
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logError' => false,
'logErrorDetails' => false,
'logger' => [
'name' => 'slim-app',
'path' => 'php://stdout',
'level' => Logger::DEBUG,
],
]);
}
]);
};
33 changes: 33 additions & 0 deletions src/quoteservice/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "openteletry-demo/quoteservice",
"description": "Quote Service part of OpenTelemetry Demo",
"license": "Apache-2.0",
"require": {
"php": "^7.4 || ^8.1",
"ext-json": "*",
"monolog/monolog": "^2.3",
"open-telemetry/opentelemetry": "dev-main",
"guzzlehttp/guzzle": "*",
"php-di/php-di": "^6.3",
"php-di/slim-bridge": "^3.2",
"php-http/guzzle7-adapter": "*",
"slim/psr7": "^1.5",
"slim/slim": "^4.9"
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
},
"config": {
"process-timeout": 0,
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
}
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"scripts": {
"start": "php -S localhost:8080 -t public",
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
"test": "phpunit"
}
}
34 changes: 34 additions & 0 deletions src/quoteservice/public/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Options All -Indexes

<Files .htaccess>
order allow,deny
deny from all
</Files>

<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect to HTTPS
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Some hosts may require you to use the `RewriteBase` directive.
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Loading