Skip to content

Commit

Permalink
支持对数据源URL替换
Browse files Browse the repository at this point in the history
(cherry picked from commit c99f714acc28a13bf0c18e33d974ef2785cd379d)
  • Loading branch information
MicroOps-cn committed Mar 28, 2022
1 parent ad4dcf5 commit 7533645
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
17 changes: 15 additions & 2 deletions collector/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ type CollectContext struct {
*CollectConfig
cancelFunc context.CancelFunc
context.Context
DatasourceName string
DatasourceUrl string
}

func (c *CollectContext) Describe(_ chan<- *prometheus.Desc) {
Expand All @@ -269,11 +271,21 @@ func (c *CollectContext) Collect(proMetrics chan<- prometheus.Metric) {
go func() {
wg := sync.WaitGroup{}
for i := range c.Datasource {
if c.Datasource[i].ReadMode != Stream {
ds := *c.Datasource[i]
if len(c.DatasourceName) != 0 {
if c.DatasourceName != c.Datasource[i].Name {
continue
} else if ds.AllowReplace {
if len(c.DatasourceUrl) != 0 {
ds.Url = c.DatasourceUrl
}
}
}
if ds.ReadMode != Stream {
wg.Add(1)
go func(idx int) {
defer wg.Done()
c.GetMetricByDs(c.Context, c.logger, c.Datasource[idx], metrics)
c.GetMetricByDs(c.Context, c.logger, &ds, metrics)
}(i)
}
}
Expand All @@ -296,6 +308,7 @@ type Collects []CollectConfig

func (c Collects) Get(name string) *CollectConfig {
for idx := range c {
fmt.Println(c[idx].Name, name)
if c[idx].Name == name {
return &c[idx]
}
Expand Down
1 change: 1 addition & 0 deletions collector/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type Streamer interface {
type Datasource struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
AllowReplace bool `yaml:"allow_replace"`
Type DatasourceType `yaml:"type"`
Timeout time.Duration `yaml:"timeout"`
RelabelConfigs RelabelConfigs `yaml:"relabel_configs"`
Expand Down
3 changes: 3 additions & 0 deletions examples/data_exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ collects:
datasource:
- type: "http"
url: "http://127.0.0.1:10101/my_data.json"
name: "simple-http-data"
# 覆盖URL http://microops:9116/test-http/metrics?datasource=simple-http-data&url=http://127.0.0.1:10101/my_data2.json
allow_replace: true
metrics:
- name: "Point1"
relabel_configs:
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,18 @@ func collectMetricsByName(logger log.Logger, name string, w http.ResponseWriter,
level.Debug(logger).Log("msg", "collect metrics by collect_name", "name", name)
conf := sc.GetConfig()
reg := prometheus.NewRegistry()

if collect := conf.Collects.Get(name); collect != nil {
reg.MustRegister(&collector.CollectContext{
collectCtx := &collector.CollectContext{
CollectConfig: collect,
Context: r.Context(),
})
}
if r.URL.Query().Has("datasource") {
collectCtx.DatasourceName = r.URL.Query().Get("datasource")
if r.URL.Query().Has("url") {
collectCtx.DatasourceUrl = r.URL.Query().Get("url")
}
}
reg.MustRegister(collectCtx)
} else {
http.NotFound(w, r)
return
Expand Down

0 comments on commit 7533645

Please sign in to comment.