Skip to content

Commit

Permalink
fix orm metrics + use logx as default
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 committed Sep 28, 2024
1 parent 4d472d2 commit a9729c6
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 157 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
go.osspkg.com/errors v0.3.1
go.osspkg.com/events v0.3.0
go.osspkg.com/grape v1.2.0
go.osspkg.com/ioutils v0.4.4
go.osspkg.com/ioutils v0.4.6
go.osspkg.com/logx v0.4.1
go.osspkg.com/network v0.4.2
go.osspkg.com/random v0.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ go.osspkg.com/events v0.3.0 h1:W2IngTsKs0BKYIglqhrETwtpo6uNSZXWRIt0/l7c6dY=
go.osspkg.com/events v0.3.0/go.mod h1:Cjpx+qNM1y2MIAygFyZWYagTuRiYirmKppZQdaZumd4=
go.osspkg.com/grape v1.2.0 h1:4t/AadlqH7kkoUFVn3u65RqOCaYQOHmkiuCulauWeAE=
go.osspkg.com/grape v1.2.0/go.mod h1:/BpreooYFHz+VdKei4ANaYdxl0qcPXU9amT0MODg17c=
go.osspkg.com/ioutils v0.4.4 h1:1DCGtlPn0/OaoRgUxNzRcH1L3K90WyFRY6CPcKbWuMU=
go.osspkg.com/ioutils v0.4.4/go.mod h1:58HhG2NHf9JUtixAH3R2XISlUmJruwVIUZ3039QVjOY=
go.osspkg.com/ioutils v0.4.6 h1:gnfUf6H0NaJNStmI6/TQ+LkfrkF2aYx3nSyeeGbqxJQ=
go.osspkg.com/ioutils v0.4.6/go.mod h1:58HhG2NHf9JUtixAH3R2XISlUmJruwVIUZ3039QVjOY=
go.osspkg.com/logx v0.4.1 h1:EAzp6EfUmx3YurJrIO2heXIGLimi/RwlnxbM8Lpe8jY=
go.osspkg.com/logx v0.4.1/go.mod h1:hz0oh9mu6Wo4+K6Yk59EbqYVj504FWqSDhEi71btM5g=
go.osspkg.com/network v0.4.2 h1:mRJLcXu6KSKX2GkB3lPdL+aZl4vciHnm7W6j4vR6F3E=
Expand Down
5 changes: 2 additions & 3 deletions metrics/plugin_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"go.osspkg.com/goppy/v2/env"
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
)

