Skip to content

Commit

Permalink
feat: remove global tracer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 4963aff commit f12ab5d
Show file tree
Hide file tree
Showing 29 changed files with 362 additions and 159 deletions.
8 changes: 4 additions & 4 deletions internal/api/common/middleware_resolver.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package common

import (
"go.opentelemetry.io/otel/trace"
"net/http"
"strings"

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

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/ledger/internal/controller/system"

"errors"
)
Expand All @@ -20,6 +19,7 @@ const (
func LedgerMiddleware(
backend system.Controller,
resolver func(*http.Request) string,
tracer trace.Tracer,
excludePathFromSchemaCheck ...string,
) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
Expand All @@ -30,7 +30,7 @@ func LedgerMiddleware(
return
}

ctx, span := tracing.Start(r.Context(), "OpenLedger")
ctx, span := tracer.Start(r.Context(), "OpenLedger")
defer span.End()

var err error
Expand Down
3 changes: 2 additions & 1 deletion internal/api/common/middleware_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/json"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestResolverMiddleware(t *testing.T) {

m := LedgerMiddleware(systemController, func(*http.Request) string {
return ledger
})
}, nooptracer.Tracer{})
h := m(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
Expand Down
3 changes: 3 additions & 0 deletions internal/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/ledger/internal/controller/system"
"github.com/go-chi/chi/v5"
"go.opentelemetry.io/otel/trace"

"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/health"
Expand All @@ -23,6 +24,7 @@ func Module(cfg Config) fx.Option {
healthController *health.HealthController,
authenticator auth.Authenticator,
logger logging.Logger,
tracer trace.TracerProvider,
) chi.Router {
return NewRouter(
backend,
Expand All @@ -31,6 +33,7 @@ func Module(cfg Config) fx.Option {
logger,
"develop",
cfg.Debug,
WithTracer(tracer.Tracer("api")),
)
}),
health.Module(),
Expand Down
41 changes: 39 additions & 2 deletions internal/api/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"go.opentelemetry.io/otel/trace"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"net/http"

"github.com/formancehq/ledger/internal/controller/system"
Expand All @@ -25,7 +27,14 @@ func NewRouter(
logger logging.Logger,
version string,
debug bool,
opts ...RouterOption,
) chi.Router {

routerOptions := routerOptions{}
for _, opt := range append(defaultRouterOptions, opts...) {
opt(&routerOptions)
}

mux := chi.NewRouter()
mux.Use(
middleware.Recoverer,
Expand All @@ -47,12 +56,40 @@ func NewRouter(
)
mux.Get("/_healthcheck", healthController.Check)

v2Router := v2.NewRouter(systemController, authenticator, version, debug)
v2Router := v2.NewRouter(
systemController,
authenticator,
version,
debug,
v2.WithTracer(routerOptions.tracer),
)
mux.Handle("/v2*", http.StripPrefix("/v2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
chi.RouteContext(r.Context()).Reset()
v2Router.ServeHTTP(w, r)
})))
mux.Handle("/*", v1.NewRouter(systemController, authenticator, version, debug))
mux.Handle("/*", v1.NewRouter(
systemController,
authenticator,
version,
debug,
v1.WithTracer(routerOptions.tracer),
))

return mux
}

type routerOptions struct {
tracer trace.Tracer
}

type RouterOption func(ro *routerOptions)

func WithTracer(tracer trace.Tracer) RouterOption {
return func(ro *routerOptions) {
ro.tracer = tracer
}
}

var defaultRouterOptions = []RouterOption{
WithTracer(nooptracer.Tracer{}),
}
27 changes: 26 additions & 1 deletion internal/api/v1/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"go.opentelemetry.io/otel/trace"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"net/http"

"github.com/formancehq/ledger/internal/controller/system"
Expand All @@ -20,7 +22,14 @@ func NewRouter(
authenticator auth.Authenticator,
version string,
debug bool,
opts ...RouterOption,
) chi.Router {

routerOptions := &routerOptions{}
for _, opt := range append(defaultRouterOptions, opts...) {
opt(routerOptions)
}

router := chi.NewMux()

router.Get("/_info", getInfo(systemController, version))
Expand All @@ -39,7 +48,7 @@ func NewRouter(
router.Use(autoCreateMiddleware(systemController))
router.Use(common.LedgerMiddleware(systemController, func(r *http.Request) string {
return chi.URLParam(r, "ledger")
}, "/_info"))
}, routerOptions.tracer, "/_info"))

