From cd9eee3f201647f95b1e9e35d269d7f64a5b40e7 Mon Sep 17 00:00:00 2001 From: Muhamad Azamy Date: Mon, 18 Mar 2024 08:53:33 +0100 Subject: [PATCH] improve on example logging --- example/checkout.go | 22 +++++++++++++++------- example/main.go | 3 ++- example/user_session.go | 9 ++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/example/checkout.go b/example/checkout.go index 4caed62..2d464e4 100644 --- a/example/checkout.go +++ b/example/checkout.go @@ -9,26 +9,34 @@ import ( ) type PaymentRequest struct { - UserID string - Tickets []string + UserID string `json:"userId"` + Tickets []string `json:"tickets"` } -func payment(ctx restate.Context, request PaymentRequest) (bool, error) { +type PaymentResponse struct { + ID string `json:"id"` + Price int `json:"price"` +} + +func payment(ctx restate.Context, request PaymentRequest) (response PaymentResponse, err error) { uuid, err := restate.SideEffectAs(ctx, func() (string, error) { uuid := uuid.New() return uuid.String(), nil }) + response.ID = uuid + if err != nil { - return false, err + return response, err } // We are a uniform shop where everything costs 30 USD // that is cheaper than the official example :P price := len(request.Tickets) * 30 + response.Price = price i := 0 - success, err := restate.SideEffectAs(ctx, func() (bool, error) { + _, err = restate.SideEffectAs(ctx, func() (bool, error) { log := log.With().Str("uuid", uuid).Int("price", price).Logger() if i > 2 { log.Info().Msg("payment succeeded") @@ -41,12 +49,12 @@ func payment(ctx restate.Context, request PaymentRequest) (bool, error) { }) if err != nil { - return false, err + return response, err } // todo: send email - return success, nil + return response, nil } var ( diff --git a/example/main.go b/example/main.go index b1753f0..47be6c4 100644 --- a/example/main.go +++ b/example/main.go @@ -17,7 +17,8 @@ const ( func main() { - zerolog.SetGlobalLevel(zerolog.DebugLevel) + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + zerolog.SetGlobalLevel(zerolog.InfoLevel) server := server.NewRestate(). Bind(UserSessionServiceName, UserSession). diff --git a/example/user_session.go b/example/user_session.go index b0bad8d..4d9e035 100644 --- a/example/user_session.go +++ b/example/user_session.go @@ -6,6 +6,7 @@ import ( "time" "github.com/muhamadazmy/restate-sdk-go" + "github.com/rs/zerolog/log" ) func addTicket(ctx restate.Context, userId, ticketId string) (bool, error) { @@ -73,17 +74,15 @@ func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error) if len(tickets) == 0 { return false, nil } - var success bool + var response PaymentResponse if err := ctx.Service(CheckoutServiceName). Method("checkout"). - Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &success); err != nil { + Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &response); err != nil { return false, err } - if !success { - return false, nil - } + log.Info().Str("id", response.ID).Int("price", response.Price).Msg("payment details") call := ctx.Service(TicketServiceName).Method("markAsSold") for _, ticket := range tickets {