From 82e03978b89938219958032efb1448cc76baa181 Mon Sep 17 00:00:00 2001 From: Saumit Date: Sat, 27 Sep 2025 02:14:26 +0530 Subject: Initial snapshot - OpenTelemetry demo 2.1.3 -f --- src/quote/app/routes.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/quote/app/routes.php (limited to 'src/quote/app/routes.php') diff --git a/src/quote/app/routes.php b/src/quote/app/routes.php new file mode 100644 index 0000000..2c2231a --- /dev/null +++ b/src/quote/app/routes.php @@ -0,0 +1,74 @@ +getTracer('manual-instrumentation') + ->spanBuilder('calculate-quote') + ->setSpanKind(SpanKind::KIND_INTERNAL) + ->startSpan(); + $childSpan->addEvent('Calculating quote'); + + try { + if (!array_key_exists('numberOfItems', $jsonObject)) { + throw new \InvalidArgumentException('numberOfItems not provided'); + } + $numberOfItems = intval($jsonObject['numberOfItems']); + $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 { + $childSpan->end(); + return $quote; + } +} + +return function (App $app) { + $app->post('/getquote', function (Request $request, Response $response, LoggerInterface $logger) { + $span = Span::getCurrent(); + $span->addEvent('Received get quote request, processing it'); + + $jsonObject = $request->getParsedBody(); + + $data = calculateQuote($jsonObject); + + $payload = json_encode($data); + $response->getBody()->write($payload); + + $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, + ]); + + return $response + ->withHeader('Content-Type', 'application/json'); + }); +}; -- cgit v1.2.3