Skip to content

Commit

Permalink
feat: propagate cartservice exceptions (open-telemetry#1744)
Browse files Browse the repository at this point in the history
* feat: propagate GetCart errors

* feat: propagate AddItem errors

---------

Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
  • Loading branch information
rogercoll and julianocosta89 authored Oct 22, 2024
1 parent 4ee9f0c commit c79152c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ the release.
([#1733](https://github.com/open-telemetry/opentelemetry-demo/pull/1733))
* [chore] Move opensearch image version to .env file
([#1743](https://github.com/open-telemetry/opentelemetry-demo/pull/1743))
* [cartservice] Propagate cartservice exceptions
([#1744](https://github.com/open-telemetry/opentelemetry-demo/pull/1744))
* [cartservice] Update cart service to fail when cartServiceFailure is enabled
([#1748](https://github.com/open-telemetry/opentelemetry-demo/pull/1748))

Expand Down
37 changes: 28 additions & 9 deletions src/cartservice/src/services/CartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ public override async Task<Empty> AddItem(AddItemRequest request, ServerCallCont
activity?.SetTag("app.product.id", request.Item.ProductId);
activity?.SetTag("app.product.quantity", request.Item.Quantity);

await _cartStore.AddItemAsync(request.UserId, request.Item.ProductId, request.Item.Quantity);
return Empty;
try
{
await _cartStore.AddItemAsync(request.UserId, request.Item.ProductId, request.Item.Quantity);

return Empty;
}
catch (RpcException ex)
{
activity?.RecordException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
}

public override async Task<Cart> GetCart(GetCartRequest request, ServerCallContext context)
Expand All @@ -43,15 +53,24 @@ public override async Task<Cart> GetCart(GetCartRequest request, ServerCallConte
activity?.SetTag("app.user.id", request.UserId);
activity?.AddEvent(new("Fetch cart"));

var cart = await _cartStore.GetCartAsync(request.UserId);
var totalCart = 0;
foreach (var item in cart.Items)
try
{
totalCart += item.Quantity;
}
activity?.SetTag("app.cart.items.count", totalCart);
var cart = await _cartStore.GetCartAsync(request.UserId);
var totalCart = 0;
foreach (var item in cart.Items)
{
totalCart += item.Quantity;
}
activity?.SetTag("app.cart.items.count", totalCart);

return cart;
return cart;
}
catch (RpcException ex)
{
activity?.RecordException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
}

public override async Task<Empty> EmptyCart(EmptyCartRequest request, ServerCallContext context)
Expand Down

0 comments on commit c79152c

Please sign in to comment.