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
}
prom, err := InitMetricsCollector(blackListed)
prom, err := InitMetricsCollector(blackListed, conf.Region)
if err != nil {
return fmt.Errorf("InitMetricsCollector: %w", err)
}

View File

@ -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)
}

View File

@ -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,

View File

@ -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))