// LedgerController
router.Get("/_info", getLedgerInfo)
Expand Down Expand Up @@ -74,3 +83,19 @@ func NewRouter(

return router
}

type routerOptions struct {
tracer trace.Tracer
}

type RouterOption func(ro *routerOptions)

func WithTracer(tracer trace.Tracer) RouterOption {
return func(ro *routerOptions) {
ro.tracer = tracer
}
}

var defaultRouterOptions = []RouterOption{
WithTracer(nooptracer.Tracer{}),
}
16 changes: 8 additions & 8 deletions internal/api/v2/controllers_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"net/http"

"errors"
"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/metadata"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/internal/tracing"

"github.com/formancehq/go-libs/api"
"github.com/formancehq/ledger/internal/api/common"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
)

func bulkHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -60,10 +58,12 @@ type Result struct {
ResponseType string `json:"responseType"` // Added for sdk generation (discriminator in oneOf)
}

func ProcessBulk(ctx context.Context, l ledgercontroller.Controller, bulk Bulk, continueOnFailure bool) ([]Result, bool, error) {

ctx, span := tracing.Start(ctx, "Bulk")
defer span.End()
func ProcessBulk(
ctx context.Context,
l ledgercontroller.Controller,
bulk Bulk,
continueOnFailure bool,
) ([]Result, bool, error) {

ret := make([]Result, 0, len(bulk))

Expand Down
25 changes: 24 additions & 1 deletion internal/api/v2/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2

import (
nooptracer "go.opentelemetry.io/otel/trace/noop"
"net/http"

"github.com/formancehq/ledger/internal/controller/system"
Expand All @@ -23,7 +24,13 @@ func NewRouter(
authenticator auth.Authenticator,
version string,
debug bool,
opts ...RouterOption,
) chi.Router {
routerOptions := routerOptions{}
for _, opt := range append(defaultRouterOptions, opts...) {
opt(&routerOptions)
}

router := chi.NewMux()

router.Get("/_info", getInfo(version))
Expand All @@ -50,7 +57,7 @@ func NewRouter(

router.With(common.LedgerMiddleware(systemController, func(r *http.Request) string {
return chi.URLParam(r, "ledger")
}, "/_info")).Group(func(router chi.Router) {
}, routerOptions.tracer, "/_info")).Group(func(router chi.Router) {
router.Post("/_bulk", bulkHandler)

// LedgerController
Expand Down Expand Up @@ -87,3 +94,19 @@ func NewRouter(

return router
}

type routerOptions struct {
tracer trace.Tracer
}

type RouterOption func(ro *routerOptions)

func WithTracer(tracer trace.Tracer) RouterOption {
return func(ro *routerOptions) {
ro.tracer = tracer
}
}

var defaultRouterOptions = []RouterOption{
WithTracer(nooptracer.Tracer{}),
}
47 changes: 31 additions & 16 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"database/sql"
"fmt"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
noopmetrics "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"math/big"
"reflect"

Expand All @@ -27,23 +29,12 @@ 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

tracer trace.Tracer
meter metric.Meter
executeMachineHistogram metric.Int64Histogram
}
Expand Down Expand Up @@ -246,9 +237,15 @@ 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.TraceWithMetric(ctx, "ExecuteMachine", ctrl.executeMachineHistogram, func(ctx context.Context) (*MachineResult, error) {
return m.Execute(ctx, sqlTX, input.Vars)
})
result, err := tracing.TraceWithMetric(
ctx,
"ExecuteMachine",
ctrl.tracer,
ctrl.executeMachineHistogram,
func(ctx context.Context) (*MachineResult, error) {
return m.Execute(ctx, sqlTX, input.Vars)
},
)
if err != nil {
return nil, fmt.Errorf("failed to execute program: %w", err)
}
Expand Down Expand Up @@ -433,3 +430,21 @@ func (ctrl *DefaultController) DeleteAccountMetadata(ctx context.Context, parame
}

var _ Controller = (*DefaultController)(nil)

type DefaultControllerOption func(controller *DefaultController)

var defaultOptions = []DefaultControllerOption{
WithMeter(noopmetrics.Meter{}),
WithTracer(nooptracer.Tracer{}),
}

func WithMeter(meter metric.Meter) DefaultControllerOption {
return func(controller *DefaultController) {
controller.meter = meter
}
}
func WithTracer(tracer trace.Tracer) DefaultControllerOption {
return func(controller *DefaultController) {
controller.tracer = tracer
}
}
Loading

0 comments on commit f12ab5d

Please sign in to comment.