Skip to content

Commit

Permalink
feat(metrics): add operator metric attributes in generic connector calls
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji authored and paul-nicolas committed Dec 16, 2024
1 parent 430e33b commit 04f7430
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
6 changes: 2 additions & 4 deletions internal/connectors/plugins/public/generic/client/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
)

func (c *Client) ListAccounts(ctx context.Context, page, pageSize int64, createdAtFrom time.Time) ([]genericclient.Account, error) {
// TODO(f): Add metrics
// f := connectors.ClientMetrics(ctx, "generic", "list_accounts")
// now := time.Now()
// defer f(ctx, now)
start := time.Now()
defer c.recordMetrics(ctx, start, "list_accounts")

req := c.apiClient.DefaultApi.
GetAccounts(ctx).
Expand Down
7 changes: 3 additions & 4 deletions internal/connectors/plugins/public/generic/client/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package client

import (
"context"
"time"

"github.com/formancehq/payments/genericclient"
)

func (c *Client) GetBalances(ctx context.Context, accountID string) (*genericclient.Balances, error) {
// TODO(polo): add metrics
// f := connectors.ClientMetrics(ctx, "generic", "get_balance")
// now := time.Now()
// defer f(ctx, now)
start := time.Now()
defer c.recordMetrics(ctx, start, "list_balances")

req := c.apiClient.DefaultApi.GetAccountBalances(ctx, accountID)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
)

func (c *Client) ListBeneficiaries(ctx context.Context, page, pageSize int64, createdAtFrom time.Time) ([]genericclient.Beneficiary, error) {
// TODO(polo): add metrics
// f := connectors.ClientMetrics(ctx, "generic", "list_beneficiaries")
// now := time.Now()
// defer f(ctx, now)
start := time.Now()
defer c.recordMetrics(ctx, start, "list_beneficiaries")

req := c.apiClient.DefaultApi.
GetBeneficiaries(ctx).
Expand Down
35 changes: 33 additions & 2 deletions internal/connectors/plugins/public/generic/client/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package client

import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/formancehq/payments/genericclient"
"github.com/formancehq/payments/internal/connectors/metrics"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

type apiTransport struct {
Expand All @@ -20,7 +26,8 @@ func (t *apiTransport) RoundTrip(req *http.Request) (*http.Response, error) {
}

type Client struct {
apiClient *genericclient.APIClient
apiClient *genericclient.APIClient
commonMetricAttributes []attribute.KeyValue
}

func New(apiKey, baseURL string) *Client {
Expand All @@ -38,6 +45,30 @@ func New(apiKey, baseURL string) *Client {
genericClient := genericclient.NewAPIClient(configuration)

return &Client{
apiClient: genericClient,
apiClient: genericClient,
commonMetricAttributes: CommonMetricsAttributes(),
}
}

// recordMetrics is meant to be called in a defer
func (c *Client) recordMetrics(ctx context.Context, start time.Time, operation string) {
registry := metrics.GetMetricsRegistry()

attrs := c.commonMetricAttributes
attrs = append(attrs, attribute.String("operation", operation))
opts := metric.WithAttributes(attrs...)

registry.ConnectorPSPCalls().Add(ctx, 1, opts)
registry.ConnectorPSPCallLatencies().Record(ctx, time.Since(start).Milliseconds(), opts)
}

func CommonMetricsAttributes() []attribute.KeyValue {
metricsAttributes := []attribute.KeyValue{
attribute.String("connector", "generic"),
}
stack := os.Getenv("STACK")
if stack != "" {
metricsAttributes = append(metricsAttributes, attribute.String("stack", stack))
}
return metricsAttributes
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
)

func (c *Client) ListTransactions(ctx context.Context, page, pageSize int64, updatedAtFrom time.Time) ([]genericclient.Transaction, error) {
// TODO(polo): add metrics
// f := connectors.ClientMetrics(ctx, "generic", "list_transactions")
// now := time.Now()
// defer f(ctx, now)
start := time.Now()
defer c.recordMetrics(ctx, start, "list_transactions")

req := c.apiClient.DefaultApi.GetTransactions(ctx).
Page(page).
Expand Down

0 comments on commit 04f7430

Please sign in to comment.