Skip to content

Commit

Permalink
feat(fxcore): Added possibility to provide extra information
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Jan 23, 2024
1 parent 21e59a0 commit 359aeba
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions fxcore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ import (
var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
fxorm.FxOrmModule, // load the ORM module (provides *gorm.DB)
fx.Provide(service.NewExampleService), // autowire your service (*gorm.DB auto injection)
fxcore.AsCoreExtraInfo("foo", "bar"), // register extra information to display on core dashboard
)
```

Expand Down
62 changes: 51 additions & 11 deletions fxcore/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ import (
"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/trace"
"github.com/rs/zerolog"
"go.uber.org/fx"
)

// FxExtraInfo is the struct used by modules or apps to provide their extra info to fxcore.
type FxExtraInfo struct {
name string
value string
}

// NewFxExtraInfo returns a new FxExtraInfo.
func NewFxExtraInfo(name string, value string) *FxExtraInfo {
return &FxExtraInfo{
name: name,
value: value,
}
}

// Name returns the name of the [FxExtraInfo].
func (i *FxExtraInfo) Name() string {
return i.name
}

// Value returns the value of the [FxExtraInfo].
func (i *FxExtraInfo) Value() string {
return i.value
}

// FxModuleInfo is the interface to implement by modules to provide their info to fxcore.
type FxModuleInfo interface {
Name() string
Expand All @@ -23,36 +48,50 @@ type FxCoreModuleInfo struct {
LogOutput string
TraceProcessor string
TraceSampler string
ExtraInfos map[string]string
}

// FxCoreModuleInfoParam allows injection of the required dependencies in [NewFxCoreModuleInfo].
type FxCoreModuleInfoParam struct {
fx.In
Config *config.Config
ExtraInfos []*FxExtraInfo `group:"core-extra-infos"`
}

// NewFxCoreModuleInfo returns a new [FxCoreModuleInfo].
func NewFxCoreModuleInfo(cfg *config.Config) *FxCoreModuleInfo {
func NewFxCoreModuleInfo(p FxCoreModuleInfoParam) *FxCoreModuleInfo {
logLevel, logOutput := "", ""
if cfg.IsTestEnv() {
if p.Config.IsTestEnv() {
logLevel = zerolog.DebugLevel.String()
logOutput = log.TestOutputWriter.String()
} else {
logLevel = log.FetchLogLevel(cfg.GetString("modules.log.level")).String()
logOutput = log.FetchLogOutputWriter(cfg.GetString("modules.log.output")).String()
logLevel = log.FetchLogLevel(p.Config.GetString("modules.log.level")).String()
logOutput = log.FetchLogOutputWriter(p.Config.GetString("modules.log.output")).String()
}

traceProcessor := ""
traceSampler := trace.FetchSampler(cfg.GetString("modules.trace.sampler.type")).String()
if cfg.IsTestEnv() {
traceSampler := trace.FetchSampler(p.Config.GetString("modules.trace.sampler.type")).String()
if p.Config.IsTestEnv() {
traceProcessor = trace.TestSpanProcessor.String()
} else {
traceProcessor = trace.FetchSpanProcessor(cfg.GetString("modules.trace.processor.type")).String()
traceProcessor = trace.FetchSpanProcessor(p.Config.GetString("modules.trace.processor.type")).String()
}

extraInfos := make(map[string]string)
for _, info := range p.ExtraInfos {
extraInfos[info.Name()] = info.Value()
}

return &FxCoreModuleInfo{
AppName: cfg.AppName(),
AppEnv: cfg.AppEnv(),
AppDebug: cfg.AppDebug(),
AppVersion: cfg.AppVersion(),
AppName: p.Config.AppName(),
AppEnv: p.Config.AppEnv(),
AppDebug: p.Config.AppDebug(),
AppVersion: p.Config.AppVersion(),
LogLevel: logLevel,
LogOutput: logOutput,
TraceProcessor: traceProcessor,
TraceSampler: traceSampler,
ExtraInfos: extraInfos,
}
}

Expand All @@ -69,6 +108,7 @@ func (i *FxCoreModuleInfo) Data() map[string]interface{} {
"env": i.AppEnv,
"debug": i.AppDebug,
"version": i.AppVersion,
"extra": i.ExtraInfos,
},
"log": map[string]interface{}{
"level": i.LogLevel,
Expand Down
13 changes: 12 additions & 1 deletion fxcore/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ func TestNewFxCoreModuleInfo(t *testing.T) {
)
assert.NoError(t, err)

info := fxcore.NewFxCoreModuleInfo(cfg)
info := fxcore.NewFxCoreModuleInfo(
fxcore.FxCoreModuleInfoParam{
Config: cfg,
ExtraInfos: []*fxcore.FxExtraInfo{
fxcore.NewFxExtraInfo("foo", "bar"),
fxcore.NewFxExtraInfo("foo", "baz"),
},
},
)
assert.IsType(t, &fxcore.FxCoreModuleInfo{}, info)
assert.Implements(t, (*fxcore.FxModuleInfo)(nil), info)

Expand All @@ -29,6 +37,9 @@ func TestNewFxCoreModuleInfo(t *testing.T) {
"env": "test",
"debug": true,
"version": "0.1.0",
"extra": map[string]string{
"foo": "baz",
},
},
"log": map[string]interface{}{
"level": "debug",
Expand Down
16 changes: 16 additions & 0 deletions fxcore/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fxcore

import (
"go.uber.org/fx"
)

// AsCoreExtraInfo registers extra information in the core.
func AsCoreExtraInfo(name string, value string) fx.Option {
return fx.Supply(
fx.Annotate(
NewFxExtraInfo(name, value),
fx.As(new(*FxExtraInfo)),
fx.ResultTags(`group:"core-extra-infos"`),
),
)
}
17 changes: 17 additions & 0 deletions fxcore/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fxcore_test

import (
"fmt"
"testing"

"github.com/ankorstore/yokai/fxcore"
"github.com/stretchr/testify/assert"
)

func TestAsCoreExtraInfo(t *testing.T) {
t.Parallel()

result := fxcore.AsCoreExtraInfo("foo", "bar")

assert.Equal(t, "fx.supplyOption", fmt.Sprintf("%T", result))
}
7 changes: 6 additions & 1 deletion fxcore/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ func prepareTestFxModuleInfoRegistry() (*fxcore.FxModuleInfoRegistry, error) {
return fxcore.NewFxModuleInfoRegistry(fxcore.FxModuleInfoRegistryParam{
Infos: []interface{}{
&testModuleInfo{},
fxcore.NewFxCoreModuleInfo(cfg),
fxcore.NewFxCoreModuleInfo(fxcore.FxCoreModuleInfoParam{
Config: cfg,
ExtraInfos: []*fxcore.FxExtraInfo{
fxcore.NewFxExtraInfo("foo", "bar"),
},
}),
"invalid",
},
}), nil
Expand Down

0 comments on commit 359aeba

Please sign in to comment.