This commit is contained in:
jiangyong 2024-01-21 18:49:51 +08:00
parent 4dba9b162c
commit bc9c6c5ce3
8 changed files with 82 additions and 61 deletions

View File

@ -3,8 +3,8 @@ package adapi
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/smbrave/gosdk/util"
"github.com/spf13/cast" "github.com/spf13/cast"
"gitlab.batiao8.com/open/gosdk/util"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"

View File

@ -3,7 +3,7 @@ package adminapi
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/smbrave/gosdk/util" "gitlab.batiao8.com/open/gosdk/util"
) )
type Application struct { type Application struct {

View File

@ -3,7 +3,7 @@ package adminapi
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/smbrave/gosdk/util" "gitlab.batiao8.com/open/gosdk/util"
"net/url" "net/url"
) )

7
go.mod
View File

@ -1,5 +1,8 @@
module github.com/smbrave/gosdk module gitlab.batiao8.com/open/gosdk
go 1.18 go 1.18
require github.com/spf13/cast v1.5.0 require (
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/spf13/cast v1.5.0
)

View File

@ -1,13 +1,14 @@
package metric package metric
import "os"
var ( var (
serv *service serv *service
) )
type Config struct { type Config struct {
Address string Address string
Username string
Password string
Database string
Interval int Interval int
} }
@ -23,12 +24,16 @@ func Init(c *Config) error {
return nil return nil
} }
hostname, _ := os.Hostname() serv = NewService(c)
serv = &service{
config: c,
hostname: hostname,
metrics: make(chan *metric, 100000),
}
go serv.run() go serv.run()
return nil return nil
} }
func (c *Config) Default() {
if c.Database == "" {
c.Database = "telegraf"
}
if c.Interval == 0 {
c.Interval = 10
}
}

View File

@ -8,10 +8,11 @@ import (
func TestName(t *testing.T) { func TestName(t *testing.T) {
c := NewConfg() c := NewConfg()
c.Address = "https://monitor.batiao8.com" c.Address = "http://14.22.116.197:9305"
c.Interval = 1
Init(c) Init(c)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
Metric("test", float64(rand.Int()%100), map[string]string{ Metric("test.test.b", float64(rand.Int()%100), map[string]string{
"a": "b", "a": "b",
"c": "d", "c": "d",
}) })

View File

@ -1,13 +1,10 @@
package metric package metric
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"github.com/spf13/cast" influxdb "github.com/influxdata/influxdb1-client"
"io/ioutil" "net/url"
"log" "os"
"net/http"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -21,10 +18,37 @@ type metric struct {
} }
type service struct { type service struct {
hostname string hostname string
metrics chan *metric serviceName string
megers map[string]*metric hostIp string
config *Config podIp string
metrics chan *metric
megers map[string]*metric
config *Config
client *influxdb.Client
}
func NewService(c *Config) *service {
c.Default()
hostname, _ := os.Hostname()
infUrl, _ := url.Parse(c.Address)
infCfg := influxdb.NewConfig()
infCfg.Username = c.Username
infCfg.Password = c.Password
infCfg.URL = *infUrl
client, _ := influxdb.NewClient(infCfg)
return &service{
metrics: make(chan *metric, 100000),
hostname: hostname,
serviceName: os.Getenv("SERVICE_NAME"),
podIp: os.Getenv("POD_IP"),
hostIp: os.Getenv("HOST_IP"),
config: c,
client: client,
}
} }
func (s *service) run() { func (s *service) run() {
@ -66,51 +90,39 @@ func (s *service) process(m *metric) {
s.megers[key] = m s.megers[key] = m
} }
func (s *service) defaultTags(tags map[string]string) map[string]string { func (s *service) defaultTags() map[string]string {
if tags == nil {
tags = map[string]string{ return map[string]string{
"hostname": s.hostname, "hostname": s.hostname,
} "host_ip": s.hostIp,
} else { "pod_ip": s.podIp,
tags["hostname"] = s.hostname "service_name": s.serviceName,
} }
return tags
} }
func (s *service) report() { func (s *service) report() {
if s.megers == nil { if s.megers == nil {
return return
} }
metrics := make([]*metric, 0)
var bp influxdb.BatchPoints
for _, v := range s.megers { for _, v := range s.megers {
v.Tags = s.defaultTags(v.Tags) var p influxdb.Point
metrics = append(metrics, v) p.Measurement = v.Metric
} p.Tags = v.Tags
reqUrl := fmt.Sprintf("%s/opentsdb/put", serv.config.Address) p.Fields = map[string]interface{}{
"value": v.Value,
reqBody, _ := json.Marshal(metrics) }
resp, err := http.Post(reqUrl, "application/json", bytes.NewBuffer(reqBody)) p.Time = time.Unix(v.Timestamp, 0)
if err != nil { bp.Points = append(bp.Points, p)
log.Printf("http.Post error :%s", err.Error())
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf(" ioutil.ReadAll error :%s", err.Error())
return
}
result := make(map[string]interface{})
if err := json.Unmarshal(rspBody, &result); err != nil {
log.Printf("json result : %s", string(rspBody))
return
} }
fail := cast.ToInt(result["fail"]) bp.Database = s.config.Database
if fail != 0 { bp.Tags = s.defaultTags()
log.Printf("http result : %s", string(rspBody))
return s.client.Write(bp)
} fmt.Println("ok write")
s.megers = nil s.megers = nil
} }

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/smbrave/gosdk/util" "gitlab.batiao8.com/open/gosdk/util"
) )
var ( var (