Skip to content

Commit

Permalink
refactor datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Jun 25, 2023
1 parent 97c6836 commit 16db570
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions prom/reader.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package prom

import (
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"time"

"github.com/ccfos/nightingale/v6/alert/aconf"
Expand Down Expand Up @@ -38,10 +38,6 @@ func (pc *PromClientMap) InitReader() error {
return nil
}

type PromSetting struct {
WriterAddr string `json:"write_addr"`
}

func (pc *PromClientMap) loadFromDatabase() {
var datasources []*models.Datasource
var err error
Expand Down Expand Up @@ -71,19 +67,20 @@ func (pc *PromClientMap) loadFromDatabase() {
header = append(header, v)
}

var promSetting PromSetting
if ds.Settings != "" {
err := json.Unmarshal([]byte(ds.Settings), &promSetting)
if err != nil {
logger.Errorf("failed to unmarshal prom settings, error: %v", err)
continue
var writeAddr string
var internalAddr string
for k, v := range ds.SettingsJson {
if strings.Contains(k, "write_addr") {
writeAddr = v.(string)
} else if strings.Contains(k, "internal_addr") && v.(string) != "" {
internalAddr = v.(string)
}
}

po := PromOption{
ClusterName: ds.Name,
Url: ds.HTTPJson.Url,
WriteAddr: promSetting.WriterAddr,
WriteAddr: writeAddr,
BasicAuthUser: ds.AuthJson.BasicAuthUser,
BasicAuthPass: ds.AuthJson.BasicAuthPassword,
Timeout: ds.HTTPJson.Timeout,
Expand All @@ -92,6 +89,11 @@ func (pc *PromClientMap) loadFromDatabase() {
Headers: header,
}

if internalAddr != "" && !pc.ctx.IsCenter {
// internal addr is set, use internal addr when edge mode
po.Url = internalAddr
}

newCluster[dsId] = struct{}{}
if pc.IsNil(dsId) {
// first time
Expand Down

0 comments on commit 16db570

Please sign in to comment.