Skip to content

Commit

Permalink
feat: add some metrics and export them add api level
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent b285a56 commit 4963aff
Show file tree
Hide file tree
Showing 28 changed files with 886 additions and 634 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ go.work*
*.jar
node_modules
dist
vendor
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ deploy-staging:
BUILD --pass-args core+deploy-staging

lint:
#todo: check config from core
#todo: get config from core
FROM +tidy
RUN golangci-lint run --fix --build-tags it --timeout 5m
SAVE ARTIFACT cmd AS LOCAL cmd
Expand Down
22 changes: 21 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"github.com/formancehq/go-libs/httpserver"
"github.com/formancehq/go-libs/pprof"
"github.com/formancehq/ledger/internal/storage/driver"
"github.com/go-chi/chi/v5"
"go.opentelemetry.io/otel/sdk/metric"
"time"

"github.com/formancehq/go-libs/auth"
Expand Down Expand Up @@ -57,6 +60,8 @@ func NewServeCommand() *cobra.Command {
options = append(options, pprof.NewFXModule())
}

otelMetricsExporter, _ := cmd.Flags().GetString(otlpmetrics.OtelMetricsExporterFlag)

options = append(options,
publish.FXModuleFromFlags(cmd, service.IsDebug(cmd)),
otlpmetrics.FXModuleFromFlags(cmd),
Expand All @@ -77,9 +82,24 @@ func NewServeCommand() *cobra.Command {
api.Module(api.Config{
Version: Version,
Debug: service.IsDebug(cmd),
Bind: serveConfiguration.bind,
}),
fx.Invoke(func(lc fx.Lifecycle, h chi.Router) {
lc.Append(httpserver.NewHook(h, httpserver.WithAddress(serveConfiguration.bind)))
}),
)
if otelMetricsExporter == "memory" {
options = append(options, fx.Decorate(func(
h chi.Router,
meterProvider *metric.MeterProvider,
exporter *otlpmetrics.InMemoryExporter,
) chi.Router {
wrappedRouter := chi.NewRouter()
wrappedRouter.Handle("/_metrics", otlpmetrics.NewInMemoryExporterHandler(meterProvider, exporter))
wrappedRouter.Mount("/", h)

return wrappedRouter
}))
}

return service.New(cmd.OutOrStdout(), options...).Run(cmd)
},
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ services:
volumes:
- .:/src
ports:
# HTTP API
- 3068:3068
# pprof
- 3000:3000
working_dir: /src
depends_on:
Expand All @@ -37,3 +39,5 @@ services:
DEBUG: "true"
ENABLE_PPROF: "true"
GOWORK: off
OTEL_METRICS: "true"
OTEL_METRICS_EXPORTER: "memory"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/alitto/pond v1.9.2
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
github.com/bluele/gcache v0.0.2
github.com/formancehq/go-libs v1.7.2-0.20241011081325-92040b4fad01
github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c
github.com/formancehq/stack/ledger/client v0.0.0-00010101000000-000000000000
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ github.com/formancehq/go-libs v1.7.2-0.20241001151743-2bd31525c7d1 h1:/Zo3e6njdS
github.com/formancehq/go-libs v1.7.2-0.20241001151743-2bd31525c7d1/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/formancehq/go-libs v1.7.2-0.20241011081325-92040b4fad01 h1:fRokKJe35GLMfva8ehQbLra3oQdUHdfi2mG72BaQlEk=
github.com/formancehq/go-libs v1.7.2-0.20241011081325-92040b4fad01/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4=
github.com/formancehq/go-libs v1.7.2-0.20241011190222-bbd435d8b38f h1:kyG3XJK0S/U5O1h3dYmQFcyFYiir95m7KeeEu/751Ng=
github.com/formancehq/go-libs v1.7.2-0.20241011190222-bbd435d8b38f/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4=
github.com/formancehq/go-libs v1.7.2-0.20241011191048-664ae117c799 h1:b1MrUxQ8tcjzS6PXdtLJr7yCxO7QBR4D9w+P5JajwQo=
github.com/formancehq/go-libs v1.7.2-0.20241011191048-664ae117c799/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4=
github.com/formancehq/go-libs v1.7.2-0.20241011191318-a18ddf18e4f9 h1:71fY4Pc9ZgS0bcKwIvg+4hJHKXGG7SnI3GPtlsaBlDU=
github.com/formancehq/go-libs v1.7.2-0.20241011191318-a18ddf18e4f9/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4=
github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c h1:HE8yjpXyLkXJKDC7ig+INzdWvkT0SiuINt+gqC0/cBU=
github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
Expand Down
18 changes: 0 additions & 18 deletions internal/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package api

