一个remoteWrite的中间处理层,转存指标名到ES,过滤不规范Metric
Metric = MetricName + Labels
Labels = []{Label.Name=Label.Value})
- 接收RemoteWrite,处理后转存远程时序库存储
- 过滤不规范Metric,丢弃对应Metric
- 将Metric存入Redis和ES中
- Gocache提供本地缓存Metric,减轻reids负担,提供Exist判断,过期时间可配置
- Redis缓存Metric,提供Exist判断,过期时间可配置
- ES存储Metric,提供Metric搜索,通过ES生命周期控制过期时间
- routers 路由层,URL配置,项目接口入口
- controller 控制器层,验证提交的数据,将验证完成的数据传递给 service
- service 业务层,只完成业务逻辑的开发,不进行操作数据库
- dao 数据库层,操作数据的CURD
- Viper 配置管理,监听配置,自动加载更新
- Zap 日志管理
- patrickmn/go-cache 本地缓存
- go-redis/redis redis驱动
- go-elasticsearch/v7 elasticsearch驱动
- gin-contrib/pprof Gin性能分析
- air 能够实时监听项目的代码文件,在代码发生变更之后自动重新编译并执行
使用 golangci-lint和golint做代码规范检测,推荐使用golint
bash build_linux.sh
-
环境变量配置
目前只区分开发环境和线上环境,通过配置环境变量
export GO_ENV=dev
或export GO_ENV=prod
-
配置文件
app: # 项目配置
name: "metric-index" # 项目名称
port: 7001 # 项目端口
log: # 日志配置
level: "debug" # 日志级别
filename: "logs/metric-index.log" # 日志文件相对路径
max_size: 200 # 日志文件大小,单位MB
max_age: 30 # 日志保存时长,单位天
max_backups: 7 # 日志文件保存个数
redis:
host: "127.0.0.1"
port: 6379
password: ""
db: 0
pool_size: 100
metric_filter:
metricNameRegex: ".*"
metricValueRegex: ".*"
labelNameRegex: ".*"
labelValueRegex: ".*"
metric_store:
cache:
isexpire: true
expire: 604800
defaultExpire: 604800
cleanupInterval: 600
dist_interval: 172800
worker_num: 50
flush_lens: 1000
flush_interval: 10
store:
url:
- "http://elasticsearch-host:9200"
username:
password:
index_name: "metric-test"
worker_num: 50 # 写入ES工作协程数
flush_bytes: 5242880 # 批量写入ES的缓存队列大小
flush_interval: 10 # 超时刷新,时间内如果未写满队列则主动刷入ES
remote: # remote wirte转发配置,将接收到的数据转发到指定接口
write:
url: "http://vminsert-host:8480/insert/1/prometheus/api/v1/write"
content_type: "application/x-protobuf"
send:
url: "http://vminsert-host:4242/insert/1/opentsdb/api/put"
content_type: "application/json"
-
配置air配置文件,air能够实时监听项目的代码文件,在代码发生变更之后自动重新编译并执行,大大提高gin框架项目的开发效率
- Air实时加载
- 根据上面教程安装air、配置.air.conf,需要修改full_bin,添加开发环境的环境变量参数
full_bin = "export GO_ENV=dev; ./tmp/main"
- 启动项目
air -c .air.conf
-
goland运行
项目配置(Edit Configurations) → Configuration → Environment → 添加:GO_ENV=dev
-
本地开发环境二进制启动(MAC)
- 配置环境变量
sudo echo 'export GO_ENV=dev' >> ~/.zshrc
- config目录中创建配置文件:dev.yml
- 执行脚本build_linux.sh打包编译
- 启动项目:./metric-index
-
优化系统配置
# 注释/etc/security/limits.d/20-nproc.conf内所有配置 > sed -i 's/^[^#]/#&/g' /etc/security/limits.d/20-nproc.conf # 配置/etc/security/limits.d/limit.conf,重新登录中端使之生效 > cat /etc/security/limits.d/limits.conf | egrep -v '^#|^$' root soft nofile 1024000 root hard nofile 1024000 * soft nofile 1024000 * hard nofile 1024000
-
优化supervisor配置
# 修改文件/etc/supervisord.conf中下面配置项,重启supervisor minfds=1024000 minprocs=1024000
-
配置supervisor项目启动文件
> cat /etc/supervisord.d/metric-index.conf [program:metric-index] command=/opt/metric-index/monica-index directory=/opt/metric-index/ user=root environment=GO_ENV="prod" # ,GOMAXPROCS=7 stderr_logfile=/var/log/supervisor/metric-index-err.log stdout_logfile=/var/log/supervisor/metric-index-info.log autostart=true autorestart=true startsecs=3
-
配置环境变量
sudo echo 'export GO_ENV=prod' >> ~/.zshrc
- config目录中创建配置文件:prod.yml
- 执行脚本build_linux.sh打包编译,二进制文件metric-index发布到/opt/metric-index/
- 启动项目:
supervisorctl update
、supervisorctl start metric-index
-
项目配置了gin-contrib/pprof,可以通过pprof工具进行性能分析,接口为
/debug/pprof/
-
mac安装pprof
-
安装 graphviz,支持打开svg文件
brew install graphviz
-
安装pprof工具
go get github.com/gin-contrib/pprof
-
测试pprof
go tool pprof --help
-
-
执行pprof进行数据收集分析
-
cpu分析
go tool pprof --seconds 60 http://[host]:[port]/debug/pprof/profile
-
memory分析
go tool pprof --seconds 60 http://[host]:[port]/debug/pprof/heap
-
goroutine分析
go tool pprof --seconds 60 http://[host]:[port]/debug/pprof/goroutine
-
-
查看分析结果
- pprof指令执行完后,会提示生成的分析文件位置
- 打开分析文件
- 例如:
go tool pprof -http 127.0.0.1:port [pproffile path]