metric: add region option support

Signed-off-by: Tonghao Zhang <tonghao@bamaicloud.com>
This commit is contained in:
Tonghao Zhang 2025-07-13 06:54:04 -04:00
parent fc9c20d24a
commit dc2fb43987
4 changed files with 18 additions and 13 deletions

View File

@ -92,7 +92,7 @@ func mainAction(ctx *cli.Context) error {
return err return err
} }
prom, err := InitMetricsCollector(blackListed) prom, err := InitMetricsCollector(blackListed, conf.Region)
if err != nil { if err != nil {
return fmt.Errorf("InitMetricsCollector: %w", err) return fmt.Errorf("InitMetricsCollector: %w", err)
} }

View File

@ -26,8 +26,8 @@ import (
var promNamespace = "huatuo_bamai" var promNamespace = "huatuo_bamai"
// InitMetricsCollector creates a new MetricsCollector instance. // InitMetricsCollector creates a new MetricsCollector instance.
func InitMetricsCollector(blackListed []string) (*prometheus.Registry, error) { func InitMetricsCollector(blackListed []string, region string) (*prometheus.Registry, error) {
nc, err := metric.NewCollectorManager(blackListed) nc, err := metric.NewCollectorManager(blackListed, region)
if err != nil { if err != nil {
return nil, fmt.Errorf("create collector: %w", err) return nil, fmt.Errorf("create collector: %w", err)
} }

View File

@ -47,7 +47,7 @@ type CollectorManager struct {
scrapeSuccessDesc *prometheus.Desc scrapeSuccessDesc *prometheus.Desc
} }
func NewCollectorManager(blackListed []string) (*CollectorManager, error) { func NewCollectorManager(blackListed []string, region string) (*CollectorManager, error) {
tracings, err := tracing.NewRegister(blackListed) tracings, err := tracing.NewRegister(blackListed)
if err != nil { if err != nil {
return nil, err return nil, err
@ -79,6 +79,8 @@ func NewCollectorManager(blackListed []string) (*CollectorManager, error) {
) )
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
defaultRegion = region
defaultHostname = hostname
return &CollectorManager{ return &CollectorManager{
collectors: collectors, collectors: collectors,

View File

@ -17,7 +17,6 @@ package metric
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"sort" "sort"
"sync" "sync"
@ -26,11 +25,11 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var hostname string var (
// FIXME If you use this package to other project.
func init() { defaultHostname string
hostname, _ = os.Hostname() defaultRegion string
} )
const ( const (
// MetricTypeGauge indicates a gauge metric. // MetricTypeGauge indicates a gauge metric.
@ -40,6 +39,8 @@ const (
// LabelHost indicates the host. // LabelHost indicates the host.
LabelHost = "Host" LabelHost = "Host"
// LabelRegion indicates the data collected from.
LabelRegion = "Region"
// LabelContainerName indicates the container name. // LabelContainerName indicates the container name.
LabelContainerName = "ContainerName" LabelContainerName = "ContainerName"
// LabelContainerHost indicates the container host. // LabelContainerHost indicates the container host.
@ -94,8 +95,8 @@ func NewGaugeData(name string, value float64, help string, label map[string]stri
help: help, help: help,
} }
data.labelKey = append(data.labelKey, LabelHost) data.labelKey = append(data.labelKey, LabelRegion, LabelHost)
data.labelValue = append(data.labelValue, hostname) data.labelValue = append(data.labelValue, defaultRegion, defaultHostname)
// sort the labelKey // sort the labelKey
selfLabelKeys := make([]string, 0, len(label)) selfLabelKeys := make([]string, 0, len(label))
@ -127,6 +128,7 @@ func NewContainerGaugeData(container *pod.Container, name string, value float64,
// default label // default label
data.labelKey = append(data.labelKey, data.labelKey = append(data.labelKey,
LabelRegion,
LabelContainerHost, LabelContainerHost,
LabelContainerName, LabelContainerName,
LabelContainerType, LabelContainerType,
@ -134,12 +136,13 @@ func NewContainerGaugeData(container *pod.Container, name string, value float64,
LabelContainerHostNamespace, LabelContainerHostNamespace,
LabelHost) LabelHost)
data.labelValue = append(data.labelValue, data.labelValue = append(data.labelValue,
defaultRegion,
container.Hostname, container.Hostname,
container.Name, container.Name,
container.Type.String(), container.Type.String(),
container.Qos.String(), container.Qos.String(),
container.LabelHostNamespace(), container.LabelHostNamespace(),
hostname) defaultHostname)
// sort the labelKey // sort the labelKey
selfLabelKeys := make([]string, 0, len(label)) selfLabelKeys := make([]string, 0, len(label))