import (
_ "embed"
"net/http"

"github.com/formancehq/go-libs/httpserver"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/ledger/internal/controller/system"

"github.com/go-chi/chi/v5"

"github.com/formancehq/go-libs/auth"
Expand All @@ -18,7 +14,6 @@ import (
type Config struct {
Version string
Debug bool
Bind string
}

func Module(cfg Config) fx.Option {
Expand All @@ -39,18 +34,5 @@ func Module(cfg Config) fx.Option {
)
}),
health.Module(),
fx.Invoke(func(lc fx.Lifecycle, h chi.Router, logger logging.Logger) {

wrappedRouter := chi.NewRouter()
wrappedRouter.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(logging.ContextWithLogger(r.Context(), logger))
handler.ServeHTTP(w, r)
})
})
wrappedRouter.Mount("/", h)

lc.Append(httpserver.NewHook(wrappedRouter, httpserver.WithAddress(cfg.Bind)))
}),
)
}
31 changes: 30 additions & 1 deletion internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"fmt"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"math/big"
"reflect"

Expand All @@ -25,23 +27,50 @@ import (
ledger "github.com/formancehq/ledger/internal"
)

type DefaultControllerOption func(controller *DefaultController)

var defaultOptions []DefaultControllerOption = []DefaultControllerOption{
WithMeter(noop.Meter{}),
}

func WithMeter(meter metric.Meter) DefaultControllerOption {
return func(controller *DefaultController) {
controller.meter = meter
}
}

type DefaultController struct {
store Store
machineFactory MachineFactory
ledger ledger.Ledger

meter metric.Meter
executeMachineHistogram metric.Int64Histogram
}

func NewDefaultController(
ledger ledger.Ledger,
store Store,
machineFactory MachineFactory,
opts ...DefaultControllerOption,
) *DefaultController {
ret := &DefaultController{
store: store,
ledger: ledger,
machineFactory: machineFactory,
}

for _, opt := range append(defaultOptions, opts...) {
opt(ret)
}

histogram, err := ret.meter.Int64Histogram("numscript.run")
if err != nil {
return nil
}

ret.executeMachineHistogram = histogram

return ret
}

Expand Down Expand Up @@ -217,7 +246,7 @@ func (ctrl *DefaultController) CreateTransaction(ctx context.Context, parameters
}

