Skip to content

Commit b7595d6

Browse files
author
Juliano Costa
authored
add quoteservice docs (#392)
1 parent 3a5df14 commit b7595d6

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

docs/service_table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ View [Service Graph](../README.md#architecture) to visualize request flows.
1414
| [loadgenerator](../src/loadgenerator/README.md) | Python/Locust | Continuously sends requests imitating realistic user shopping flows to the frontend. |
1515
| [paymentservice](../src/paymentservice/README.md) | JavaScript | Charges the given credit card info (mock) with the given amount and returns a transaction ID. |
1616
| [productcatalogservice](../src/productcatalogservice/README.md) | Go | Provides the list of products from a JSON file and ability to search products and get individual products. |
17-
| [quoteservice](../src/quoteservice/README.md) | PHP | Calculates the shipping costs, based on the number of items to be shipped. |
17+
| [quoteservice](services/quoteservice.md) | PHP | Calculates the shipping costs, based on the number of items to be shipped. |
1818
| [recommendationservice](../src/recommendationservice/README.md) | Python | Recommends other products based on what's given in the cart. |
1919
| [shippingservice](../src/shippingservice/README.md) | Rust | Gives shipping cost estimates based on the shopping cart. Ships items to the given address (mock). |

docs/services/quoteservice.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

src/quoteservice/app/routes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ function calculateQuote($jsonObject, Tracer $tracer): float
4646
$payload = json_encode($data);
4747
$response->getBody()->write($payload);
4848

49-
$span->addEvent('Quote processed, response sent back');
49+
$span->addEvent('Quote processed, response sent back', [
50+
'app.quote.cost.total' => $data
51+
]);
5052

5153
return $response
5254
->withHeader('Content-Type', 'application/json');

0 commit comments

Comments
 (0)