Skip to content

Commit

Permalink
- promelog 时区由utc改为local
Browse files Browse the repository at this point in the history
- 使用makefile编译
- 添加Dockerfile
- 完善readme
  • Loading branch information
ning1875 committed Jan 13, 2021
1 parent f759960 commit e626ef7
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 50 deletions.
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.14-alpine as builder
WORKDIR /usr/src/app
ENV GOPROXY=https://goproxy.cn
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk add --no-cache upx ca-certificates tzdata
COPY ./go.mod ./
COPY ./go.sum ./
RUN go mod download
COPY . .

RUN BUILDUSER=`whoami`@`hostname` ;\
BUILDDATE=`date +'%Y-%m-%d %H:%M:%S'` ;\
GITREVISION=`git rev-parse HEAD` ;\
GITVERSION=`cat VERSION` ;\
GITBRANCH=`git symbolic-ref --short -q HEAD` ;\
LDFLAGES=" -X 'github.com/prometheus/common/version.BuildUser=${BUILDUSER}' -X 'github.com/prometheus/common/version.BuildDate=${BUILDDATE}' -X 'github.com/prometheus/common/version.Revision=${GITREVISION}' -X 'github.com/prometheus/common/version.Version=${GITVERSION}' -X 'github.com/prometheus/common/version.Branch=${GITBRANCH}' ";\
echo ${LDFLAGES} ;\
CGO_ENABLED=0 go build -o server -ldflags "${LDFLAGES}"
FROM busybox as runner
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/src/app/server /opt/app/stree-index
ENTRYPOINT [ "/opt/app/stree-index" ]
CMD [ "--config.file=/etc/stree-index.yml"]

