Skip to content

Commit

Permalink
Merge pull request #288 from jzwlqx/feature/remove-legacy-metrics-label
Browse files Browse the repository at this point in the history
deprecate legacy metrics labels
  • Loading branch information
BSWANG authored May 31, 2024
2 parents 903a9b9 + 8b80731 commit 4b262ee
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 34 deletions.
40 changes: 10 additions & 30 deletions pkg/exporter/probe/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
log "github.com/sirupsen/logrus"
)

var legacyMetricsLabels = []string{"target_node", "target_namespace", "target_pod", "node", "namespace", "pod"}
var StandardMetricsLabels = []string{"k8s_node", "k8s_namespace", "k8s_pod"}
var TupleMetricsLabels = []string{"protocol", "src", "src_type", "src_node", "src_namespace", "src_pod", "dst", "dst_type", "dst_node", "dst_namespace", "dst_pod", "sport", "dport"}
var AdditionalLabelValueExpr []string
Expand Down Expand Up @@ -48,7 +47,7 @@ func BuildAdditionalLabelsValues(podLabels map[string]string) []string {
lastIndex := 0

for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
var groups []string
for i := 0; i < len(v); i += 2 {
groups = append(groups, str[v[i]:v[i+1]])
}
Expand All @@ -74,48 +73,34 @@ func BuildAdditionalLabelsValues(podLabels map[string]string) []string {
}

type legacyBatchMetrics struct {
module string
collector LegacyCollector
descs map[string]*prometheus.Desc
underscore bool
module string
collector LegacyCollector
descs map[string]*prometheus.Desc
}

func legacyMetricsName(module string, name string, underscore bool) string {
if underscore {
return fmt.Sprintf("%s_pod_%s_%s", LegacyMetricsNamespace, module, name)
}
return fmt.Sprintf("%s_pod_%s%s", LegacyMetricsNamespace, module, name)
}
func newMetricsName(module, name string) string {
return prometheus.BuildFQName(MetricsNamespace, module, name)
}

type LegacyCollector func() (map[string]map[uint32]uint64, error)

func NewLegacyBatchMetrics(module string, metrics []string, collector LegacyCollector) prometheus.Collector {
return newLegacyBatchMetrics(module, false, metrics, collector)
return newLegacyBatchMetrics(module, metrics, collector)
}

func newLegacyBatchMetrics(module string, underscore bool, metrics []string, collector LegacyCollector) prometheus.Collector {
func newLegacyBatchMetrics(module string, metrics []string, collector LegacyCollector) prometheus.Collector {
descs := make(map[string]*prometheus.Desc)
for _, m := range metrics {
legacyName := legacyMetricsName(module, m, underscore)
newName := newMetricsName(module, m)
descs[legacyName] = prometheus.NewDesc(legacyName, "", legacyMetricsLabels, nil)
descs[newName] = prometheus.NewDesc(newName, "", StandardMetricsLabels, nil)
}
return &legacyBatchMetrics{
module: module,
collector: collector,
descs: descs,
underscore: underscore,
module: module,
collector: collector,
descs: descs,
}
}

func NewLegacyBatchMetricsWithUnderscore(module string, metrics []string, collector LegacyCollector) prometheus.Collector {
return newLegacyBatchMetrics(module, true, metrics, collector)
}

func (l *legacyBatchMetrics) Describe(descs chan<- *prometheus.Desc) {
for _, desc := range l.descs {
descs <- desc
Expand Down Expand Up @@ -146,17 +131,12 @@ func (l *legacyBatchMetrics) Collect(metrics chan<- prometheus.Metric) {
continue
}
labelValues := BuildStandardMetricsLabelValues(et)
// for legacy pod labels
emit(newMetricsName(l.module, key), labelValues, float64(value))

var metaPodData = []string{nettop.GetNodeName(), et.GetPodNamespace(), et.GetPodName()}
labelValues = append(metaPodData, metaPodData...)
emit(legacyMetricsName(l.module, key, l.underscore), labelValues, float64(value))
}
}
}

func LagacyEventLabels(netns uint32) []Label {
func LegacyEventLabels(netns uint32) []Label {
et, err := nettop.GetEntityByNetns(int(netns))
if err != nil || et == nil {
log.Infof("nettop get entity failed, netns: %d, err: %v", netns, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/probe/tracekernel/tracekernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (p *kernelLatencyProbe) perfLoop() {
netns := event.SkbMeta.Netns
evt := &probe.Event{
Timestamp: time.Now().UnixNano(),
Labels: probe.LagacyEventLabels(netns),
Labels: probe.LegacyEventLabels(netns),
}
/*
#define RX_KLATENCY 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (p *netifTxlatencyProbe) perfLoop() {

evt := &probe.Event{
Timestamp: time.Now().UnixNano(),
Labels: probe.LagacyEventLabels(event.SkbMeta.Netns),
Labels: probe.LegacyEventLabels(event.SkbMeta.Netns),
}
tuple := fmt.Sprintf("protocol=%s saddr=%s sport=%d daddr=%s dport=%d ", bpfutil.GetProtoStr(event.Tuple.L4Proto), bpfutil.GetAddrStr(event.Tuple.L3Proto, *(*[16]byte)(unsafe.Pointer(&event.Tuple.Saddr))), bits.ReverseBytes16(event.Tuple.Sport), bpfutil.GetAddrStr(event.Tuple.L3Proto, *(*[16]byte)(unsafe.Pointer(&event.Tuple.Daddr))), bits.ReverseBytes16(event.Tuple.Dport))
evt.Message = fmt.Sprintf("%s latency:%s", tuple, bpfutil.GetHumanTimes(event.Latency))
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/probe/tracesocketlatency/socketlatency.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (p *socketLatencyProbe) perfLoop() {
}
evt := &probe.Event{
Timestamp: time.Now().UnixNano(),
Labels: probe.LagacyEventLabels(event.SkbMeta.Netns),
Labels: probe.LegacyEventLabels(event.SkbMeta.Netns),
}
/*
#define ACTION_READ 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/probe/tracetcpreset/tracetcpreset.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *tcpResetProbe) perfLoop() {
evt := &probe.Event{
Timestamp: time.Now().UnixNano(),
Type: eventType,
Labels: probe.LagacyEventLabels(event.SkbMeta.Netns),
Labels: probe.LegacyEventLabels(event.SkbMeta.Netns),
}

tuple := fmt.Sprintf("protocol=%s saddr=%s sport=%d daddr=%s dport=%d ", bpfutil.GetProtoStr(event.Tuple.L4Proto), bpfutil.GetAddrStr(event.Tuple.L3Proto, *(*[16]byte)(unsafe.Pointer(&event.Tuple.Saddr))), bits.ReverseBytes16(event.Tuple.Sport), bpfutil.GetAddrStr(event.Tuple.L3Proto, *(*[16]byte)(unsafe.Pointer(&event.Tuple.Daddr))), bits.ReverseBytes16(event.Tuple.Dport))
Expand Down

0 comments on commit 4b262ee

Please sign in to comment.