-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query batch feature #1052
Merged
Merged
Query batch feature #1052
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7b43b93
Merge pull request #1 from ccfos/main
SunnyBoy-WYH f7c4259
batch query prom for single panel
SunnyBoy-WYH 0d36966
Merge branch 'ccfos:main' into main
SunnyBoy-WYH c34024e
make code better:
SunnyBoy-WYH cd91bf5
Merge remote-tracking branch 'origin/main' into main
SunnyBoy-WYH 8be48bd
clear code
SunnyBoy-WYH b9f6144
clear code
SunnyBoy-WYH 6cecdfe
format code
SunnyBoy-WYH f86b9a2
move reader.go,reuse webapi/prom/prom.go clusterTypes clients cache
SunnyBoy-WYH e49fffb
clear code,extract common method
SunnyBoy-WYH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package prom | ||
|
||
type ClientOptions struct { | ||
BasicAuthUser string | ||
|
||
BasicAuthPass string | ||
|
||
Headers []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,17 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"github.com/didi/nightingale/v5/src/pkg/prom" | ||
"github.com/didi/nightingale/v5/src/webapi/config" | ||
"github.com/prometheus/client_golang/api" | ||
"github.com/toolkits/pkg/logger" | ||
"github.com/toolkits/pkg/net/httplib" | ||
) | ||
|
||
type ClusterType struct { | ||
Opts config.ClusterOptions | ||
Transport *http.Transport | ||
Opts config.ClusterOptions | ||
Transport *http.Transport | ||
PromClient prom.API | ||
} | ||
|
||
type ClustersType struct { | ||
|
@@ -39,6 +42,19 @@ func (cs *ClustersType) Get(name string) (*ClusterType, bool) { | |
return c, has | ||
} | ||
|
||
func (cs *ClustersType) GetClusters() []*ClusterType { | ||
cs.mutex.Lock() | ||
clusterTypes := make([]*ClusterType, 0, len(cs.datas)) | ||
for k := range cs.datas { | ||
c, has := cs.datas[k] | ||
if has { | ||
clusterTypes = append(clusterTypes, c) | ||
} | ||
} | ||
cs.mutex.Unlock() | ||
return clusterTypes | ||
} | ||
|
||
var Clusters = ClustersType{ | ||
datas: make(map[string]*ClusterType), | ||
mutex: new(sync.RWMutex), | ||
|
@@ -61,17 +77,34 @@ func initClustersFromConfig() error { | |
opts := config.C.Clusters | ||
|
||
for i := 0; i < len(opts); i++ { | ||
transport := &http.Transport{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initClustersFromConfig 和 initClustersFromAPI 中有大片代码重复,看看是否可以抽取出方法? |
||
// TLSClientConfig: tlsConfig, | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond, | ||
}).DialContext, | ||
ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond, | ||
MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost, | ||
} | ||
|
||
cli, err := api.NewClient(api.Config{ | ||
Address: opts[i].Prom, | ||
RoundTripper: transport, | ||
}) | ||
|
||
if err != nil { | ||
logger.Errorf("new client fail: %v", err) | ||
continue | ||
} | ||
|
||
cluster := &ClusterType{ | ||
Opts: opts[i], | ||
Transport: &http.Transport{ | ||
// TLSClientConfig: tlsConfig, | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond, | ||
}).DialContext, | ||
ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond, | ||
MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost, | ||
}, | ||
Opts: opts[i], | ||
Transport: transport, | ||
PromClient: prom.NewAPI(cli, prom.ClientOptions{ | ||
BasicAuthUser: opts[i].BasicAuthUser, | ||
BasicAuthPass: opts[i].BasicAuthPass, | ||
Headers: opts[i].Headers, | ||
}), | ||
} | ||
Clusters.Put(opts[i].Name, cluster) | ||
} | ||
|
@@ -173,17 +206,34 @@ func loadClustersFromAPI() { | |
MaxIdleConnsPerHost: 32, | ||
} | ||
|
||
transport := &http.Transport{ | ||
// TLSClientConfig: tlsConfig, | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: time.Duration(opt.DialTimeout) * time.Millisecond, | ||
}).DialContext, | ||
ResponseHeaderTimeout: time.Duration(opt.Timeout) * time.Millisecond, | ||
MaxIdleConnsPerHost: opt.MaxIdleConnsPerHost, | ||
} | ||
|
||
cli, err := api.NewClient(api.Config{ | ||
Address: opt.Prom, | ||
RoundTripper: transport, | ||
}) | ||
|
||
if err != nil { | ||
logger.Errorf("new client fail: %v", err) | ||
continue | ||
} | ||
|
||
cluster := &ClusterType{ | ||
Opts: opt, | ||
Transport: &http.Transport{ | ||
// TLSClientConfig: tlsConfig, | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: time.Duration(opt.DialTimeout) * time.Millisecond, | ||
}).DialContext, | ||
ResponseHeaderTimeout: time.Duration(opt.Timeout) * time.Millisecond, | ||
MaxIdleConnsPerHost: opt.MaxIdleConnsPerHost, | ||
}, | ||
Opts: opt, | ||
Transport: transport, | ||
PromClient: prom.NewAPI(cli, prom.ClientOptions{ | ||
BasicAuthUser: opt.BasicAuthUser, | ||
BasicAuthPass: opt.BasicAuthPass, | ||
Headers: opt.Headers, | ||
}), | ||
} | ||
|
||
Clusters.Put(item.Name, cluster) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,10 @@ func (a Webapi) initialize() (func(), error) { | |
if err = prom.Init(); err != nil { | ||
return nil, err | ||
} | ||
// init reader clients | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重复了,上面已经有了prom.Init |
||
if err = prom.Init(); err != nil { | ||
return nil, err | ||
} | ||
|
||
stat.Init() | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个方法是不是已经不需要了?