From 9833348748dc54e1a86d466f7723ec6a245c024c Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 11 Apr 2024 18:07:59 +1000 Subject: [PATCH] [quoteservice] manual metrics, logs export (#1519) * [quoteservice] adding manual metrics to quoteservice - add a manual metric to the php quote service - randomize per-item cost to add some variability to quotes - bump dependencies to latest - fix a monolog deprecation - ensure logs are exported per the configured delay * update changelog * fix * rename variable * remove total cost from metric attributes * quote service returns randomized cost --------- Co-authored-by: Pierre Tessier Co-authored-by: Juliano Costa --- CHANGELOG.md | 2 ++ src/quoteservice/app/routes.php | 11 ++++++++++- src/quoteservice/app/settings.php | 4 ++-- src/quoteservice/composer.json | 2 +- src/quoteservice/public/index.php | 6 ++++++ test/tracetesting/shipping-service/quote.yaml | 3 +-- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d900116059..021d602ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ the release. ([#1507](https://github.com/open-telemetry/opentelemetry-demo/pull/1507)) * [cartservice] update .NET package to 1.8.0 release ([#1514](https://github.com/open-telemetry/opentelemetry-demo/pull/1514)) +* [quoteservice] add manual metric, export logs periodically + ([#1519](https://github.com/open-telemetry/opentelemetry-demo/pull/1519)) ## 1.9.0 diff --git a/src/quoteservice/app/routes.php b/src/quoteservice/app/routes.php index 126007ebd9..2c2231aa56 100644 --- a/src/quoteservice/app/routes.php +++ b/src/quoteservice/app/routes.php @@ -26,12 +26,20 @@ function calculateQuote($jsonObject): float throw new \InvalidArgumentException('numberOfItems not provided'); } $numberOfItems = intval($jsonObject['numberOfItems']); - $quote = round(8.90 * $numberOfItems, 2); + $costPerItem = rand(400, 1000)/10; + $quote = round($costPerItem * $numberOfItems, 2); $childSpan->setAttribute('app.quote.items.count', $numberOfItems); $childSpan->setAttribute('app.quote.cost.total', $quote); $childSpan->addEvent('Quote calculated, returning its value'); + + //manual metrics + static $counter; + $counter ??= Globals::meterProvider() + ->getMeter('quotes') + ->createCounter('quotes', 'quotes', 'number of quotes calculated'); + $counter->add(1, ['number_of_items' => $numberOfItems]); } catch (\Exception $exception) { $childSpan->recordException($exception); } finally { @@ -55,6 +63,7 @@ function calculateQuote($jsonObject): float $span->addEvent('Quote processed, response sent back', [ 'app.quote.cost.total' => $data ]); + //exported as an opentelemetry log (see dependencies.php) $logger->info('Calculated quote', [ 'total' => $data, ]); diff --git a/src/quoteservice/app/settings.php b/src/quoteservice/app/settings.php index 11e7b226f4..dd4774e134 100644 --- a/src/quoteservice/app/settings.php +++ b/src/quoteservice/app/settings.php @@ -9,7 +9,7 @@ use App\Application\Settings\Settings; use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; -use Monolog\Logger; +use Psr\Log\LogLevel; return function (ContainerBuilder $containerBuilder) { // Global Settings Object @@ -22,7 +22,7 @@ 'logger' => [ 'name' => 'slim-app', 'path' => 'php://stdout', - 'level' => Logger::DEBUG, + 'level' => LogLevel::DEBUG, ], ]); } diff --git a/src/quoteservice/composer.json b/src/quoteservice/composer.json index 83386dc0ba..9bfbed15e6 100644 --- a/src/quoteservice/composer.json +++ b/src/quoteservice/composer.json @@ -9,7 +9,7 @@ "monolog/monolog": "3.5.0", "open-telemetry/api": "1.0.3", "open-telemetry/sdk": "1.0.8", - "open-telemetry/exporter-otlp": "1.0.3", + "open-telemetry/exporter-otlp": "1.0.4", "open-telemetry/opentelemetry-auto-slim": "1.0.4", "open-telemetry/detector-container": "1.0.0", "open-telemetry/opentelemetry-logger-monolog": "1.0.0", diff --git a/src/quoteservice/public/index.php b/src/quoteservice/public/index.php index c5c7e775ce..b13085342e 100644 --- a/src/quoteservice/public/index.php +++ b/src/quoteservice/public/index.php @@ -11,6 +11,7 @@ use OpenTelemetry\API\Globals; use OpenTelemetry\SDK\Common\Configuration\Configuration; use OpenTelemetry\SDK\Common\Configuration\Variables; +use OpenTelemetry\SDK\Logs\LoggerProviderInterface; use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Trace\TracerProviderInterface; use Psr\Http\Message\ServerRequestInterface; @@ -61,6 +62,11 @@ $tracerProvider->forceFlush(); }); } +if (($loggerProvider = Globals::loggerProvider()) instanceof LoggerProviderInterface) { + Loop::addPeriodicTimer(Configuration::getInt(Variables::OTEL_BLRP_SCHEDULE_DELAY)/1000, function() use ($loggerProvider) { + $loggerProvider->forceFlush(); + }); +} if (($meterProvider = Globals::meterProvider()) instanceof MeterProviderInterface) { Loop::addPeriodicTimer(Configuration::getInt(Variables::OTEL_METRIC_EXPORT_INTERVAL)/1000, function() use ($meterProvider) { $meterProvider->forceFlush(); diff --git a/test/tracetesting/shipping-service/quote.yaml b/test/tracetesting/shipping-service/quote.yaml index 945609ea81..c4d8fad6ed 100644 --- a/test/tracetesting/shipping-service/quote.yaml +++ b/test/tracetesting/shipping-service/quote.yaml @@ -37,5 +37,4 @@ spec: selector: span[tracetest.span.type="general" name="Tracetest trigger"] assertions: - attr:tracetest.response.body | json_path '$.costUsd.currencyCode' = "USD" - - attr:tracetest.response.body | json_path '$.costUsd.units' = 17 - - attr:tracetest.response.body | json_path '$.costUsd.nanos' = 800000000 + - attr:tracetest.response.body | json_path '$.costUsd.units' > 0