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

[quoteservice] manual metrics, logs export #1519

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading