Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use sharedlogging package from go-libs #166

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/numary/go-libs/sharedlogging"
"github.com/numary/go-libs/sharedlogging/sharedlogginglogrus"
"github.com/numary/ledger/pkg/api"
"github.com/numary/ledger/pkg/api/controllers"
"github.com/numary/ledger/pkg/api/middlewares"
"github.com/numary/ledger/pkg/api/routes"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/opentelemetry/opentelemetrymetrics"
"github.com/numary/ledger/pkg/opentelemetry/opentelemetrytraces"
"github.com/numary/ledger/pkg/storage"
"github.com/numary/ledger/pkg/storage/sqlstorage"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.opentelemetry.io/otel/metric"
Expand All @@ -32,7 +34,9 @@ func NewContainer(v *viper.Viper, options ...fx.Option) *fx.App {
options = append(options, fx.NopLogger)
}

options = append(options, logging.LogrusModule())
l := logrus.New()
loggerFactory := sharedlogging.StaticLoggerFactory(sharedlogginglogrus.New(l))
sharedlogging.SetFactory(loggerFactory)

// Handle OpenTelemetry
if v.GetBool(otelTracesFlag) {
Expand Down Expand Up @@ -142,7 +146,6 @@ func NewContainer(v *viper.Viper, options ...fx.Option) *fx.App {
options = append(options, routes.ProvideMiddlewares(func(params struct {
fx.In
TracerProvider trace.TracerProvider `optional:"true"`
Logger logging.Logger
}) []gin.HandlerFunc {
res := make([]gin.HandlerFunc, 0)

Expand All @@ -155,7 +158,7 @@ func NewContainer(v *viper.Viper, options ...fx.Option) *fx.App {
if viper.GetBool(otelTracesFlag) {
res = append(res, otelgin.Middleware("ledger", otelgin.WithTracerProvider(params.TracerProvider)))
}
res = append(res, middlewares.Log(params.Logger))
res = append(res, middlewares.Log())
var writer io.Writer = os.Stderr
if viper.GetBool(otelTracesFlag) {
writer = ioutil.Discard
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
NUMARY_OTEL_TRACES_EXPORTER_JAEGER_ENDPOINT: http://jaeger:14268/api/traces
NUMARY_OTEL_TRACES_EXPORTER_JAEGER_INSECURE: "true"
NUMARY_OTEL_METRICS: "false"
depends_on:
- jaeger

jaeger:
image: jaegertracing/all-in-one:1.31
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/numary/go-libs v0.0.0-20220224131631-02329283c5bd
github.com/numary/go-libs v0.0.0-20220301123239-18e848add472
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ github.com/numary/go-libs v0.0.0-20220224130919-9d6ff19c1eb6 h1:YSu6sZOVwTsX2wH2
github.com/numary/go-libs v0.0.0-20220224130919-9d6ff19c1eb6/go.mod h1:kp6/a0l1j6I/SXAVxKuQM8qNGNmKssXDsi84USBMkwM=
github.com/numary/go-libs v0.0.0-20220224131631-02329283c5bd h1:xIu4Wk6uNmu22TbgrTHkjiVbqVZdlic5LvGJjUYzXcE=
github.com/numary/go-libs v0.0.0-20220224131631-02329283c5bd/go.mod h1:kp6/a0l1j6I/SXAVxKuQM8qNGNmKssXDsi84USBMkwM=
github.com/numary/go-libs v0.0.0-20220301123239-18e848add472 h1:X2/xAxFKRMLNAsvPazgtO8aw7ka3bIAnJW1ah1lRt2w=
github.com/numary/go-libs v0.0.0-20220301123239-18e848add472/go.mod h1:PcMl7qlpi3hJDDgoamQm5KIVCIVCCv1onU3y2iDQh68=
github.com/numary/ledger v0.0.0-20210702172952-a5bd30e551d0/go.mod h1:u2K28z9TDYd6id1qeD2uv7JDlajuRZ0fvOnCeDZmDxk=
github.com/numary/ledger v0.0.0-20211227131550-dc7b78f85b5b/go.mod h1:uovuDsK7Gs7duqKQ9PgaFulJnPTDftGdR/n3rBRzNIs=
github.com/numary/machine v0.0.0-20210702091459-23a82555adbf/go.mod h1:WAFvefAGYNjdDmPtDoZ305F58QDtUJyB0QWN3vzSZao=
Expand Down
2 changes: 0 additions & 2 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/numary/ledger/pkg/core"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/ledgertesting"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/storage"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -34,7 +33,6 @@ func withNewModule(t *testing.T, options ...fx.Option) {
ledger.ResolveModule(),
storage.DefaultModule(),
ledgertesting.StorageModule(),
logging.LogrusModule(),
fx.NopLogger,
}, options...)
options = append(options, fx.Invoke(func() {
Expand Down
11 changes: 4 additions & 7 deletions pkg/api/controllers/script_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/numary/go-libs/sharedapi"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/go-libs/sharedlogging"
"net/http"
"strings"

Expand Down Expand Up @@ -35,14 +35,11 @@ func EncodeLink(errStr string) string {
// ScriptController -
type ScriptController struct {
BaseController
Logger logging.Logger
}

// NewScriptController -
func NewScriptController(logger logging.Logger) ScriptController {
return ScriptController{
Logger: logger,
}
func NewScriptController() ScriptController {
return ScriptController{}
}

func (ctl *ScriptController) PostScript(c *gin.Context) {
Expand Down Expand Up @@ -71,7 +68,7 @@ func (ctl *ScriptController) PostScript(c *gin.Context) {
code = scriptError.Code
message = scriptError.Message
} else {
ctl.Logger.Error(c.Request.Context(), "internal errors executing script: %s", err)
sharedlogging.GetLogger(c.Request.Context()).Errorf("internal errors executing script: %s", err)
}
res.ErrorResponse = sharedapi.ErrorResponse{
ErrorCode: code,
Expand Down
2 changes: 0 additions & 2 deletions pkg/api/internal/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/numary/ledger/pkg/core"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/ledgertesting"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/storage"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -169,7 +168,6 @@ func WithNewModule(t *testing.T, options ...fx.Option) {
ledger.ResolveModule(),
storage.DefaultModule(),
ledgertesting.StorageModule(),
logging.LogrusModule(),
fx.NopLogger,
}, options...)
options = append(options, fx.Invoke(func() {
Expand Down
7 changes: 2 additions & 5 deletions pkg/api/middlewares/ledger_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ package middlewares

import (
"github.com/gin-gonic/gin"
"github.com/numary/go-libs/sharedlogging"
"github.com/numary/ledger/pkg/api/controllers"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/logging"
)

// LedgerMiddleware struct
type LedgerMiddleware struct {
resolver *ledger.Resolver
logger logging.Logger
}

// NewLedgerMiddleware
func NewLedgerMiddleware(
resolver *ledger.Resolver,
logger logging.Logger,
) LedgerMiddleware {
return LedgerMiddleware{
resolver: resolver,
logger: logger,
}
}

Expand All @@ -41,7 +38,7 @@ func (m *LedgerMiddleware) LedgerMiddleware() gin.HandlerFunc {
defer func() {
err := l.Close(c.Request.Context())
if err != nil {
m.logger.Warn(c.Request.Context(), "error closing ledger: %s", err)
sharedlogging.GetLogger(c.Request.Context()).Errorf("error closing ledger: %s", err)
}
}()
c.Set("ledger", l)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/middlewares/log_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package middlewares

import (
"github.com/gin-gonic/gin"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/go-libs/sharedlogging"
"time"
)

func Log(logger logging.Logger) gin.HandlerFunc {
func Log() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
latency := time.Now().Sub(start)
logger.WithFields(map[string]interface{}{
sharedlogging.GetLogger(c.Request.Context()).WithFields(map[string]interface{}{
"status": c.Writer.Status(),
"method": c.Request.Method,
"path": c.Request.URL.Path,
"ip": c.ClientIP(),
"latency": latency,
"user_agent": c.Request.UserAgent(),
}).Info(c.Request.Context(), "Request")
}).Info("Request")
}
}
4 changes: 0 additions & 4 deletions pkg/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/numary/ledger/pkg/api/controllers"
"github.com/numary/ledger/pkg/api/middlewares"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/logging"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -44,7 +43,6 @@ type Routes struct {
mappingController controllers.MappingController
globalMiddlewares []gin.HandlerFunc
perLedgerMiddlewares []gin.HandlerFunc
logger logging.Logger
}

// NewRoutes -
Expand All @@ -59,7 +57,6 @@ func NewRoutes(
accountController controllers.AccountController,
transactionController controllers.TransactionController,
mappingController controllers.MappingController,
logger logging.Logger,
) *Routes {
return &Routes{
globalMiddlewares: globalMiddlewares,
Expand All @@ -72,7 +69,6 @@ func NewRoutes(
accountController: accountController,
transactionController: transactionController,
mappingController: mappingController,
logger: logger,
}
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"context"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/go-libs/sharedlogging"
"github.com/spf13/viper"
"os"
"path/filepath"
Expand All @@ -26,7 +26,7 @@ type LedgerStorage struct {
Ledgers []string `json:"ledgers"`
}

func Remember(ctx context.Context, logger logging.Logger, ledger string) {
func Remember(ctx context.Context, ledger string) {
ledgers := viper.GetStringSlice("ledgers")

for _, v := range ledgers {
Expand All @@ -52,14 +52,13 @@ func Remember(ctx context.Context, logger logging.Logger, ledger string) {
if writeTo == "" {
_, err := os.Create(userConfigFile)
if err != nil {
logger.Error(ctx, "failed to create config file: ledger %s will not be remembered", ledger)
sharedlogging.GetLogger(ctx).Errorf("failed to create config file: ledger %s will not be remembered", ledger)
}
}

viper.Set("ledgers", append(ledgers, ledger))
err = viper.WriteConfig()
if err != nil {
logger.Error(ctx, "failed to write config: ledger %s will not be remembered",
ledger)
sharedlogging.GetLogger(ctx).Errorf("failed to write config: ledger %s will not be remembered", ledger)
}
}
2 changes: 0 additions & 2 deletions pkg/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"github.com/numary/ledger/pkg/ledgertesting"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/storage"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -28,7 +27,6 @@ func with(f func(l *Ledger)) {
app := fx.New(
fx.NopLogger,
ledgertesting.StorageModule(),
logging.LogrusModule(),
fx.Provide(storage.NewDefaultFactory),
fx.Invoke(func(lc fx.Lifecycle, storageFactory storage.Factory) {
lc.Append(fx.Hook{
Expand Down
13 changes: 1 addition & 12 deletions pkg/ledger/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ledger

import (
"context"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/storage"
"github.com/numary/ledger/pkg/storage/sqlstorage"
"github.com/pkg/errors"
Expand Down Expand Up @@ -33,25 +32,16 @@ func WithLocker(locker Locker) ResolveOptionFn {
})
}

func WithLogger(logger logging.Logger) ResolveOptionFn {
return func(r *Resolver) error {
r.logger = logger
return nil
}
}

var DefaultResolverOptions = []ResolverOption{
WithStorageFactory(storage.NewDefaultFactory(sqlstorage.NewInMemorySQLiteDriver(logging.DefaultLogger()))),
WithStorageFactory(storage.NewDefaultFactory(sqlstorage.NewInMemorySQLiteDriver())),
WithLocker(NewInMemoryLocker()),
WithLogger(logging.DefaultLogger()),
}

type Resolver struct {
storageFactory storage.Factory
locker Locker
lock sync.RWMutex
initializedStores map[string]struct{}
logger logging.Logger
}

func NewResolver(options ...ResolverOption) *Resolver {
Expand Down Expand Up @@ -113,6 +103,5 @@ func ResolveModule() fx.Option {
fx.Annotate(NewResolver, fx.ParamTags(ResolverOptionsKey)),
),
ProvideResolverOption(WithStorageFactory),
ProvideResolverOption(WithLogger),
)
}
10 changes: 4 additions & 6 deletions pkg/ledgertesting/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ledgertesting
import (
"context"
"fmt"
"github.com/numary/ledger/pkg/logging"
"github.com/numary/ledger/pkg/storage"
"github.com/numary/ledger/pkg/storage/sqlstorage"
"github.com/pborman/uuid"
Expand All @@ -21,11 +20,11 @@ func StorageDriverName() string {
return "sqlite"
}

func Driver(logger logging.Logger) (storage.Driver, func(), error) {
func Driver() (storage.Driver, func(), error) {
switch StorageDriverName() {
case "sqlite":
id := uuid.New()
return sqlstorage.NewOpenCloseDBDriver(logger, "sqlite", sqlstorage.SQLite, func(name string) string {
return sqlstorage.NewOpenCloseDBDriver("sqlite", sqlstorage.SQLite, func(name string) string {
return sqlstorage.SQLiteFileConnString(path.Join(
os.TempDir(),
fmt.Sprintf("%s_%s.db", id, name),
Expand All @@ -37,7 +36,6 @@ func Driver(logger logging.Logger) (storage.Driver, func(), error) {
return nil, nil, err
}
return sqlstorage.NewOpenCloseDBDriver(
logger,
"postgres",
sqlstorage.PostgreSQL,
func(name string) string {
Expand All @@ -52,8 +50,8 @@ func Driver(logger logging.Logger) (storage.Driver, func(), error) {

func StorageModule() fx.Option {
return fx.Options(
fx.Provide(func(logger logging.Logger, lifecycle fx.Lifecycle) (storage.Driver, error) {
driver, stopFn, err := Driver(logger)
fx.Provide(func(lifecycle fx.Lifecycle) (storage.Driver, error) {
driver, stopFn, err := Driver()
if err != nil {
return nil, err
}
Expand Down
Loading