add usage report

This commit is contained in:
Ulric Qin 2022-05-17 19:24:06 +08:00
parent dd5ae29f82
commit 2bea8b7c84
6 changed files with 132 additions and 24 deletions

View File

@ -1,4 +1,4 @@
package engine
package conv
import (
"math"

View File

@ -119,25 +119,26 @@ func MustLoad(fpaths ...string) {
}
type Config struct {
RunMode string
ClusterName string
BusiGroupLabelKey string
EngineDelay int64
Log logx.Config
HTTP httpx.Config
BasicAuth gin.Accounts
SMTP SMTPConfig
Heartbeat HeartbeatConfig
Alerting Alerting
NoData NoData
Redis storage.RedisConfig
Gorm storage.Gorm
MySQL storage.MySQL
Postgres storage.Postgres
WriterOpt writer.GlobalOpt
Writers []writer.Options
Reader reader.Options
Ibex Ibex
RunMode string
ClusterName string
BusiGroupLabelKey string
EngineDelay int64
DisableUsageReport bool
Log logx.Config
HTTP httpx.Config
BasicAuth gin.Accounts
SMTP SMTPConfig
Heartbeat HeartbeatConfig
Alerting Alerting
NoData NoData
Redis storage.RedisConfig
Gorm storage.Gorm
MySQL storage.MySQL
Postgres storage.Postgres
WriterOpt writer.GlobalOpt
Writers []writer.Options
Reader reader.Options
Ibex Ibex
}
type HeartbeatConfig struct {

View File

@ -11,6 +11,7 @@ import (
"github.com/toolkits/pkg/str"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/server/common/conv"
"github.com/didi/nightingale/v5/src/server/config"
"github.com/didi/nightingale/v5/src/server/memsto"
"github.com/didi/nightingale/v5/src/server/naming"
@ -106,7 +107,7 @@ func (r RuleEval) Work() {
return
}
r.judge(ConvertVectors(value))
r.judge(conv.ConvertVectors(value))
}
type WorkersType struct {
@ -172,7 +173,7 @@ func (ws *WorkersType) Build(rids []int64) {
}
}
func (r RuleEval) judge(vectors []Vector) {
func (r RuleEval) judge(vectors []conv.Vector) {
// 有可能rule的一些配置已经发生变化比如告警接收人、callbacks等
// 这些信息的修改是不会引起worker restart的但是确实会影响告警处理逻辑
// 所以这里直接从memsto.AlertRuleCache中获取并覆盖

View File

@ -13,8 +13,8 @@ import (
"github.com/toolkits/pkg/ginx"
"github.com/didi/nightingale/v5/src/server/common"
"github.com/didi/nightingale/v5/src/server/common/conv"
"github.com/didi/nightingale/v5/src/server/config"
"github.com/didi/nightingale/v5/src/server/engine"
"github.com/didi/nightingale/v5/src/server/idents"
"github.com/didi/nightingale/v5/src/server/memsto"
"github.com/didi/nightingale/v5/src/server/reader"
@ -41,7 +41,7 @@ func queryPromql(c *gin.Context) {
return
}
c.JSON(200, engine.ConvertVectors(value))
c.JSON(200, conv.ConvertVectors(value))
}
func remoteWrite(c *gin.Context) {

View File

@ -20,6 +20,7 @@ import (
"github.com/didi/nightingale/v5/src/server/reader"
"github.com/didi/nightingale/v5/src/server/router"
"github.com/didi/nightingale/v5/src/server/stat"
"github.com/didi/nightingale/v5/src/server/usage"
"github.com/didi/nightingale/v5/src/server/writer"
"github.com/didi/nightingale/v5/src/storage"
)
@ -153,6 +154,10 @@ func (s Server) initialize() (func(), error) {
// register ident and nodata logic
idents.Handle(ctx)
if !config.C.DisableUsageReport {
go usage.Report()
}
// release all the resources
return fns.Ret(), nil
}

101
src/server/usage/usage.go Normal file
View File

@ -0,0 +1,101 @@
package usage
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/didi/nightingale/v5/src/server/common/conv"
"github.com/didi/nightingale/v5/src/server/reader"
)
const (
url = "http://n9e.io/report"
request = "sum(rate(n9e_server_samples_received_total[5m]))"
)
type Usage struct {
Samples float64 `json:"samples"` // per second
Maintainer string `json:"maintainer"`
Hostname string `json:"hostname"`
}
func getSamples() (float64, error) {
value, warns, err := reader.Reader.Client.Query(context.Background(), request, time.Now())
if err != nil {
return 0, err
}
if len(warns) > 0 {
return 0, fmt.Errorf("occur some warnings: %v", warns)
}
lst := conv.ConvertVectors(value)
if len(lst) == 0 {
return 0, fmt.Errorf("convert result is empty")
}
return lst[0].Value, nil
}
func Report() {
for {
time.Sleep(time.Minute * 10)
report()
}
}
func report() {
sps, err := getSamples()
if err != nil {
return
}
hostname, err := os.Hostname()
if err != nil {
return
}
maintainer := "blank"
u := Usage{
Samples: sps,
Hostname: hostname,
Maintainer: maintainer,
}
post(u)
}
func post(u Usage) error {
body, err := json.Marshal(u)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return err
}
cli := http.Client{
Timeout: time.Second * 10,
}
resp, err := cli.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("got %s", resp.Status)
}
_, err = ioutil.ReadAll(resp.Body)
return err
}