type ConfigMetrics struct {
Expand Down Expand Up @@ -40,8 +39,8 @@ func (v *ConfigMetrics) Default() {
func WithServer() plugins.Plugin {
return plugins.Plugin{
Config: &ConfigMetrics{},
Inject: func(app env.AppInfo, c *ConfigMetrics, l logx.Logger) Metrics {
return New(app, c.Config, l)
Inject: func(app env.AppInfo, c *ConfigMetrics) Metrics {
return New(app, c.Config)
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.osspkg.com/goppy/v2/env"
"go.osspkg.com/goppy/v2/web"
"go.osspkg.com/logx"
"go.osspkg.com/xc"
)

Expand All @@ -23,11 +22,11 @@ type Server struct {
conf Config
}

func New(app env.AppInfo, c Config, l logx.Logger) *Server {
func New(app env.AppInfo, c Config) *Server {
router := web.NewBaseRouter()
conf := web.Config{Addr: c.Addr, Tag: "metric"}
return &Server{
server: web.NewServer(conf, router, l),
server: web.NewServer(conf, router),
route: router,
conf: c,
app: app,
Expand Down
4 changes: 2 additions & 2 deletions metrics/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestUnit_NewServer(t *testing.T) {
ctx := xc.New()
logBuff := bytes.NewBuffer(nil)

log := logx.New()
log := logx.Default()
log.SetLevel(logx.LevelError)
log.SetOutput(logBuff)

Expand All @@ -55,7 +55,7 @@ func TestUnit_NewServer(t *testing.T) {
},
}

srv := metrics.New(app, conf, log)
srv := metrics.New(app, conf)
casecheck.NoError(t, err)
casecheck.NoError(t, srv.Up(ctx))

Expand Down
20 changes: 1 addition & 19 deletions orm/devnull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,10 @@

package orm

import (
"io"

"go.osspkg.com/logx"
)

var (
DevNullLog logx.Logger = &devNullLogger{}
DevNullMetric MetricExecutor = new(devNullMetric)
DevNullMetric metricExecutor = new(devNullMetric)
)

type devNullMetric struct{}

func (devNullMetric) ExecutionTime(_ string, call func()) { call() }

type devNullLogger struct{}

func (devNullLogger) SetOutput(out io.Writer) {}
func (devNullLogger) SetFormatter(f logx.Formatter) {}
func (devNullLogger) SetLevel(v uint32) {}
func (devNullLogger) Fatal(message string, args ...interface{}) {}
func (devNullLogger) Error(message string, args ...interface{}) {}
func (devNullLogger) Warn(message string, args ...interface{}) {}
func (devNullLogger) Info(message string, args ...interface{}) {}
func (devNullLogger) Debug(message string, args ...interface{}) {}
28 changes: 9 additions & 19 deletions orm/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,28 @@ package orm

import (
"time"

"go.osspkg.com/goppy/v2/metrics"
)

type (
metric struct {
metrics MetricWriter
name string
}
// MetricExecutor interface
MetricExecutor interface {
metricExecutor interface {
ExecutionTime(name string, call func())
}
// MetricWriter interface
MetricWriter interface {
Metric(name string, time time.Duration)
}
)

// StdOutMetric simple stdout metrig writer
var StdOutMetric = NewMetric(StdOutWriter)

// NewMetric init new metric
func NewMetric(m MetricWriter) MetricExecutor {
return &metric{metrics: m}
func newMetric(name string) metricExecutor {
return &metric{
name: name,
}
}

// ExecutionTime calculating the execution time
func (m *metric) ExecutionTime(name string, call func()) {
if m.metrics == nil {
call()
return
}

t := time.Now()
call()
m.metrics.Metric(name, time.Since(t))
metrics.HistogramVec(m.name, "query", name).Observe(time.Since(t).Seconds())
}
27 changes: 0 additions & 27 deletions orm/metric_test.go

This file was deleted.

6 changes: 2 additions & 4 deletions orm/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ type (
Migrate struct {
conn ORM
conf []ConfigMigrateItem
log logx.Logger
}
)

func NewMigrate(conn ORM, conf []ConfigMigrateItem, log logx.Logger) *Migrate {
func NewMigrate(conn ORM, conf []ConfigMigrateItem) *Migrate {
return &Migrate{
conn: conn,
conf: conf,
log: log,
}
}

Expand Down Expand Up @@ -187,7 +185,7 @@ func (v *Migrate) executor(ctx context.Context,
if _, ok := exist[name]; ok {
continue
}
v.log.Info("New migration", "file", filePath)
logx.Info("New migration", "file", filePath)
b, err0 := os.ReadFile(filePath)
if err0 != nil {
return errors.Wrapf(err0, "read migration file [%s]", name)
Expand Down
24 changes: 10 additions & 14 deletions orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type (
opts *options
mux syncing.Lock
ctx context.Context
log logx.Logger
}

ORM interface {
Expand All @@ -37,23 +36,21 @@ type (
}

options struct {
Logger logx.Logger
Metrics MetricExecutor
Metrics metricExecutor
}

PluginSetup func(o *options)
Option func(o *options)
)

func UseMetric(m MetricExecutor) PluginSetup {
func UseMetric(name string) Option {
return func(o *options) {
o.Metrics = m
o.Metrics = newMetric(name)
}
}

// New init database connections
func New(ctx context.Context, log logx.Logger, opts ...PluginSetup) ORM {
func New(ctx context.Context, opts ...Option) ORM {
o := &options{
Logger: log,
Metrics: DevNullMetric,
}

Expand All @@ -67,7 +64,6 @@ func New(ctx context.Context, log logx.Logger, opts ...PluginSetup) ORM {
opts: o,
mux: syncing.NewLock(),
ctx: ctx,
log: log,
}

routine.Interval(ctx, time.Second*15, db.checkConnects)
Expand All @@ -79,7 +75,7 @@ func (v *_orm) Close() {
v.mux.Lock(func() {
for tag, stmt := range v.pool {
if err := stmt.Close(); err != nil {
v.log.Error("Close DB connect", "err", err, "tag", tag)
logx.Error("Close DB connect", "err", err, "tag", tag)
}
delete(v.pool, tag)
delete(v.conns, tag)
Expand All @@ -102,13 +98,13 @@ func (v *_orm) Tag(name string) (s Stmt) {
func (v *_orm) Register(c Connector) {
for _, tag := range c.Tags() {
if err := v.appendConnect(c, tag); err != nil {
v.log.Error("Create DB connect", "err", err, "tag", tag)
logx.Error("Create DB connect", "err", err, "tag", tag)
continue
}
v.mux.Lock(func() {
v.conns[tag] = c
})
v.log.Info("Create DB connect", "dialect", c.Dialect(), "tag", tag)
logx.Info("Create DB connect", "dialect", c.Dialect(), "tag", tag)
}
return
}
Expand Down Expand Up @@ -139,7 +135,7 @@ func (v *_orm) checkConnects(ctx context.Context) {
badTags = make(map[string]Connector, len(v.conns))
for tag, st := range v.pool {
if err := st.PingContext(ctx); err != nil {
v.log.Error("Bad DB connect", "err", err, "tag", tag)
logx.Error("Bad DB connect", "err", err, "tag", tag)
badTags[tag] = nil
}
}
Expand All @@ -163,7 +159,7 @@ func (v *_orm) checkConnects(ctx context.Context) {
}
for tag, c := range badTags {
if err := v.appendConnect(c, tag); err != nil {
v.log.Error("Create DB connect", "err", err, "tag", tag)
logx.Error("Create DB connect", "err", err, "tag", tag)
}
}
}
5 changes: 2 additions & 3 deletions orm/plugin_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
)

// ConfigMysql mysql config model
Expand Down Expand Up @@ -68,10 +67,10 @@ func WithMysql() plugins.Plugin {

return plugins.Plugin{
Config: &ConfigMysql{},
Inject: func(c *ConfigMysql, o ORM, l logx.Logger) error {
Inject: func(c *ConfigMysql, o ORM) error {
conn := NewMysqlClient(c)
o.Register(conn)
return NewMigrate(o, c.Migrate, l).
return NewMigrate(o, c.Migrate).
Run(context.TODO(), conn.Dialect())
},
}
Expand Down
7 changes: 3 additions & 4 deletions orm/plugin_orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ package orm

import (
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
"go.osspkg.com/xc"
)

func WithORM() plugins.Plugin {
func WithORM(opts ...Option) plugins.Plugin {
return plugins.Plugin{
Inject: func(ctx xc.Context, l logx.Logger) ORM {
o := New(ctx.Context(), l)
Inject: func(ctx xc.Context) ORM {
o := New(ctx.Context(), opts...)
go func() {
select {
case <-ctx.Done():
Expand Down
5 changes: 2 additions & 3 deletions orm/plugin_pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
)

// ConfigPgsql pgsql config model
Expand Down Expand Up @@ -64,10 +63,10 @@ func WithPGSql() plugins.Plugin {

return plugins.Plugin{
Config: &ConfigPgsql{},
Inject: func(c *ConfigPgsql, o ORM, l logx.Logger) error {
Inject: func(c *ConfigPgsql, o ORM) error {
conn := NewPGSqlClient(c)
o.Register(conn)
return NewMigrate(o, c.Migrate, l).
return NewMigrate(o, c.Migrate).
Run(context.TODO(), conn.Dialect())
},
}
Expand Down
5 changes: 2 additions & 3 deletions orm/plugin_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"

"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
)

// ConfigSqlite sqlite config model
Expand Down Expand Up @@ -56,10 +55,10 @@ func WithSqlite() plugins.Plugin {

return plugins.Plugin{
Config: &ConfigSqlite{},
Inject: func(c *ConfigSqlite, o ORM, l logx.Logger) error {
Inject: func(c *ConfigSqlite, o ORM) error {
conn := NewSqliteClient(c)
o.Register(conn)
return NewMigrate(o, c.Migrate, l).
return NewMigrate(o, c.Migrate).
Run(context.TODO(), conn.Dialect())
},
}
Expand Down
5 changes: 2 additions & 3 deletions web/plugin_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package web

import (
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/logx"
)

// ConfigHttpPool config to initialize HTTP service
Expand All @@ -28,8 +27,8 @@ func (v *ConfigHttpPool) Default() {
func WithServer() plugins.Plugin {
return plugins.Plugin{
Config: &ConfigHttpPool{},
Inject: func(conf *ConfigHttpPool, l logx.Logger) RouterPool {
return newRouteProvider(conf.Config, l)
Inject: func(conf *ConfigHttpPool) RouterPool {
return newRouteProvider(conf.Config)
},
}
}
Loading

0 comments on commit a9729c6

Please sign in to comment.