|
| 1 | +# quote service |
| 2 | + |
| 3 | +This service is responsible for calculating shipping costs, based on |
| 4 | +the number of items to be shipped. The quote service is called from |
| 5 | +Shipping Service via HTTP. |
| 6 | + |
| 7 | +The Quote Service is implemented using the Slim framework and |
| 8 | +php-di for managing the Dependency Injection. |
| 9 | + |
| 10 | +The PHP instrumentation may vary when using a different framework. |
| 11 | + |
| 12 | +[Quote service source](../../src/quoteservice/) |
| 13 | + |
| 14 | +## Traces |
| 15 | + |
| 16 | +### Initialize tracer provider |
| 17 | + |
| 18 | +The OpenTelemetry SDK is initialized from `index`. |
| 19 | + |
| 20 | +```php |
| 21 | + $tracerProvider = (new TracerProviderFactory('quoteservice'))->create(); |
| 22 | + ShutdownHandler::register([$tracerProvider, 'shutdown']); |
| 23 | + $tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); |
| 24 | + |
| 25 | + $containerBuilder->addDefinitions([ |
| 26 | + Tracer::class => $tracer |
| 27 | + ]); |
| 28 | +``` |
| 29 | + |
| 30 | +You should call `$tracerProvider->shutdown()` when your service is shutdown to |
| 31 | +ensure all spans are exported. |
| 32 | + |
| 33 | +### Adding HTTP instrumentation |
| 34 | + |
| 35 | +This service receives HTTP requests, which are instrumented in the middleware. |
| 36 | + |
| 37 | +The middleware starts root span based on route pattern, sets span status |
| 38 | +from http code. |
| 39 | + |
| 40 | +```php |
| 41 | + $app->add(function (Request $request, RequestHandler $handler) use ($tracer) { |
| 42 | + $parent = TraceContextPropagator::getInstance()->extract($request->getHeaders()); |
| 43 | + $routeContext = RouteContext::fromRequest($request); |
| 44 | + $route = $routeContext->getRoute(); |
| 45 | + $root = $tracer->spanBuilder($route->getPattern()) |
| 46 | + ->setStartTimestamp((int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * 1e9)) |
| 47 | + ->setParent($parent) |
| 48 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 49 | + ->startSpan(); |
| 50 | + $scope = $root->activate(); |
| 51 | + |
| 52 | + try { |
| 53 | + $response = $handler->handle($request); |
| 54 | + $root->setStatus($response->getStatusCode() < 500 ? StatusCode::STATUS_OK : StatusCode::STATUS_ERROR); |
| 55 | + } finally { |
| 56 | + $root->end(); |
| 57 | + $scope->detach(); |
| 58 | + } |
| 59 | + |
| 60 | + return $response; |
| 61 | + }); |
| 62 | +``` |
| 63 | + |
| 64 | +This is enough to get a new span every time a new request is received by the service. |
| 65 | + |
| 66 | +Note that the `root` span is created with `setParent($parent)` which is coming from |
| 67 | +the request headers. This is required to ensure Context Propagation. |
| 68 | + |
| 69 | +### Add span attributes |
| 70 | + |
| 71 | +Within the definition of routes, you can get current span using |
| 72 | +`OpenTelemetry\API\Trace\AbstractSpan`. |
| 73 | + |
| 74 | +```php |
| 75 | + $span = AbstractSpan::getCurrent(); |
| 76 | +``` |
| 77 | + |
| 78 | +Adding attributes to a span is accomplished using `setAttribute` on the span |
| 79 | +object. In the `calculateQuote` function 2 attributes are added to the `childSpan`. |
| 80 | + |
| 81 | +```php |
| 82 | + $childSpan->setAttribute('app.quote.items.count', $numberOfItems); |
| 83 | + $childSpan->setAttribute('app.quote.cost.total', $quote); |
| 84 | +``` |
| 85 | + |
| 86 | +### Add span events |
| 87 | + |
| 88 | +Adding span events is accomplished using `addEvent` on the span object. In the |
| 89 | +`getquote` route span events are added. Some events have |
| 90 | +additional attributes, others do not. |
| 91 | + |
| 92 | +Adding a span event without attributes: |
| 93 | + |
| 94 | +```php |
| 95 | + $span->addEvent('Received get quote request, processing it'); |
| 96 | +``` |
| 97 | + |
| 98 | +Adding a span event with additional attributes: |
| 99 | + |
| 100 | +```php |
| 101 | + $span->addEvent('Quote processed, response sent back', [ |
| 102 | + 'app.quote.cost.total' => $payload |
| 103 | + ]); |
| 104 | +``` |
| 105 | + |
| 106 | +## Metrics |
| 107 | + |
| 108 | +TBD |
| 109 | + |
| 110 | +## Logs |
| 111 | + |
| 112 | +TBD |
0 commit comments