Skip to content

Commit e355505

Browse files
authored
feat(fxcore): Added possibility to provide extra information (#56)
1 parent 21e59a0 commit e355505

File tree

7 files changed

+127
-13
lines changed

7 files changed

+127
-13
lines changed

fxcore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ import (
206206
var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
207207
fxorm.FxOrmModule, // load the ORM module (provides *gorm.DB)
208208
fx.Provide(service.NewExampleService), // autowire your service (*gorm.DB auto injection)
209+
fxcore.AsCoreExtraInfo("foo", "bar"), // register extra information to display on core dashboard
209210
)
210211
```
211212

fxcore/info.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,39 @@ import (
55
"github.com/ankorstore/yokai/log"
66
"github.com/ankorstore/yokai/trace"
77
"github.com/rs/zerolog"
8+
"go.uber.org/fx"
89
)
910

11+
// FxExtraInfo is the struct used by modules or apps to provide their extra info to fxcore.
12+
type FxExtraInfo interface {
13+
Name() string
14+
Value() string
15+
}
16+
17+
// fxExtraInfo is the default [FxExtraInfo] implementation.
18+
type fxExtraInfo struct {
19+
name string
20+
value string
21+
}
22+
23+
// NewFxExtraInfo returns a new FxExtraInfo.
24+
func NewFxExtraInfo(name string, value string) FxExtraInfo {
25+
return &fxExtraInfo{
26+
name: name,
27+
value: value,
28+
}
29+
}
30+
31+
// Name returns the name of the [fxExtraInfo].
32+
func (i *fxExtraInfo) Name() string {
33+
return i.name
34+
}
35+
36+
// Value returns the value of the [fxExtraInfo].
37+
func (i *fxExtraInfo) Value() string {
38+
return i.value
39+
}
40+
1041
// FxModuleInfo is the interface to implement by modules to provide their info to fxcore.
1142
type FxModuleInfo interface {
1243
Name() string
@@ -23,36 +54,50 @@ type FxCoreModuleInfo struct {
2354
LogOutput string
2455
TraceProcessor string
2556
TraceSampler string
57+
ExtraInfos map[string]string
58+
}
59+
60+
// FxCoreModuleInfoParam allows injection of the required dependencies in [NewFxCoreModuleInfo].
61+
type FxCoreModuleInfoParam struct {
62+
fx.In
63+
Config *config.Config
64+
ExtraInfos []FxExtraInfo `group:"core-extra-infos"`
2665
}
2766

2867
// NewFxCoreModuleInfo returns a new [FxCoreModuleInfo].
29-
func NewFxCoreModuleInfo(cfg *config.Config) *FxCoreModuleInfo {
68+
func NewFxCoreModuleInfo(p FxCoreModuleInfoParam) *FxCoreModuleInfo {
3069
logLevel, logOutput := "", ""
31-
if cfg.IsTestEnv() {
70+
if p.Config.IsTestEnv() {
3271
logLevel = zerolog.DebugLevel.String()
3372
logOutput = log.TestOutputWriter.String()
3473
} else {
35-
logLevel = log.FetchLogLevel(cfg.GetString("modules.log.level")).String()
36-
logOutput = log.FetchLogOutputWriter(cfg.GetString("modules.log.output")).String()
74+
logLevel = log.FetchLogLevel(p.Config.GetString("modules.log.level")).String()
75+
logOutput = log.FetchLogOutputWriter(p.Config.GetString("modules.log.output")).String()
3776
}
3877

3978
traceProcessor := ""
40-
traceSampler := trace.FetchSampler(cfg.GetString("modules.trace.sampler.type")).String()
41-
if cfg.IsTestEnv() {
79+
traceSampler := trace.FetchSampler(p.Config.GetString("modules.trace.sampler.type")).String()
80+
if p.Config.IsTestEnv() {
4281
traceProcessor = trace.TestSpanProcessor.String()
4382
} else {
44-
traceProcessor = trace.FetchSpanProcessor(cfg.GetString("modules.trace.processor.type")).String()
83+
traceProcessor = trace.FetchSpanProcessor(p.Config.GetString("modules.trace.processor.type")).String()
84+
}
85+
86+
extraInfos := make(map[string]string)
87+
for _, info := range p.ExtraInfos {
88+
extraInfos[info.Name()] = info.Value()
4589
}
4690

4791
return &FxCoreModuleInfo{
48-
AppName: cfg.AppName(),
49-
AppEnv: cfg.AppEnv(),
50-
AppDebug: cfg.AppDebug(),
51-
AppVersion: cfg.AppVersion(),
92+
AppName: p.Config.AppName(),
93+
AppEnv: p.Config.AppEnv(),
94+
AppDebug: p.Config.AppDebug(),
95+
AppVersion: p.Config.AppVersion(),
5296
LogLevel: logLevel,
5397
LogOutput: logOutput,
5498
TraceProcessor: traceProcessor,
5599
TraceSampler: traceSampler,
100+
ExtraInfos: extraInfos,
56101
}
57102
}
58103

@@ -78,5 +123,6 @@ func (i *FxCoreModuleInfo) Data() map[string]interface{} {
78123
"processor": i.TraceProcessor,
79124
"sampler": i.TraceSampler,
80125
},
126+
"extra": i.ExtraInfos,
81127
}
82128
}

fxcore/info_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ func TestNewFxCoreModuleInfo(t *testing.T) {
1616
)
1717
assert.NoError(t, err)
1818

19-
info := fxcore.NewFxCoreModuleInfo(cfg)
19+
info := fxcore.NewFxCoreModuleInfo(
20+
fxcore.FxCoreModuleInfoParam{
21+
Config: cfg,
22+
ExtraInfos: []fxcore.FxExtraInfo{
23+
fxcore.NewFxExtraInfo("foo", "bar"),
24+
fxcore.NewFxExtraInfo("foo", "baz"),
25+
},
26+
},
27+
)
2028
assert.IsType(t, &fxcore.FxCoreModuleInfo{}, info)
2129
assert.Implements(t, (*fxcore.FxModuleInfo)(nil), info)
2230

@@ -38,6 +46,9 @@ func TestNewFxCoreModuleInfo(t *testing.T) {
3846
"processor": "test",
3947
"sampler": "parent-based-always-on",
4048
},
49+
"extra": map[string]string{
50+
"foo": "baz",
51+
},
4152
},
4253
info.Data(),
4354
)

fxcore/register.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fxcore
2+
3+
import (
4+
"go.uber.org/fx"
5+
)
6+
7+
// AsCoreExtraInfo registers extra information in the core.
8+
func AsCoreExtraInfo(name string, value string) fx.Option {
9+
return fx.Supply(
10+
fx.Annotate(
11+
NewFxExtraInfo(name, value),
12+
fx.As(new(FxExtraInfo)),
13+
fx.ResultTags(`group:"core-extra-infos"`),
14+
),
15+
)
16+
}

fxcore/register_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fxcore_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/ankorstore/yokai/fxcore"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestAsCoreExtraInfo(t *testing.T) {
12+
t.Parallel()
13+
14+
result := fxcore.AsCoreExtraInfo("foo", "bar")
15+
16+
assert.Equal(t, "fx.supplyOption", fmt.Sprintf("%T", result))
17+
}

fxcore/registry_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ func prepareTestFxModuleInfoRegistry() (*fxcore.FxModuleInfoRegistry, error) {
7676
return fxcore.NewFxModuleInfoRegistry(fxcore.FxModuleInfoRegistryParam{
7777
Infos: []interface{}{
7878
&testModuleInfo{},
79-
fxcore.NewFxCoreModuleInfo(cfg),
79+
fxcore.NewFxCoreModuleInfo(fxcore.FxCoreModuleInfoParam{
80+
Config: cfg,
81+
ExtraInfos: []fxcore.FxExtraInfo{},
82+
}),
8083
"invalid",
8184
},
8285
}), nil

fxcore/templates/dashboard.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@
209209
</table>
210210
</div>
211211
</div>
212+
{{ if ne (len .info.ExtraInfos ) 0 }}
213+
<br/>
214+
<div class="card">
215+
<div class="card-body">
216+
<p class="card-title fw-bold">
217+
<i class="bi bi-info-square"></i>&nbsp;&nbsp;Extra information
218+
</p>
219+
<table class="table table-borderless table-sm">
220+
<tbody>
221+
{{ range $infoName, $infoValue := .info.ExtraInfos }}
222+
<tr>
223+
<td class="w-25">{{ $infoName }}</td>
224+
<td><code>{{ $infoValue }}</code></td>
225+
</tr>
226+
{{ end }}
227+
</tbody>
228+
</table>
229+
</div>
230+
</div>
231+
{{ end }}
212232
</div>
213233
</div>
214234
</div>

0 commit comments

Comments
 (0)