Skip to content

Commit 94e2826

Browse files
BruceMa29makaijun
andauthored
convert falcon data to exporters for prometheus scraping (open-falcon#959)
Co-authored-by: makaijun <[email protected]>
1 parent fc97c2f commit 94e2826

40 files changed

+1345
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CMD = agent aggregator graph hbs judge nodata transfer gateway api alarm
1+
CMD = agent aggregator graph hbs judge nodata transfer gateway api alarm falcon2p8s
22
TARGET = open-falcon
33
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
44
GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")

common/model/prometheus.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package model
2+
3+
// MetricType Prometheus指标类型:GAUGE|COUNTER
4+
type P8sItem struct {
5+
Endpoint string `json:"endpoint"`
6+
Metric string `json:"metric"`
7+
Tags map[string]string `json:"tags"`
8+
Value float64 `json:"value"`
9+
Timestamp int64 `json:"timestamp"`
10+
MetricType string `json:"metric_type"`
11+
Step int `json:"step"`
12+
}

config/falcon2p8s.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"log_level": "debug",
3+
"concurrent": 100,
4+
"http": {
5+
"listen": "0.0.0.0:9090"
6+
},
7+
"rpc": {
8+
"listen": "0.0.0.0:8080"
9+
}
10+
}

config/transfer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,22 @@
7171
"db": "foo",
7272
"address": "http://127.0.0.1:8086",
7373
"timeout": 5000
74+
},
75+
"p8s_relay": {
76+
"enabled": false,
77+
"batch": 200,
78+
"connTimeout": 1000,
79+
"callTimeout": 5000,
80+
"maxConns": 32,
81+
"maxIdle": 32,
82+
"replicas": 500,
83+
"notSyncMetrics": [
84+
"gpu.",
85+
"snmp."
86+
],
87+
"cluster": {
88+
"p8s-relay-00" : "192.168.1.1:8080",
89+
"p8s-relay-01" : "192.168.1.2:8080"
90+
}
7491
}
7592
}

g/g.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func init() {
3636
"gateway": true,
3737
"api": true,
3838
"alarm": true,
39+
"falcon2p8s": true,
3940
}
4041

