Merge pull request 'add mutex for prom transport' (#3) from main into master

This commit is contained in:
UlricQin 2022-05-22 12:46:25 +08:00
commit bd55c575a0
5 changed files with 52 additions and 51 deletions

View File

@ -2,7 +2,7 @@
NOW = $(shell date -u '+%Y%m%d%I%M%S')
RELEASE_VERSION = 5.8.0
RELEASE_VERSION = 5.8.1
APP = n9e
SERVER_BIN = $(APP)

View File

@ -10,6 +10,12 @@ MetricsYamlFile = "./etc/metrics.yaml"
BuiltinAlertsDir = "./etc/alerts"
BuiltinDashboardsDir = "./etc/dashboards"
# config | api
ClustersFrom = "config"
# using when ClustersFrom = "api"
ClustersFromAPIs = []
[[NotifyChannels]]
Label = "邮箱"
# do not change Key
@ -172,14 +178,7 @@ BasicAuthUser = ""
BasicAuthPass = ""
# timeout settings, unit: ms
Timeout = 30000
DialTimeout = 10000
TLSHandshakeTimeout = 30000
ExpectContinueTimeout = 1000
IdleConnTimeout = 90000
# time duration, unit: ms
KeepAlive = 30000
MaxConnsPerHost = 0
MaxIdleConns = 100
DialTimeout = 3000
MaxIdleConnsPerHost = 100
[Ibex]

View File

@ -15,7 +15,6 @@ import (
"github.com/didi/nightingale/v5/src/pkg/oidcc"
"github.com/didi/nightingale/v5/src/pkg/ormx"
"github.com/didi/nightingale/v5/src/storage"
"github.com/didi/nightingale/v5/src/webapi/prom"
)
var (
@ -82,6 +81,8 @@ type Config struct {
MetricsYamlFile string
BuiltinAlertsDir string
BuiltinDashboardsDir string
ClustersFrom string
ClustersFromAPIs []string
ContactKeys []LabelAndKey
NotifyChannels []LabelAndKey
Log logx.Config
@ -91,12 +92,26 @@ type Config struct {
AnonymousAccess AnonymousAccess
LDAP ldapx.LdapSection
Redis storage.RedisConfig
DB ormx.DBConfig
Clusters []prom.Options
DB ormx.DBConfig
Clusters []ClusterOptions
Ibex Ibex
OIDC oidcc.Config
}
type ClusterOptions struct {
Name string
Prom string
BasicAuthUser string
BasicAuthPass string
Timeout int64
DialTimeout int64
KeepAlive int64
MaxIdleConnsPerHost int
}
type LabelAndKey struct {
Label string `json:"label"`
Key string `json:"key"`

View File

@ -3,55 +3,48 @@ package prom
import (
"net"
"net/http"
"sync"
"time"
"github.com/didi/nightingale/v5/src/webapi/config"
)
type Options struct {
Name string
Prom string
BasicAuthUser string
BasicAuthPass string
Timeout int64
DialTimeout int64
TLSHandshakeTimeout int64
ExpectContinueTimeout int64
IdleConnTimeout int64
KeepAlive int64
MaxConnsPerHost int
MaxIdleConns int
MaxIdleConnsPerHost int
}
type ClusterType struct {
Opts Options
Opts config.ClusterOptions
Transport *http.Transport
}
type ClustersType struct {
M map[string]ClusterType
}
func NewClusters() ClustersType {
return ClustersType{
M: make(map[string]ClusterType),
}
datas map[string]ClusterType
mutex *sync.RWMutex
}
func (cs *ClustersType) Put(name string, cluster ClusterType) {
cs.M[name] = cluster
cs.mutex.Lock()
cs.datas[name] = cluster
cs.mutex.Unlock()
}
func (cs *ClustersType) Get(name string) (ClusterType, bool) {
c, has := cs.M[name]
cs.mutex.RLock()
defer cs.mutex.RUnlock()
c, has := cs.datas[name]
return c, has
}
var Clusters = NewClusters()
var Clusters = ClustersType{
datas: make(map[string]ClusterType),
mutex: new(sync.RWMutex),
}
func Init() error {
if config.C.ClustersFrom != "" && config.C.ClustersFrom != "config" {
return nil
}
opts := config.C.Clusters
func Init(opts []Options) error {
for i := 0; i < len(opts); i++ {
cluster := ClusterType{
Opts: opts[i],
@ -59,16 +52,10 @@ func Init(opts []Options) error {
// TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond,
KeepAlive: time.Duration(opts[i].KeepAlive) * time.Millisecond,
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond,
}).DialContext,
ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond,
TLSHandshakeTimeout: time.Duration(opts[i].TLSHandshakeTimeout) * time.Millisecond,
ExpectContinueTimeout: time.Duration(opts[i].ExpectContinueTimeout) * time.Millisecond,
MaxConnsPerHost: opts[i].MaxConnsPerHost,
MaxIdleConns: opts[i].MaxIdleConns,
MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(opts[i].IdleConnTimeout) * time.Millisecond,
},
}
Clusters.Put(opts[i].Name, cluster)

View File

@ -115,7 +115,7 @@ func (a Webapi) initialize() (func(), error) {
models.InitRoot()
// init prometheus proxy config
if err = prom.Init(config.C.Clusters); err != nil {
if err = prom.Init(); err != nil {
return nil, err
}