Skip to content

Commit

Permalink
OM-184 - support pod-name/ server-ip
Browse files Browse the repository at this point in the history
added support to use server-ip or pod-name/host-name according to the environment mode running
in kubernetes return host-name/pod-name as service label-value
in vm / docker return ipaddress as service label-value
  • Loading branch information
mphanias committed Apr 2, 2024
1 parent c86f645 commit 95a493d
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 15 deletions.
26 changes: 26 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Config struct {
OtelPushInterval uint8 `toml:"push_interval"`
OtelServerStatFetchInterval uint8 `toml:"server_stat_fetch_interval"`
} `toml:"OpenTelemetry"`

IsKubernetes bool
HostName string
} `toml:"Agent"`

Aerospike struct {
Expand Down Expand Up @@ -200,6 +203,27 @@ func (c *Config) FetchCloudInfo(md toml.MetaData) {
}
}

func (c *Config) FetchKubernetesInfo(md toml.MetaData) {
// envKubeConfig := os.Getenv("KUBECONFIG")
envKubeServiceHost := os.Getenv("KUBERNETES_SERVICE_HOST")
envKubeServicePort := os.Getenv("KUBERNETES_SERVICE_PORT")

log.Info("Checking is Running in Kubernetes environment ? - Kubernetes Host: ", envKubeServiceHost, " and Kubernetes Port: ", envKubeServicePort)
Cfg.Agent.IsKubernetes = false
if envKubeServiceHost != "" && len(strings.TrimSpace(envKubeServiceHost)) > 0 {
Cfg.Agent.IsKubernetes = true

// get host-name
var err error
Cfg.Agent.HostName, err = os.Hostname()
if err != nil {
log.Errorln(err)
return
}

}
}