4142
BinOf = map[string]string{
@@ -49,6 +50,7 @@ func init() {
4950
"gateway": "./gateway/bin/falcon-gateway",
5051
"api": "./api/bin/falcon-api",
5152
"alarm": "./alarm/bin/falcon-alarm",
53+
"falcon2p8s": "./falcon2p8s/bin/falcon-falcon2p8s",
5254
}
5355

5456
cfgOf = map[string]string{
@@ -62,6 +64,7 @@ func init() {
6264
"gateway": "./gateway/config/cfg.json",
6365
"api": "./api/config/cfg.json",
6466
"alarm": "./alarm/config/cfg.json",
67+
"falcon2p8s": "./falcon2p8s/config/cfg.json",
6568
}
6669

6770
ModuleApps = map[string]string{
@@ -75,6 +78,7 @@ func init() {
7578
"gateway": "falcon-gateway",
7679
"api": "falcon-api",
7780
"alarm": "falcon-alarm",
81+
"falcon2p8s": "falcon-falcon2p8s",
7882
}
7983

8084
logpathOf = map[string]string{
@@ -88,6 +92,7 @@ func init() {
8892
"gateway": "./gateway/logs/gateway.log",
8993
"api": "./api/logs/api.log",
9094
"alarm": "./alarm/logs/alarm.log",
95+
"falcon2p8s": "./falcon2p8s/logs/falcon2p8s.log",
9196
}
9297

9398
PidOf = map[string]string{
@@ -101,6 +106,7 @@ func init() {
101106
"gateway": "<NOT SET>",
102107
"api": "<NOT SET>",
103108
"alarm": "<NOT SET>",
109+
"falcon2p8s": "<NOT SET>",
104110
}
105111

106112
// Modules are deployed in this order
@@ -115,6 +121,7 @@ func init() {
115121
"gateway",
116122
"api",
117123
"alarm",
124+
"falcon2p8s",
118125
}
119126
}
120127

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ require (
2828
github.com/niean/go-metrics-lite v0.0.0-20151230091537-b5d30971b578 // indirect
2929
github.com/niean/goperfcounter v0.0.0-20160108100052-24860a8d3fac
3030
github.com/niean/gotools v0.0.0-20151221085310-ff3f51fc5c60 // indirect
31+
github.com/oleiade/lane v1.0.1
3132
github.com/open-falcon/rrdlite v0.0.0-20170412122036-7d8646c85cc5
33+
github.com/prometheus/client_golang v1.5.1
3234
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
3335
github.com/sirupsen/logrus v1.8.1
3436
github.com/smartystreets/goconvey v1.6.4

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ github.com/benbjohnson/immutable v0.2.1/go.mod h1:uc6OHo6PN2++n98KHLxW8ef4W42ylH
102102
github.com/benbjohnson/tmpl v1.0.0/go.mod h1:igT620JFIi44B6awvU9IsDhR77IXWtFigTLil/RPdps=
103103
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
104104
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
105+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
105106
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
106107
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
107108
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
@@ -624,6 +625,8 @@ github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtb
624625
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
625626
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
626627
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
628+
github.com/oleiade/lane v1.0.1 h1:hXofkn7GEOubzTwNpeL9MaNy8WxolCYb9cInAIeqShU=
629+
github.com/oleiade/lane v1.0.1/go.mod h1:IyTkraa4maLfjq/GmHR+Dxb4kCMtEGeb+qmhlrQ5Mk4=
627630
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
628631
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
629632
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

modules/falcon2p8s/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.log
2+
.idea
3+
.DS_Store
4+
/var
5+
/falcon2p8s*
6+
/cfg.json
7+
/gitversion

modules/falcon2p8s/ReadMe.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# falcon2p8s
2+
3+
## 该组件是一个prometheus exporter,实现了将falcon监控数据转储到prometheus
4+
5+
6+
## 源码编译
7+
8+
```bash
9+
cd $GOPATH/src/github.com/open-falcon
10+
git clone https://github.com/open-falcon/falcon-plus.git
11+
cd falcon-plus/modules/falcon2p8s
12+
go get
13+
./control build
14+
./control pack
15+
```
16+
17+
最后一步会pack出一个tar.gz的安装包,拿着这个包去部署服务即可。
18+
19+
## 服务部署
20+
服务部署,包括配置修改、启动服务、检验服务、停止服务等。这之前,需要将安装包解压到服务的部署目录下。
21+
22+
```bash
23+
# 修改配置, 配置项含义见下文
24+
mv cfg.example.json cfg.json
25+
vim cfg.json
26+
27+
# 启动服务
28+
./control start
29+
30+
# 校验服务,看端口是否在监听
31+
ss -tln
32+
33+
# 检查log
34+
./control tail
35+
36+
...
37+
# 停止服务
38+
./control stop
39+
40+
```
41+
42+
## 配置说明
43+
配置文件默认为./cfg.json。默认情况下,安装包会有一个cfg.example.json的配置文件示例。各配置项的含义,如下
44+
45+
```bash
46+
## Configuration
47+
{
48+
"log_level": "debug",
49+
"concurrent": 100, # 处理metric的最大并发数
50+
"http": {
51+
"listen": "0.0.0.0:9090"
52+
},
53+
"rpc": {
54+
"listen": "0.0.0.0:8080"
55+
}
56+
}
57+
```
58+
59+
sm.example.yaml是使用prometheus operator中servicemonitor做服务发现时,对应的yaml,作为参考

modules/falcon2p8s/cfg.example.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"log_level": "debug",
3+
"concurrent": 100,
4+
"http": {
5+
"listen": "0.0.0.0:9090"
6+
},
7+
"rpc": {
8+
"listen": "0.0.0.0:8080"
9+
}
10+
}

modules/falcon2p8s/control

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
WORKSPACE=$(cd $(dirname $0)/; pwd)
4+
cd $WORKSPACE
5+
6+
mkdir -p var
7+
8+
module=falcon2p8s
9+
app=falcon2p8s
10+
conf=cfg.json
11+
pidfile=var/app.pid
12+
logfile=var/app.log
13+
14+
function check_pid() {
15+
if [ -f $pidfile ];then
16+
pid=`cat $pidfile`
17+
if [ -n $pid ]; then
18+
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
19+
return $running
20+
fi
21+
fi
22+
return 0
23+
}
24+
25+
function start() {
26+
check_pid
27+
running=$?
28+
if [ $running -gt 0 ];then
29+
echo -n "$app now is running already, pid="
30+
cat $pidfile
31+
return 1
32+
fi
33+
34+
nohup ./$app -c $conf >> $logfile 2>&1 &
35+
echo $! > $pidfile
36+
echo "$app started..., pid=$!"
37+
}
38+
39+
function stop() {
40+
pid=`cat $pidfile`
41+
kill $pid
42+
echo "$app stopped..."
43+
}
44+
45+
function restart() {
46+
stop
47+
sleep 1
48+
start
49+
}
50+
51+
function status() {
52+
check_pid
53+
running=$?
54+
if [ $running -gt 0 ];then
55+
echo -n "$app now is running, pid="
56+
cat $pidfile
57+
else
58+
echo "$app is stopped"
59+
fi
60+
}
61+
62+
function tailf() {
63+
tail -f $logfile
64+
}
65+
66+
function build() {
67+
go build
68+
if [ $? -ne 0 ]; then
69+
exit $?
70+
fi
71+
mv $module $app
72+
./$app -v
73+
}
74+
75+
function pack() {
76+
build
77+
git log -1 --pretty=%h > gitversion
78+
version=`./$app -v`
79+
tar zcvf $app-$version.tar.gz control cfg.example.json $app gitversion
80+
}
81+
82+
function packbin() {
83+
build
84+
git log -1 --pretty=%h > gitversion
85+
version=`./$app -v`
86+
tar zcvf $app-bin-$version.tar.gz $app gitversion
87+
}
88+
89+
function help() {
90+
echo "$0 build|pack|packbin|start|stop|restart|status|tail"
91+
}
92+
93+
if [ "$1" == "" ]; then
94+
help
95+
elif [ "$1" == "stop" ];then
96+
stop
97+
elif [ "$1" == "start" ];then
98+
start
99+
elif [ "$1" == "restart" ];then
100+
restart
101+
elif [ "$1" == "status" ];then
102+
status
103+
elif [ "$1" == "tail" ];then
104+
tailf
105+
elif [ "$1" == "build" ];then
106+
build
107+
elif [ "$1" == "pack" ];then
108+
pack
109+
elif [ "$1" == "packbin" ];then
110+
packbin
111+
else
112+
help
113+
fi

modules/falcon2p8s/g/cfg.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g
2+
3+
import (
4+
"encoding/json"
5+
"sync"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/toolkits/file"
9+
)
10+
11+
type HttpConfig struct {
12+
Listen string `json:"listen"`
13+
}
14+
15+
type RpcConfig struct {
16+
Listen string `json:"listen"`
17+
}
18+
19+
type GlobalConfig struct {
20+
LogLevel string `json:"log_level"`
21+
Concurrent int `json:"concurrent"`
22+
Http *HttpConfig `json:"http"`
23+
Rpc *RpcConfig `json:"rpc"`
24+
}
25+
26+
var (
27+
ConfigFile string
28+
config *GlobalConfig
29+
configLock = new(sync.RWMutex)
30+
)
31+
32+
func Config() *GlobalConfig {
33+
configLock.RLock()
34+
defer configLock.RUnlock()
35+
return config
36+
}
37+
38+
func ParseConfig(cfg string) {
39+
if cfg == "" {
40+
log.Fatalln("use -c to specify configuration file")
41+
}
42+
43+
if !file.IsExist(cfg) {
44+
log.Fatalln("config file:", cfg, "is not existent")
45+
}
46+
47+
ConfigFile = cfg
48+
49+
configContent, err := file.ToTrimString(cfg)
50+
if err != nil {
51+
log.Fatalln("read config file:", cfg, "fail:", err)
52+
}
53+
54+
var c GlobalConfig
55+
err = json.Unmarshal([]byte(configContent), &c)
56+
if err != nil {
57+
log.Fatalln("parse config file:", cfg, "fail:", err)
58+
}
59+
60+
configLock.Lock()
61+
defer configLock.Unlock()
62+
config = &c
63+
log.Println("read config file:", cfg, "successfully")
64+
}

0 commit comments

Comments
 (0)