Skip to content

Commit

Permalink
[quoteservice] manual metrics, logs export (open-telemetry#1519)
Browse files Browse the repository at this point in the history
* [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 <pierre@pierretessier.com>
Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
  • Loading branch information
3 people authored and AlexPSplunk committed Jul 10, 2024
1 parent 3358f53 commit 9833348
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion src/quoteservice/app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
]);
Expand Down
4 changes: 2 additions & 2 deletions src/quoteservice/app/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +22,7 @@
'logger' => [
'name' => 'slim-app',
'path' => 'php://stdout',
'level' => Logger::DEBUG,
'level' => LogLevel::DEBUG,
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/quoteservice/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/quoteservice/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions test/tracetesting/shipping-service/quote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9833348

Please sign in to comment.