102 changes: 68 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ type HeadIndexReader struct {
- db中所有的字段出timestamp外都可以用来构建索引,而后能所有的字段都可以被用作查询条件
举例
```python
req_data = {
'resource_type': 'elb',
'use_index': True,
'labels': [
# 查询 group 不等于CBS,name ,正则匹配.*0dff.*,stree-app等于collecter的elb资源列表
{'key': 'group', 'value': 'CBS', 'type': 2},
{'key': 'name', 'value': '.*0dff.*', 'type': 3},
{'key': 'stree-app', 'value': 'collecter', 'type': 1}]
}
req_data = {
'resource_type': 'elb',
'use_index': True,
'labels': [
# 查询 group 不等于CBS,name ,正则匹配.*0dff.*,stree-app等于collecter的elb资源列表
{'key': 'group', 'value': 'CBS', 'type': 2},
{'key': 'name', 'value': '.*0dff.*', 'type': 3},
{'key': 'stree-app', 'value': 'collecter', 'type': 1}]
}
```
### 按key查询分布情况的实现
![image.png](/img/bVcHcIS)
Expand Down Expand Up @@ -192,45 +192,80 @@ def query_dis():
```

# 使用
## step 1 准备工作
> 准备mysql和redis,并修改配置文件对应字段
## 创建表
根据scripts/db_schema.sql 建表
### 资源数据表

> 根据scripts/db_schema.sql 建表
> 资源数据表
- ecs 云服务器
- elb 云负载均衡器
- rds 云关系型数据库
- dcs 云缓存
- 对应表名为
`service_tree_ecs` `service_tree_elb` `service_tree_rds` `service_tree_dcs`
### ecs 云服务器规格表 `service_tree_cloud_instance_type`
### 树结构path表

> ecs 云服务器规格表 `service_tree_cloud_instance_type`
> 树结构path表
>
## 灌入数据
- 资源数据可以由同步得来,自行实现即可
- 各个资源表中数据tags字段为json类型,切必须包含stree-index.yml的服务树tag
```yaml
# g.p.a模型key对应table中json字段名称
tree_info:
name_g: group
name_p: stree-project
name_a: stree-app
```
- 各个资源表中数据tags字段为json类型,切必须包含stree-index.yml的服务树tag

```yaml
# g.p.a模型key对应table中json字段名称
tree_info:
name_g: group
name_p: stree-project
name_a: stree-app
```
![image.png](/img/bVcHcKn)
- ecs 云服务器规格表 可以由`scripts/instance_type_insert.sh`灌入,其中包含华为和aws的大部分规格数据
## 安装stree-index
```shell
git clone https://github.com/ning1875/stree-index.git
cd dynamic-sharding/pkg/ && go build -o stree-index main.go

## step 2 编译或下载
> 直接下载
```shell script
wget https://github.com/ning1875/stree-index/releases/download/v1.0/stree-index-1.0.linux-amd64.tar.gz
```
### 补充stree-index.yml 中db,redis等信息
### 启动服务
```c
> 自行编译

```shell script
git clone https://github.com/ning1875/stree-index.git
cd stree-index && make
```
## step 3 补充信息
> 补充stree-index.yml 中db,redis等信息

## step 4 启动服务
> 直接启动
```shell script
./stree-index --config.file=stree-index.yml
```
### stree-index会自动根据资源表中服务树tag构建服务树
```c
查询path表应该有数据
> 使用systemd启动
```shell script
/bin/cp -f stree-index.service /etc/systemd/system/
/bin/cp -f stree-index /bin/
mkdir -pv /etc/stree-index
/bin/cp -f stree-index.yml /etc/stree-index/
systemctl enable /etc/systemd/system/stree-index.service
systemctl start stree-index.service
```
> 观察日志
```shell script
tail -f /var/log/messages |grep stree-index
```
## step 5 stree-index会自动根据资源表中服务树tag构建服务树
> 查询path表应该有数据

```shell script
select * from service_tree_path_tree limit 20;
```

# 查询
## 使用stree-index
### 查询接口数据结构
```
Expand Down Expand Up @@ -276,10 +311,10 @@ select * from service_tree_path_tree limit 20;
- 2:not_eq 不等于 : key!=value
- 3:reg 正则匹配 : key=~value
- 4:not_reg 正则非匹配 : key!~value
~~- 对比 : key> value~~
~~- 对比 : key> value~~ (暂时没支持)

### 查询条件自由组合
`labels`可传入多个key和value组合,可自由组合不同kv查询
- `labels`可传入多个key和value组合,可自由组合不同kv查询

## 运维stree-index
### 监控
Expand All @@ -297,5 +332,4 @@ stree-index 会打点,使用prometheus采集查看即可
static_configs:
- targets:
- $stree-index:9393

```
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ require (
github.com/go-sql-driver/mysql v1.5.0
github.com/jinzhu/gorm v1.9.15
github.com/mcuadros/go-gin-prometheus v0.1.0
github.com/oklog/run v1.1.0 // indirect
github.com/oklog/run v1.1.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.0
github.com/prometheus/common v0.10.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/viper v1.7.0
github.com/zsais/go-gin-prometheus v0.1.0
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a // indirect
gopkg.in/redis.v4 v4.2.4
gopkg.in/yaml.v2 v2.3.0
Expand Down
63 changes: 48 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,71 @@
package main

import (
"context"
"os"
"os/signal"
"context"
"path/filepath"
"syscall"
"time"

"gopkg.in/alecthomas/kingpin.v2"
"github.com/oklog/run"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
promlogflag "github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"

"stree-index/pkg"
"stree-index/pkg/config"
"stree-index/pkg/web"
"stree-index/pkg/statistics"
"stree-index/pkg/mem-index"
"stree-index/pkg/statistics"
"stree-index/pkg/web"
)

func main() {

var (
configFile = kingpin.Flag("config.file", "stree-index configuration file path.").Default("stree-index.yml").String()
app = kingpin.New(filepath.Base(os.Args[0]), "The stree-index")
//configFile = kingpin.Flag("config.file", "docker-mon configuration file path.").Default("docker-mon.yml").String()
configFile = app.Flag("config.file", "docker-mon configuration file path.").Default("stree-index.yml").String()
)

// init logger
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("stree-index"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
promlogConfig := promlog.Config{}

app.Version(version.Print("stree-index"))
app.HelpFlag.Short('h')
promlogflag.AddFlags(app, &promlogConfig)
kingpin.MustParse(app.Parse(os.Args[1:]))

var logger log.Logger
logger = func(config *promlog.Config) log.Logger {
var (
l log.Logger
le level.Option
)
if config.Format.String() == "logfmt" {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
} else {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}

switch config.Level.String() {
case "debug":
le = level.AllowDebug()
case "info":
le = level.AllowInfo()
case "warn":
le = level.AllowWarn()
case "error":
le = level.AllowError()
}
l = level.NewFilter(l, le)
l = log.With(l, "ts", log.TimestampFormat(
func() time.Time { return time.Now().Local() },
"2006-01-02T15:04:05.000Z07:00",
), "caller", log.DefaultCaller)
return l
}(&promlogConfig)

// new ctxall
ctxAll, cancelAll := context.WithCancel(context.Background())
Expand Down
34 changes: 34 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
GOCMD=go
GOBUILD=${GOCMD} build
GOCLEAN=${GOCMD} clean
GOTEST=${GOCMD} test
GOGET=${GOCMD} get

DATE= `date +%FT%T%z`


BINARY_NAME="`pwd |awk -F '/' '{print $NF}'`"
BINARY_LINUX=${BINARY_NAME}_linux
BUILDUSER=`whoami`@`hostname`
BUILDDATE=`date +'%Y-%m-%d %H:%M:%S'`
GITREVISION=`git rev-parse HEAD`
GITVERSION=`cat VERSION`
GITBRANCH=`git symbolic-ref --short -q HEAD`
LDFLAGES=" -X 'github.com/prometheus/common/version.BuildUser=${BUILDUSER}' -X 'github.com/prometheus/common/version.BuildDate=${BUILDDATE}' -X 'github.com/prometheus/common/version.Revision=${GITREVISION}' -X 'github.com/prometheus/common/version.Version=${GITVERSION}' -X 'github.com/prometheus/common/version.Branch=${GITBRANCH}' "

all: build
build:
${GOBUILD} -v -ldflags ${LDFLAGES} -o ${BINARY_NAME}
test:
${GOTEST} -v ./...
clean:
${GOCLEAN}
rm -f ${BINARY_NAME}
rm -f ${BINARY_LINUX}
run:
${GOBUILD} -o ${BINARY_NAME} -v ./...
./${BINARY_NAME}


build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ${GOBUILD} -ldflags ${LDFLAGES} -o ${BINARY_LINUX} -v
19 changes: 19 additions & 0 deletions stree-index.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description="ning1875 opensource service tree"
Documentation=https://github.com/ning1875/stree-index
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/stree-index -f /etc/stree-index/stree-index.yml
Restart=on-failure
RestartSecs=10s
SuccessExitStatus=0
LimitNOFILE=3000000
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=stree-index

[Install]
WantedBy=multi-user.target

0 comments on commit e626ef7

Please sign in to comment.