/
info.go
128 lines (113 loc) · 3.29 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package fxcore
import (
"github.com/ankorstore/yokai/config"
"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 the core.
type FxExtraInfo interface {
Name() string
Value() string
}
// fxExtraInfo is the default [FxExtraInfo] implementation.
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 the core.
type FxModuleInfo interface {
Name() string
Data() map[string]any
}
// FxCoreModuleInfo is a module info collector for the core.
type FxCoreModuleInfo struct {
AppName string
AppEnv string
AppDebug bool
AppVersion string
LogLevel string
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(p FxCoreModuleInfoParam) *FxCoreModuleInfo {
logLevel, logOutput := "", ""
if p.Config.IsTestEnv() {
logLevel = zerolog.DebugLevel.String()
logOutput = log.TestOutputWriter.String()
} else {
logLevel = log.FetchLogLevel(p.Config.GetString("modules.log.level")).String()
logOutput = log.FetchLogOutputWriter(p.Config.GetString("modules.log.output")).String()
}
traceProcessor := ""
traceSampler := trace.FetchSampler(p.Config.GetString("modules.trace.sampler.type")).String()
if p.Config.IsTestEnv() {
traceProcessor = trace.TestSpanProcessor.String()
} else {
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: 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,
}
}
// Name return the name of the module info.
func (i *FxCoreModuleInfo) Name() string {
return ModuleName
}
// Data return the data of the module info.
func (i *FxCoreModuleInfo) Data() map[string]interface{} {
return map[string]interface{}{
"app": map[string]interface{}{
"name": i.AppName,
"env": i.AppEnv,
"debug": i.AppDebug,
"version": i.AppVersion,
},
"log": map[string]interface{}{
"level": i.LogLevel,
"output": i.LogOutput,
},
"trace": map[string]interface{}{
"processor": i.TraceProcessor,
"sampler": i.TraceSampler,
},
"extra": i.ExtraInfos,
}
}