Skip to content

Commit

Permalink
feat(payments): add api traces (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Feb 26, 2024
1 parent f789a2a commit 79bfad6
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 36 deletions.
19 changes: 17 additions & 2 deletions components/payments/cmd/api/internal/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/backend"
"github.com/formancehq/payments/cmd/api/internal/storage"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/otel"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
"github.com/formancehq/stack/libs/go-libs/pointer"
"github.com/google/uuid"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel/attribute"
)

type accountResponse struct {
Expand All @@ -32,6 +34,9 @@ type accountResponse struct {

func listAccountsHandler(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "listAccountsHandler")
defer span.End()

w.Header().Set("Content-Type", "application/json")

query, err := bunpaginate.Extract[storage.ListAccountsQuery](r, func() (*storage.ListAccountsQuery, error) {
Expand All @@ -42,12 +47,14 @@ func listAccountsHandler(b backend.Backend) http.HandlerFunc {
return pointer.For(storage.NewListAccountsQuery(*options)), nil
})
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

cursor, err := b.GetService().ListAccounts(r.Context(), *query)
cursor, err := b.GetService().ListAccounts(ctx, *query)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
Expand Down Expand Up @@ -101,6 +108,7 @@ func listAccountsHandler(b backend.Backend) http.HandlerFunc {
},
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand All @@ -109,12 +117,18 @@ func listAccountsHandler(b backend.Backend) http.HandlerFunc {

func readAccountHandler(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "readAccountHandler")
defer span.End()

w.Header().Set("Content-Type", "application/json")

accountID := mux.Vars(r)["accountID"]

account, err := b.GetService().GetAccount(r.Context(), accountID)
span.SetAttributes(attribute.String("request.accountID", accountID))

account, err := b.GetService().GetAccount(ctx, accountID)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
Expand Down Expand Up @@ -157,6 +171,7 @@ func readAccountHandler(b backend.Backend) http.HandlerFunc {
Data: data,
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand Down
19 changes: 18 additions & 1 deletion components/payments/cmd/api/internal/api/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/backend"
"github.com/formancehq/payments/cmd/api/internal/storage"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/otel"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
"github.com/formancehq/stack/libs/go-libs/pointer"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel/attribute"
)

type balancesResponse struct {
Expand All @@ -27,14 +29,25 @@ type balancesResponse struct {

func listBalancesForAccount(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "listBalancesForAccount")
defer span.End()

w.Header().Set("Content-Type", "application/json")

balanceQuery, err := populateBalanceQueryFromRequest(r)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

span.SetAttributes(
attribute.String("request.accountID", balanceQuery.AccountID.String()),
attribute.String("request.currency", balanceQuery.Currency),
attribute.String("request.from", balanceQuery.From.String()),
attribute.String("request.to", balanceQuery.To.String()),
)

query, err := bunpaginate.Extract[storage.ListBalancesQuery](r, func() (*storage.ListBalancesQuery, error) {
options, err := getPagination(r, balanceQuery)
if err != nil {
Expand All @@ -43,6 +56,7 @@ func listBalancesForAccount(b backend.Backend) http.HandlerFunc {
return pointer.For(storage.NewListBalancesQuery(*options)), nil
})
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}
Expand All @@ -52,6 +66,7 @@ func listBalancesForAccount(b backend.Backend) http.HandlerFunc {
if r.URL.Query().Get("limit") != "" {
limit, err := strconv.ParseInt(r.URL.Query().Get("limit"), 10, 64)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}
Expand All @@ -62,8 +77,9 @@ func listBalancesForAccount(b backend.Backend) http.HandlerFunc {
}
}

cursor, err := b.GetService().ListBalances(r.Context(), *query)
cursor, err := b.GetService().ListBalances(ctx, *query)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
Expand Down Expand Up @@ -92,6 +108,7 @@ func listBalancesForAccount(b backend.Backend) http.HandlerFunc {
},
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand Down
3 changes: 1 addition & 2 deletions components/payments/cmd/api/internal/api/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/service"
"github.com/formancehq/payments/cmd/api/internal/storage"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/stack/libs/go-libs/api"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/auth"
"github.com/formancehq/stack/libs/go-libs/logging"
Expand Down Expand Up @@ -312,7 +311,7 @@ func TestGetBalances(t *testing.T) {
},
}

listBalancesResponse := &api.Cursor[models.Balance]{
listBalancesResponse := &sharedapi.Cursor[models.Balance]{
PageSize: testCase.pageSize,
HasMore: false,
Previous: "",
Expand Down
21 changes: 19 additions & 2 deletions components/payments/cmd/api/internal/api/bank_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

"github.com/formancehq/payments/cmd/api/internal/api/backend"
"github.com/formancehq/payments/cmd/api/internal/storage"
"github.com/formancehq/payments/internal/otel"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
"github.com/formancehq/stack/libs/go-libs/pointer"
"github.com/google/uuid"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel/attribute"
)

type bankAccountRelatedAccountsResponse struct {
Expand Down Expand Up @@ -42,6 +44,9 @@ type bankAccountResponse struct {

func listBankAccountsHandler(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "listBankAccountsHandler")
defer span.End()

w.Header().Set("Content-Type", "application/json")

query, err := bunpaginate.Extract[storage.ListBankAccountQuery](r, func() (*storage.ListBankAccountQuery, error) {
Expand All @@ -52,12 +57,14 @@ func listBankAccountsHandler(b backend.Backend) http.HandlerFunc {
return pointer.For(storage.NewListBankAccountQuery(*options)), nil
})
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

cursor, err := b.GetService().ListBankAccounts(r.Context(), *query)
cursor, err := b.GetService().ListBankAccounts(ctx, *query)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
Expand Down Expand Up @@ -102,6 +109,7 @@ func listBankAccountsHandler(b backend.Backend) http.HandlerFunc {
},
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand All @@ -110,21 +118,29 @@ func listBankAccountsHandler(b backend.Backend) http.HandlerFunc {

func readBankAccountHandler(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "readBankAccountHandler")
defer span.End()

w.Header().Set("Content-Type", "application/json")

bankAccountID, err := uuid.Parse(mux.Vars(r)["bankAccountID"])
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrInvalidID, err)
return
}

account, err := b.GetService().GetBankAccount(r.Context(), bankAccountID, true)
span.SetAttributes(attribute.String("request.bankAccountID", bankAccountID.String()))

account, err := b.GetService().GetBankAccount(ctx, bankAccountID, true)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}

if err := account.Offuscate(); err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand Down Expand Up @@ -161,6 +177,7 @@ func readBankAccountHandler(b backend.Backend) http.HandlerFunc {
Data: data,
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/service"
"github.com/formancehq/payments/cmd/api/internal/storage"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/stack/libs/go-libs/api"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/auth"
"github.com/formancehq/stack/libs/go-libs/logging"
Expand Down Expand Up @@ -215,7 +214,7 @@ func TestListBankAccounts(t *testing.T) {
},
},
}
listBankAccountsResponse := &api.Cursor[models.BankAccount]{
listBankAccountsResponse := &sharedapi.Cursor[models.BankAccount]{
PageSize: testCase.pageSize,
HasMore: false,
Previous: "",
Expand Down
20 changes: 18 additions & 2 deletions components/payments/cmd/api/internal/api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,50 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/backend"
"github.com/formancehq/payments/cmd/api/internal/api/service"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/otel"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"

"github.com/gorilla/mux"
)

func updateMetadataHandler(b backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "updateMetadataHandler")
defer span.End()

paymentID, err := models.PaymentIDFromString(mux.Vars(r)["paymentID"])
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrInvalidID, err)
return
}

span.SetAttributes(attribute.String("request.paymentID", paymentID.String()))

var metadata service.UpdateMetadataRequest
if r.ContentLength == 0 {
api.BadRequest(w, ErrMissingOrInvalidBody, errors.New("body is required"))
var err = errors.New("body is required")
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

err = json.NewDecoder(r.Body).Decode(&metadata)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

err = b.GetService().UpdatePaymentMetadata(r.Context(), *paymentID, metadata)
for k, v := range metadata {
span.SetAttributes(attribute.String("request.metadata."+k, v))
}

err = b.GetService().UpdatePaymentMetadata(ctx, *paymentID, metadata)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
Expand Down
Loading

0 comments on commit 79bfad6

Please sign in to comment.