You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
2.1 KiB

package analyze
import (
"yunyan/comm"
"yunyan/lego/core"
"yunyan/modules"
"yunyan/utils"
)
// 编译期断言:Analyze 实现 comm.IAnalyze 埋点接口。
var _ comm.IAnalyze = (*Analyze)(nil)
func NewModule() core.IModule {
return new(Analyze)
}
// Analyze 统计模块:对外实现 comm.IAnalyze 埋点接口(业务模块经 GetModule 获取后
// 调用 Report,实时累加进本地 Redis);内部由 syncer 定时把 Redis 当日聚合按分区
// 打包成绝对快照推送到 NATS,由 console 消费端唯一写库(本模块不再直接写 stats_global_day)。
type Analyze struct {
modules.ModuleBase
options *Options
syncer *syncComp
}
func (this *Analyze) GetType() core.M_Modules {
return comm.ModuleAnalyze
}
func (this *Analyze) NewOptions() (options core.IModuleOptions) {
return new(Options)
}
func (this *Analyze) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
this.options = options.(*Options)
// 用 集群标签 + 应用名 + 区域 确定 Redis 键前缀(埋点与同步两侧共用)。
setupKeys(service.GetSettings().Tag, this.options.AppName, this.options.Region)
// 统计日界固定时区(默认 Asia/Shanghai),决定 0 点切日口径。
setStatLoc(utils.LoadStatLocation(this.options.Timezone))
if err = this.ModuleBase.Init(service, module, options); err != nil {
return
}
return
}
func (this *Analyze) Start() (err error) {
if err = this.ModuleBase.Start(); err != nil {
return
}
// 核心维度自检:AppName+Region 是统计上报的必备复合维度,缺失则 console 端会全部丢弃。
// 不阻断启动(其余业务照常),仅显著告警提醒修正 modules.analyze 配置。
if this.options.AppName == "" || this.options.Region == 0 {
this.Errorf("analyze: 核心维度缺失 (AppName=%q, Region=%d),统计上报将被 console 丢弃,请检查 modules.analyze 配置",
this.options.AppName, this.options.Region)
}
return
}
func (this *Analyze) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.syncer = this.RegisterComp(new(syncComp)).(*syncComp)
}