77import com .alllexe .orderservice .model .OrderLineItems ;
88import com .alllexe .orderservice .repository .OrderRepository ;
99import lombok .RequiredArgsConstructor ;
10+ import org .springframework .cloud .sleuth .Span ;
11+ import org .springframework .cloud .sleuth .Tracer ;
1012import org .springframework .stereotype .Service ;
1113import org .springframework .transaction .annotation .Transactional ;
1214import org .springframework .web .reactive .function .client .WebClient ;
@@ -21,6 +23,7 @@ public class OrderService {
2123
2224 private final OrderRepository orderRepository ;
2325 private final WebClient .Builder webClientBuilder ;
26+ private final Tracer tracer ;
2427
2528 @ Transactional
2629 public String placeOrder (OrderRequest orderRequest ) {
@@ -37,22 +40,27 @@ public String placeOrder(OrderRequest orderRequest) {
3740 .map (OrderLineItems ::getSkuCode )
3841 .toList ();
3942
40- // Call Inventory Service, and place order if product is in stock
41- InventoryResponse [] inventoryResponseArray = webClientBuilder .build ().get ()
42- .uri ("http://inventory-service/api/inventory" ,
43- uriBuilder -> uriBuilder .queryParam ("skuCodes" , skuCodes ).build ())
44- .retrieve ()
45- .bodyToMono (InventoryResponse [].class )
46- .block ();
43+ Span inventoryServiceLookup = tracer .nextSpan ().name ("InventoryServiceLookup" );
44+ try (Tracer .SpanInScope tracerInScope = tracer .withSpan (inventoryServiceLookup .start ())) {
45+ // Call Inventory Service, and place order if product is in stock
46+ InventoryResponse [] inventoryResponseArray = webClientBuilder .build ().get ()
47+ .uri ("http://inventory-service/api/inventory" ,
48+ uriBuilder -> uriBuilder .queryParam ("skuCodes" , skuCodes ).build ())
49+ .retrieve ()
50+ .bodyToMono (InventoryResponse [].class )
51+ .block ();
4752
48- boolean allProductsInStock = Arrays .stream (inventoryResponseArray )
49- .allMatch (InventoryResponse ::isInStock );
53+ boolean allProductsInStock = Arrays .stream (inventoryResponseArray )
54+ .allMatch (InventoryResponse ::isInStock );
5055
51- if (allProductsInStock ) {
52- orderRepository .save (order );
53- return "Order placed successfully" ;
54- } else {
55- throw new IllegalArgumentException ("Product is not in stock" );
56+ if (allProductsInStock ) {
57+ orderRepository .save (order );
58+ return "Order placed successfully" ;
59+ } else {
60+ throw new IllegalArgumentException ("Product is not in stock" );
61+ }
62+ } finally {
63+ inventoryServiceLookup .end ();
5664 }
5765 }
5866
0 commit comments