// Initialize exporter configuration
func InitConfig(configFile string) {
// to print everything out regarding reading the config in app init
Expand All @@ -225,6 +249,8 @@ func InitConfig(configFile string) {

Cfg.ValidateAndUpdate(md)
Cfg.FetchCloudInfo(md)

Cfg.FetchKubernetesInfo(md)
}

// Set log file path
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/executors/otel_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ func sendNodeUp(meter metric.Meter, ctx context.Context, commonLabels []attribut
metric.WithDescription("Aerospike node active status"),
)

service := statprocessors.Service
if config.Cfg.Agent.IsKubernetes {
statprocessors.Service = config.Cfg.Agent.HostName
service = config.Cfg.Agent.HostName
}

labels := []attribute.KeyValue{
attribute.String("cluster_name", statprocessors.ClusterName),
attribute.String("service", statprocessors.Service),
attribute.String("service", service),
attribute.String("build", statprocessors.Build),
}

Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/executors/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ func (o *PrometheusImpl) Collect(ch chan<- prometheus.Metric) {
return
}

ch <- prometheus.MustNewConstMetric(nodeActiveDesc, prometheus.GaugeValue, 1.0, statprocessors.ClusterName, statprocessors.Service, statprocessors.Build)
// if kubernetes then send host-name/pod-name else send server-ip as-isnh
service := statprocessors.Service
if config.Cfg.Agent.IsKubernetes {
service = config.Cfg.Agent.HostName
statprocessors.Service = config.Cfg.Agent.HostName
}
ch <- prometheus.MustNewConstMetric(nodeActiveDesc, prometheus.GaugeValue, 1.0, statprocessors.ClusterName, service, statprocessors.Build)

for _, wm := range refreshed_metrics {
PushToPrometheus(wm, ch)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/statprocessors/sp_latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

commons "github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/commons"
"github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/config"
config "github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/config"

log "github.com/sirupsen/logrus"
)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/statprocessors/sp_namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (nw *NamespaceStatsProcessor) refreshIndexPressure(singleInfoKey string, in
nsName := values[0]

labels := []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_NS}
labelValues := []string{rawMetrics[Infokey_ClusterName], rawMetrics[Infokey_Service], nsName}
labelValues := []string{ClusterName, Service, nsName}

// Server index-pressure output: test:0:0;bar_device:0:0;materials:0:0
// ignore first element - namespace
Expand Down Expand Up @@ -182,7 +182,7 @@ func (nw *NamespaceStatsProcessor) refreshNamespaceStats(singleInfoKey string, i
// default: aerospike_namespace_<stat-name>
constructedStatname = stat
labels = []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_NS}
labelValues = []string{rawMetrics[Infokey_ClusterName], rawMetrics[Infokey_Service], nsName}
labelValues = []string{ClusterName, Service, nsName}

if isArrayType {
constructedStatname, labels, labelValues = nw.handleArrayStats(nsName, stat, pv, stats, deviceType, rawMetrics)
Expand Down Expand Up @@ -287,7 +287,7 @@ func (nw *NamespaceStatsProcessor) handleArrayStats(nsName string, statToProcess
compositeStatName := deviceType + "_" + statType + "_" + statName
deviceOrFileName := allNamespaceStats[deviceType+"."+statType+"["+statIndex+"]"]
labels := []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_NS, statType + "_index", statType}
labelValues := []string{rawMetrics[Infokey_ClusterName], rawMetrics[Infokey_Service], nsName, statIndex, deviceOrFileName}
labelValues := []string{ClusterName, Service, nsName, statIndex, deviceOrFileName}

return compositeStatName, labels, labelValues

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/statprocessors/sp_node_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (sw *NodeStatsProcessor) Refresh(infoKeys []string, rawMetrics map[string]s
log.Tracef("node-configs:%s", nodeConfigs)
log.Tracef("node-stats:%s", nodeStats)

clusterName := rawMetrics[Infokey_ClusterName]
service := rawMetrics[Infokey_Service]
clusterName := ClusterName
service := Service

// we are sending configs and stats in same refresh call, as both are being sent to prom, instead of doing prom-push in 2 functions
// handle configs
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/statprocessors/sp_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (sw *SetsStatsProcessor) Refresh(infoKeys []string, rawMetrics map[string]s
var allMetricsToSend = []AerospikeStat{}

for i := range setStats {
clusterName := rawMetrics[Infokey_ClusterName]
service := rawMetrics[Infokey_Service]
clusterName := ClusterName
service := Service

stats := commons.ParseStats(setStats[i], ":")
for stat, value := range stats {
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/statprocessors/sp_sindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (siw *SindexStatsProcessor) Refresh(infoKeys []string, rawMetrics map[strin
sindexName := sindexInfoKeySplit[1]
log.Tracef("sindex-stats:%s:%s:%s", nsName, sindexName, rawMetrics[sindex])

clusterName := rawMetrics[Infokey_ClusterName]
service := rawMetrics[Infokey_Service]
clusterName := ClusterName
service := Service

stats := commons.ParseStats(rawMetrics[sindex], ";")
for stat, value := range stats {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/statprocessors/sp_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (uw *UserStatsProcessor) refreshUserStats(infoKeys []string, rawMetrics map

func internalCreateLocalAerospikeStat(rawMetrics map[string]string, pStatName string, username string) (AerospikeStat, []string, []string) {
labels := []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_USER}
labelValues := []string{rawMetrics[Infokey_ClusterName], rawMetrics[Infokey_Service], username}
labelValues := []string{ClusterName, Service, username}
allowed := isMetricAllowed(commons.CTX_USERS, pStatName)
asMetric := NewAerospikeStat(commons.CTX_USERS, pStatName, allowed)

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/statprocessors/sp_xdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ func (xw *XdrStatsProcessor) handleRefresh(infoKeyToProcess string, xdrRawMetric
}

labels := []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_DC_NAME}
labelValues := []string{clusterName, service, dcName}
labelValues := []string{ClusterName, Service, dcName}

// if namespace exists, add it to the label and label-values array
if len(ns) > 0 {
labels = []string{commons.METRIC_LABEL_CLUSTER_NAME, commons.METRIC_LABEL_SERVICE, commons.METRIC_LABEL_DC_NAME, commons.METRIC_LABEL_NS}
labelValues = []string{clusterName, service, dcName, ns}
labelValues = []string{ClusterName, Service, dcName, ns}
}

// pushToPrometheus(asMetric, pv, labels, labelsValues, ch)
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/statprocessors/statsrefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package statprocessors

import (
commons "github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/commons"
"github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/config"
"github.com/aerospike/aerospike-prometheus-exporter/internal/pkg/dataprovider"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -68,6 +69,9 @@ func Refresh() ([]AerospikeStat, error) {

// set global values
ClusterName, Service, Build = rawMetrics[Infokey_ClusterName], rawMetrics[Infokey_Service], rawMetrics[Infokey_Build]
if config.Cfg.Agent.IsKubernetes {
Service = config.Cfg.Agent.HostName
}

// sanitize the utf8 strings before sending them to watchers
for k, v := range rawMetrics {
Expand Down

0 comments on commit 95a493d

Please sign in to comment.