Skip to content

Commit

Permalink
feat(payments): add otel traces everywhere (#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Jan 17, 2024
1 parent a4ceeb1 commit 8e439f1
Show file tree
Hide file tree
Showing 74 changed files with 3,180 additions and 1,951 deletions.
25 changes: 24 additions & 1 deletion components/payments/cmd/connectors/internal/api/bank_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (

"github.com/formancehq/payments/cmd/connectors/internal/api/backend"
"github.com/formancehq/payments/cmd/connectors/internal/api/service"
"github.com/formancehq/payments/internal/otel"
"github.com/formancehq/stack/libs/go-libs/api"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type bankAccountResponse struct {
Expand All @@ -27,26 +30,37 @@ func createBankAccountHandler(
b backend.ServiceBackend,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "createBankAccountHandler")
defer span.End()

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

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

setAttributesFromRequest(span, &bankAccountRequest)

if err := bankAccountRequest.Validate(); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

bankAccount, err := b.GetService().CreateBankAccount(r.Context(), &bankAccountRequest)
bankAccount, err := b.GetService().CreateBankAccount(ctx, &bankAccountRequest)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}

span.SetAttributes(attribute.String("bankAccount.id", bankAccount.ID.String()))
span.SetAttributes(attribute.String("bankAccount.createdAt", bankAccount.ID.String()))

data := &bankAccountResponse{
ID: bankAccount.ID.String(),
CreatedAt: bankAccount.CreatedAt,
Expand All @@ -61,8 +75,17 @@ func createBankAccountHandler(
Data: data,
})
if err != nil {
otel.RecordError(span, err)
api.InternalServerError(w, r, err)
return
}
}
}

func setAttributesFromRequest(span trace.Span, request *service.CreateBankAccountRequest) {
span.SetAttributes(
attribute.String("request.name", request.Name),
attribute.String("request.country", request.Country),
attribute.String("request.connectorID", request.ConnectorID),
)
}
Loading

1 comment on commit 8e439f1

@vercel
Copy link

@vercel vercel bot commented on 8e439f1 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.