16 Star 61 Fork 23

DiDi-opensource / falcon-log-agent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
get_http.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
package strategy
import (
"encoding/json"
"fmt"
"time"
"github.com/didi/falcon-log-agent/common/dlog"
"github.com/didi/falcon-log-agent/common/scheme"
"github.com/didi/falcon-log-agent/common/utils"
"github.com/parnurzeal/gorequest"
)
func getHTTPStrategy(addr, uri string, timeout int) ([]*scheme.Strategy, error) {
hostname, err := utils.LocalHostname()
if err != nil {
return nil, err
}
urlTemp := fmt.Sprintf("%s%s", addr, uri)
url := fmt.Sprintf(urlTemp, hostname)
dlog.Infof("URL in get strategy : [%s]", url)
body, err := getRequest(url, timeout)
if err != nil {
return nil, err
}
var strategyResp []*scheme.Strategy
err = json.Unmarshal([]byte(body), &strategyResp)
if err != nil {
dlog.Errorf("json decode error when update strategy : [%v]", err)
return nil, err
}
return strategyResp, nil
}
func getRequest(url string, timeout int) (string, error) {
request := gorequest.New().Timeout(time.Duration(timeout) * time.Second)
resp, body, errs := request.Get(url).End()
if errs == nil {
if resp.StatusCode != 200 {
dlog.Errorf("get HTTP Request Response: [code:%d][body:%s][errs:%v]", resp.StatusCode, body, errs)
return body, fmt.Errorf("Code is not 200")
}
dlog.Infof("get HTTP Request Response : [code:%d][body:%s]", 200, body)
return body, nil
}
dlog.Errorf("get HTTP Request Response: [body:%s][errs:%v]", body, errs)
return body, fmt.Errorf("%v", errs)
}
Go
1
https://gitee.com/didiopensource/falcon-log-agent.git
git@gitee.com:didiopensource/falcon-log-agent.git
didiopensource
falcon-log-agent
falcon-log-agent
542bf9e4eb8b

搜索帮助