11package mushop .orders .services ;
22
3+ import io .micrometer .core .instrument .DistributionSummary ;
4+ import io .micrometer .core .instrument .MeterRegistry ;
35import mushop .orders .config .OrdersConfigurationProperties ;
46import mushop .orders .entities .*;
57import mushop .orders .repositories .CustomerOrderRepository ;
@@ -38,6 +40,9 @@ public class OrdersService {
3840 @ Autowired
3941 private CustomerOrderRepository customerOrderRepository ;
4042
43+ @ Autowired
44+ private MeterRegistry meterRegistry ;
45+
4146 @ Value (value = "${http.timeout:5}" )
4247 private long timeout ;
4348
@@ -47,6 +52,7 @@ public class OrdersService {
4752 public CustomerOrder createNewOrder (NewOrderResource orderPayload ) {
4853 LOG .info ("Creating order {}" , orderPayload );
4954 LOG .debug ("Starting calls" );
55+ meterRegistry .counter ("orders.placed" ).increment ();
5056 try {
5157 Future <Address > addressFuture = asyncGetService .getObject (orderPayload .address ,
5258 new ParameterizedTypeReference <Address >() {
@@ -82,9 +88,11 @@ public CustomerOrder createNewOrder(NewOrderResource orderPayload) {
8288
8389 LOG .info ("Received payment response: " + paymentResponse );
8490 if (paymentResponse == null ) {
91+ meterRegistry .counter ("orders.rejected" ,"cause" ,"payment_response_invalid" ).increment ();
8592 throw new PaymentDeclinedException ("Unable to parse authorisation packet" );
8693 }
8794 if (!paymentResponse .isAuthorised ()) {
95+ meterRegistry .counter ("orders.rejected" ,"cause" ,"payment_declined" ).increment ();
8896 throw new PaymentDeclinedException (paymentResponse .getMessage ());
8997 }
9098
@@ -102,12 +110,28 @@ public CustomerOrder createNewOrder(NewOrderResource orderPayload) {
102110
103111 CustomerOrder savedOrder = customerOrderRepository .save (order );
104112 LOG .debug ("Saved order: " + savedOrder );
113+ meterRegistry .summary ("orders.amount" ).record (amount );
114+ DistributionSummary .builder ("order.stats" )
115+ .serviceLevelObjectives (10d ,20d ,30d ,40d ,50d ,60d ,70d ,80d ,90d ,100d ,110d )
116+ //.publishPercentileHistogram()
117+ .maximumExpectedValue (120d )
118+ .minimumExpectedValue (5d )
119+ .register (meterRegistry )
120+ .record (amount );
105121 OrderUpdate update = new OrderUpdate (savedOrder .getId (), null );
106122 messagingService .dispatchToFulfillment (update );
107123 return savedOrder ;
108124 } catch (TimeoutException e ) {
125+ meterRegistry .counter ("orders.rejected" ,"cause" ,"timeout" ).increment ();
109126 throw new OrderFailedException ("Unable to create order due to timeout from one of the services." , e );
110- } catch (InterruptedException | IOException | ExecutionException e ) {
127+ } catch (InterruptedException e ) {
128+ meterRegistry .counter ("orders.rejected" ,"cause" ,"interrupted" ).increment ();
129+ throw new OrderFailedException ("Unable to create order due to unspecified IO error." , e );
130+ }catch ( IOException e ) {
131+ meterRegistry .counter ("orders.rejected" ,"cause" ,"connectivity" ).increment ();
132+ throw new OrderFailedException ("Unable to create order due to unspecified IO error." , e );
133+ }catch (ExecutionException e ) {
134+ meterRegistry .counter ("orders.rejected" ,"cause" ,"task_failed" ).increment ();
111135 throw new OrderFailedException ("Unable to create order due to unspecified IO error." , e );
112136 }
113137
0 commit comments