夜莺初探四·mtail插件采集日志指标
前言
上一篇介绍了Categraf的配置,这篇我们尝试通过使用google开源的mtail工具来作为Categraf的插件,从应用日志中提取指标数据。
mtail项目介绍和配置文件说明
通过mtail -h可以很方便看到参数详细,也推荐乔克-从日志中提取指标的瑞士军刀或者Dream运维梦工厂-categraf-mtail日志收集插件详解来了解更多,我就不再班门弄斧了。
当然也可以通过官方来了解详情新手村介绍 和高手入门
Categraf采集插件
categraf-mtail插件地址
https://github.com/flashcatcloud/categraf/tree/main/inputs/mtail
源码解读
package mtail
...
//常量值
const inputName = `mtail`
const description = ` extract internal monitoring data from application logs`
//配置
// MTail holds the configuration for the plugin.
type MTail struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
}
//配置文件中instances对象需要参数结构体
type Instance struct {
config.InstanceConfig
/**
type InternalConfig struct {
// append labels
Labels map[string]string `toml:"labels"`
// metrics drop and pass filter
MetricsDrop []string `toml:"metrics_drop"`
MetricsPass []string `toml:"metrics_pass"`
MetricsDropFilter filter.Filter
MetricsPassFilter filter.Filter
// metric name prefix
MetricsNamePrefix string `toml:"metrics_name_prefix"`
// mapping value
ProcessorEnum []*ProcessorEnum `toml:"processor_enum"`
// whether instance initial success
inited bool `toml:"-"`
}
type InstanceConfig struct {
InternalConfig
IntervalTimes int64 `toml:"interval_times"`
}
**/
NamePrefix string `toml:"name_prefix"`
Progs string `toml:"progs"` //规则文件(xxx.mtail)的目录
Logs []string `toml:"logs"` //要监控的日志文件
IgnoreFileRegPattern string `toml:"ignore_filename_regex_pattern"`
OverrideTimeZone string `toml:"override_timezone"` //指定时区
EmitProgLabel string `toml:"emit_prog_label"` //是否导出label标签 string类型的bool值
emitProgLabel bool `toml:"-"`
EmitMetricTimestamp string `toml:"emit_metric_timestamp"` //metrics是否带时间戳 string类型的bool值
emitMetricTimestamp bool `toml:"-"`
PollInterval time.Duration `toml:"poll_interval"`
PollLogInterval time.Duration `toml:"poll_log_interval"`
MetricPushInterval time.Duration `toml:"metric_push_interval"`
MaxRegexpLen int `toml:"max_regexp_length"`
MaxRecursionDepth int `toml:"max_recursion_depth"`
SyslogUseCurrentYear string `toml:"syslog_use_current_year"` // true
sysLogUseCurrentYear bool `toml:"-"`
LogRuntimeErrors string `toml:"vm_logs_runtime_errors"` // true
logRuntimeErrors bool `toml:"-"`
//
ctx context.Context `toml:"-"`
cancel context.CancelFunc `toml:"-"`
m *mtail.Server
}
//配置文件中instances对象的Init函数,调用mtail
func (ins *Instance) Init() error {
//初始化检查,设置默认值
if len(ins.Progs) == 0 || len(ins.Logs) == 0 {
return types.ErrInstancesEmpty
}
// set default value
ins.sysLogUseCurrentYear = ins.SyslogUseCurrentYear == "true"
ins.logRuntimeErrors = ins.LogRuntimeErrors == "true"
ins.emitProgLabel = ins.EmitProgLabel == "true"
ins.emitMetricTimestamp = ins.EmitMetricTimestamp == "true"
if