diff --git a/cmd/huatuo-bamai/main.go b/cmd/huatuo-bamai/main.go index 4d6945d8..2eb15022 100644 --- a/cmd/huatuo-bamai/main.go +++ b/cmd/huatuo-bamai/main.go @@ -92,7 +92,7 @@ func mainAction(ctx *cli.Context) error { return err } - prom, err := InitMetricsCollector(blackListed) + prom, err := InitMetricsCollector(blackListed, conf.Region) if err != nil { return fmt.Errorf("InitMetricsCollector: %w", err) } diff --git a/cmd/huatuo-bamai/metric_init.go b/cmd/huatuo-bamai/metric_init.go index 4bef14b7..fd4841cf 100644 --- a/cmd/huatuo-bamai/metric_init.go +++ b/cmd/huatuo-bamai/metric_init.go @@ -26,8 +26,8 @@ import ( var promNamespace = "huatuo_bamai" // InitMetricsCollector creates a new MetricsCollector instance. -func InitMetricsCollector(blackListed []string) (*prometheus.Registry, error) { - nc, err := metric.NewCollectorManager(blackListed) +func InitMetricsCollector(blackListed []string, region string) (*prometheus.Registry, error) { + nc, err := metric.NewCollectorManager(blackListed, region) if err != nil { return nil, fmt.Errorf("create collector: %w", err) } diff --git a/pkg/metric/collector.go b/pkg/metric/collector.go index c92d1b47..7efa7af7 100644 --- a/pkg/metric/collector.go +++ b/pkg/metric/collector.go @@ -47,7 +47,7 @@ type CollectorManager struct { scrapeSuccessDesc *prometheus.Desc } -func NewCollectorManager(blackListed []string) (*CollectorManager, error) { +func NewCollectorManager(blackListed []string, region string) (*CollectorManager, error) { tracings, err := tracing.NewRegister(blackListed) if err != nil { return nil, err @@ -79,6 +79,8 @@ func NewCollectorManager(blackListed []string) (*CollectorManager, error) { ) hostname, _ := os.Hostname() + defaultRegion = region + defaultHostname = hostname return &CollectorManager{ collectors: collectors, diff --git a/pkg/metric/data.go b/pkg/metric/data.go index 42f91452..2c91ac36 100644 --- a/pkg/metric/data.go +++ b/pkg/metric/data.go @@ -17,7 +17,6 @@ package metric import ( "errors" "fmt" - "os" "sort" "sync" @@ -26,11 +25,11 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -var hostname string - -func init() { - hostname, _ = os.Hostname() -} +var ( + // FIXME If you use this package to other project. + defaultHostname string + defaultRegion string +) const ( // MetricTypeGauge indicates a gauge metric. @@ -40,6 +39,8 @@ const ( // LabelHost indicates the host. LabelHost = "Host" + // LabelRegion indicates the data collected from. + LabelRegion = "Region" // LabelContainerName indicates the container name. LabelContainerName = "ContainerName" // LabelContainerHost indicates the container host. @@ -94,8 +95,8 @@ func NewGaugeData(name string, value float64, help string, label map[string]stri help: help, } - data.labelKey = append(data.labelKey, LabelHost) - data.labelValue = append(data.labelValue, hostname) + data.labelKey = append(data.labelKey, LabelRegion, LabelHost) + data.labelValue = append(data.labelValue, defaultRegion, defaultHostname) // sort the labelKey selfLabelKeys := make([]string, 0, len(label)) @@ -127,6 +128,7 @@ func NewContainerGaugeData(container *pod.Container, name string, value float64, // default label data.labelKey = append(data.labelKey, + LabelRegion, LabelContainerHost, LabelContainerName, LabelContainerType, @@ -134,12 +136,13 @@ func NewContainerGaugeData(container *pod.Container, name string, value float64, LabelContainerHostNamespace, LabelHost) data.labelValue = append(data.labelValue, + defaultRegion, container.Hostname, container.Name, container.Type.String(), container.Qos.String(), container.LabelHostNamespace(), - hostname) + defaultHostname) // sort the labelKey selfLabelKeys := make([]string, 0, len(label))