output, err := forgeLog(ctx, ctrl.store, parameters, func(ctx context.Context, sqlTX TX, input RunScript) (*ledger.CreatedTransaction, error) {
result, err := tracing.TraceWithLatency(ctx, "ExecuteMachine", func(ctx context.Context) (*MachineResult, error) {
result, err := tracing.TraceWithMetric(ctx, "ExecuteMachine", ctrl.executeMachineHistogram, func(ctx context.Context) (*MachineResult, error) {
return m.Execute(ctx, sqlTX, input.Vars)
})
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions internal/controller/ledger/log_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/go-libs/pointer"
ledger "github.com/formancehq/ledger/internal"
"github.com/formancehq/ledger/internal/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -24,9 +23,7 @@ func runTx[INPUT any, OUTPUT ledger.LogPayload](ctx context.Context, store Store
log.IdempotencyKey = parameters.IdempotencyKey
log.IdempotencyHash = ledger.ComputeIdempotencyHash(parameters.Input)

_, err = tracing.TraceWithLatency(ctx, "InsertLog", func(ctx context.Context) (*struct{}, error) {
return nil, tx.InsertLog(ctx, &log)
})
err = tx.InsertLog(ctx, &log)
if err != nil {
return false, fmt.Errorf("failed to insert log: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/controller/system/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package system

import (
"context"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"time"

"github.com/formancehq/ledger/internal/tracing"
Expand Down Expand Up @@ -30,6 +32,8 @@ type DefaultController struct {
compiler ledgercontroller.Compiler
registry *ledgercontroller.StateRegistry
databaseRetryConfiguration DatabaseRetryConfiguration

meter metric.Meter
}

func (ctrl *DefaultController) GetLedgerController(ctx context.Context, name string) (ledgercontroller.Controller, error) {
Expand All @@ -43,6 +47,7 @@ func (ctrl *DefaultController) GetLedgerController(ctx context.Context, name str
*l,
store,
ledgercontroller.NewDefaultMachineFactory(ctrl.compiler),
ledgercontroller.WithMeter(ctrl.meter),
)

// Add too many client error handling
Expand Down Expand Up @@ -131,6 +136,13 @@ func WithDatabaseRetryConfiguration(configuration DatabaseRetryConfiguration) Op
}
}

func WithMeter(m metric.Meter) Option {
return func(ctrl *DefaultController) {
ctrl.meter = m
}
}

var defaultOptions = []Option{
WithCompiler(ledgercontroller.NewDefaultCompiler()),
WithMeter(noop.Meter{}),
}
12 changes: 10 additions & 2 deletions internal/controller/system/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"time"
)
Expand All @@ -24,6 +25,7 @@ func NewFXModule(configuration ModuleConfiguration) fx.Option {
fx.Provide(func(
store Store,
listener ledgercontroller.Listener,
meterProvider metric.MeterProvider,
) *DefaultController {
options := make([]Option, 0)
if configuration.NSCacheConfiguration.MaxCount != 0 {
Expand All @@ -32,9 +34,15 @@ func NewFXModule(configuration ModuleConfiguration) fx.Option {
configuration.NSCacheConfiguration,
)))
}
options = append(options, WithDatabaseRetryConfiguration(configuration.DatabaseRetryConfiguration))

return NewDefaultController(store, listener, options...)
return NewDefaultController(
store,
listener,
append(options,
WithDatabaseRetryConfiguration(configuration.DatabaseRetryConfiguration),
WithMeter(meterProvider.Meter("core")),
)...,
)
}),
)
}
27 changes: 23 additions & 4 deletions internal/storage/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
. "github.com/formancehq/go-libs/collectionutils"
"github.com/formancehq/go-libs/metadata"
"github.com/formancehq/go-libs/platform/postgres"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"

systemcontroller "github.com/formancehq/ledger/internal/controller/system"

Expand All @@ -27,6 +29,7 @@ const (

type Driver struct {
db *bun.DB
meter metric.Meter
}

func (d *Driver) createLedgerStore(ctx context.Context, db bun.IDB, ledger ledger.Ledger) (*ledgerstore.Store, error) {
Expand All @@ -49,7 +52,7 @@ func (d *Driver) createLedgerStore(ctx context.Context, db bun.IDB, ledger ledge
return nil, fmt.Errorf("committing sql transaction to create ledger and schemas: %w", err)
}

return ledgerstore.New(d.db, ledger), nil
return ledgerstore.New(d.db, ledger, ledgerstore.WithMeter(d.meter)), nil
}

func (d *Driver) CreateLedger(ctx context.Context, l *ledger.Ledger) (*ledgerstore.Store, error) {
Expand Down Expand Up @@ -106,7 +109,7 @@ func (d *Driver) OpenLedger(ctx context.Context, name string) (*ledgerstore.Stor
return nil, nil, postgres.ResolveError(err)
}

return ledgerstore.New(d.db, *ret), ret, nil
return ledgerstore.New(d.db, *ret, ledgerstore.WithMeter(d.meter)), ret, nil
}

func (d *Driver) Initialize(ctx context.Context) error {
Expand Down Expand Up @@ -215,8 +218,24 @@ func (d *Driver) UpgradeAllLedgers(ctx context.Context) error {
return nil
}

func New(db *bun.DB) *Driver {
return &Driver{
func New(db *bun.DB, opts ...Option) *Driver {
ret := &Driver{
db: db,
}
for _, opt := range append(defaultOptions, opts...) {
opt(ret)
}
return ret
}

type Option func(d *Driver)

func WithMeter(m metric.Meter) Option {
return func(d *Driver) {
d.meter = m
}
}

var defaultOptions = []Option {
WithMeter(noop.Meter{}),
}
5 changes: 3 additions & 2 deletions internal/storage/driver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"
"go.opentelemetry.io/otel/metric"

systemcontroller "github.com/formancehq/ledger/internal/controller/system"

Expand All @@ -20,8 +21,8 @@ type ModuleConfiguration struct {

func NewFXModule(autoUpgrade bool) fx.Option {
return fx.Options(
fx.Provide(func(db *bun.DB) (*Driver, error) {
return New(db), nil
fx.Provide(func(db *bun.DB, meterProvider metric.MeterProvider) (*Driver, error) {
return New(db, WithMeter(meterProvider.Meter("store"))), nil
}),
fx.Provide(fx.Annotate(NewControllerStorageDriverAdapter, fx.As(new(systemcontroller.Store)))),
fx.Invoke(func(driver *Driver, lifecycle fx.Lifecycle, logger logging.Logger) error {
Expand Down
Loading

0 comments on commit 4963aff

Please sign in to comment.