Skip to content

Commit

Permalink
feat: operator 支持标准化指标名 --story=118330168 (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx authored Jul 4, 2024
1 parent 151daf4 commit aa0d810
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/bkmonitorbeat/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v3.39.x
v3.40.x
1 change: 1 addition & 0 deletions pkg/bkmonitorbeat/dockerfile/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM centos:7

VOLUME /data
RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tencent.com/repo/centos7_base.repo
RUN yum install -y iproute strace tcpdump

RUN mkdir -p /data/bkmonitorbeat/config/child_configs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"unicode"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
Expand Down Expand Up @@ -72,6 +74,7 @@ type MetricSet struct {
remoteClient *http.Client
workers int
disableCustomTimestamp bool
normalizeMetricName bool
remoteRelabelCache []*relabel.Config
MetricRelabelRemote string
MetricRelabelConfigs []*relabel.Config
Expand All @@ -90,6 +93,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
TempFilePattern string `config:"temp_file_pattern"`
Workers int `config:"workers"`
DisableCustomTimestamp bool `config:"disable_custom_timestamp"`
NormalizeMetricName bool `config:"normalize_metric_name"`
}{}

if err := base.Module().UnpackConfig(&config); err != nil {
Expand Down Expand Up @@ -149,6 +153,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
DimensionReplace: config.DimensionReplace,
MetricRelabelConfigs: relabels,
disableCustomTimestamp: config.DisableCustomTimestamp,
normalizeMetricName: config.NormalizeMetricName,
workers: config.Workers,
}, nil
}
Expand Down Expand Up @@ -325,6 +330,10 @@ func (m *MetricSet) getEventsFromReader(metricsReader io.ReadCloser, cleanup fun
return eventChan
}

func normalizeName(s string) string {
return strings.Join(strings.FieldsFunc(s, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' }), "_")
}

func (m *MetricSet) produceEvents(line string, timestamp int64) ([]common.MapStr, *diffKey, error) {
if len(line) <= 0 || line[0] == '#' {
return nil, nil, nil
Expand All @@ -340,6 +349,10 @@ func (m *MetricSet) produceEvents(line string, timestamp int64) ([]common.MapStr
return nil, nil, upErr
}

if m.normalizeMetricName {
promEvent.Key = normalizeName(promEvent.Key)
}

// 生成事件
var events []common.MapStr
event, dk := m.getEventFromPromEvent(&promEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ func TestGetEventFromPromEvent(t *testing.T) {
}

mb := &MetricSet{
deltaKeys: deltaKeys,
lastDeltaMetrics: lastDeltaMetrics,
deltaKeys: deltaKeys,
lastDeltaMetrics: lastDeltaMetrics,
normalizeMetricName: true,
}

lines1 := `
metric1{label1="value1"} 10
metric2{label1="value2"} 11
metric3{label1="value3"} 12
metric3:foo:bar{label1="value3"} 12
`
ch := mb.getEventsFromReader(io.NopCloser(bytes.NewBufferString(lines1)), func() {}, true)
expected := []common.MapStr{
Expand All @@ -55,16 +56,16 @@ metric3{label1="value3"} 12
},
},
{
"key": "metric3",
"key": "metric3_foo_bar",
"labels": common.MapStr{
"label1": "value3",
},
"value": float64(12),
},
{
"key": "bkm_gather_up",
"key": "bkm_metricbeat_endpoint_up",
"labels": common.MapStr{
"bkm_up_code": "0",
"code": "0",
},
"value": float64(1),
},
Expand Down Expand Up @@ -109,9 +110,9 @@ metric4label1"value3"} 22
"value": float64(22),
},
{
"key": "bkm_gather_up",
"key": "bkm_metricbeat_endpoint_up",
"labels": common.MapStr{
"bkm_up_code": "2502",
"code": "2502",
},
"value": float64(1),
},
Expand All @@ -129,7 +130,6 @@ metric4label1"value3"} 22
failedMetric := newFailReader(define.BeatMetricBeatConnOuterError)
ch = mb.getEventsFromReader(failedMetric, func() {}, false)
for msg := range ch {
fmt.Printf("aaa: %v", msg)
fmt.Printf("msg: %v", msg)
}

}
5 changes: 3 additions & 2 deletions pkg/collector/dockerfile/collector/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM centos:7

VOLUME /data
#RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
#RUN yum install -y iproute strace tcpdump
RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tencent.com/repo/centos7_base.repo
RUN yum install -y iproute strace tcpdump

RUN mkdir -p /data/hostid && mkdir -p /data/pid && mkdir -p /data/logs && mkdir -p /data/data
RUN mkdir -p /data/collector
Expand Down
5 changes: 3 additions & 2 deletions pkg/collector/dockerfile/sidecar/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM centos:7

VOLUME /data
#RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
#RUN yum install -y iproute strace tcpdump
RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tencent.com/repo/centos7_base.repo
RUN yum install -y iproute strace tcpdump

RUN mkdir -p /data/sidecar
COPY sidecar /data/sidecar/sidecar
Expand Down
9 changes: 9 additions & 0 deletions pkg/collector/internal/testkits/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func AssertAttrsFoundStringVal(t *testing.T, attrs pcommon.Map, key, val string)
assert.Equal(t, val, v.AsString())
}

func AssertAttrsStringVal(t *testing.T, attrs pcommon.Map, key, val string) {
var s string
v, ok := attrs.Get(key)
if ok {
s = v.AsString()
}
assert.Equal(t, val, s)
}

func AssertAttrsFoundIntVal(t *testing.T, attrs pcommon.Map, key string, val int) {
v, ok := attrs.Get(key)
assert.True(t, ok)
Expand Down
30 changes: 19 additions & 11 deletions pkg/collector/processor/servicediscover/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,24 @@ processor:
testCases := []struct {
name string
httpUrl string
attrs map[string]string
}{
{"MatchSuccess", "http://example:19100/benchmark/2024"},
{"MatchFailed", "http://example:19100/benchmark-1/2024"},
{
name: "Success",
httpUrl: "http://example:19100/benchmark/2024",
attrs: map[string]string{
"http.route": "GET:/benchmark/{uuid}",
"peer.service": "example:19100",
},
},
{
name: "Failed",
httpUrl: "http://example:19100/benchmark-1/2024",
attrs: map[string]string{
"http.route": "",
"peer.service": "",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -421,19 +436,12 @@ processor:

data = record.Data.(ptrace.Traces)
foreach.Spans(data.ResourceSpans(), func(span ptrace.Span) {
if tc.name == "MatchSuccess" {
testkits.AssertAttrsFoundStringVal(t, span.Attributes(), "http.route", "GET:/benchmark/{uuid}")
testkits.AssertAttrsFoundStringVal(t, span.Attributes(), "peer.service", "example:19100")
}
if tc.name == "MatchFailed" {
testkits.AssertAttrsNotFound(t, span.Attributes(), "http.route")
testkits.AssertAttrsNotFound(t, span.Attributes(), "peer.service")
for k, v := range tc.attrs {
testkits.AssertAttrsStringVal(t, span.Attributes(), k, v)
}
})
})

}

}

func TestTracesRegexMatchedWithSpanName(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/operator/common/kits/kits.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func CheckIfSystemResource(items map[string]string) bool {
func CheckIfForwardLocalhost(items map[string]string) bool {
return stringToBool(items, "forwardLocalhost")
}

func CheckIfNormalizeMetricName(items map[string]string) bool {
return stringToBool(items, "normalizeMetricName")
}
1 change: 1 addition & 0 deletions pkg/operator/dockerfile/operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM centos:7

VOLUME /data
RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tencent.com/repo/centos7_base.repo
RUN yum install -y iproute strace tcpdump

RUN mkdir -p /data/bkmonitor-operator/config
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/dockerfile/reloader/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM centos:7

VOLUME /data
RUN yum clean all && rm -f /var/lib/rpm/__db* && rpm --rebuilddb
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tencent.com/repo/centos7_base.repo
RUN yum install -y iproute strace tcpdump

RUN mkdir -p /data/reloader/config
Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/operator/discover/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type BaseParams struct {
Client kubernetes.Interface
RelabelRule string
RelabelIndex string
NormalizeMetricName bool
Name string
KubeConfig string
Namespaces []string
Expand Down Expand Up @@ -408,6 +409,7 @@ func (d *BaseDiscover) makeMetricTarget(lbls, origLabels labels.Labels, namespac
metricTarget.TaskType = taskType
metricTarget.RelabelRule = d.RelabelRule
metricTarget.RelabelIndex = d.RelabelIndex
metricTarget.NormalizeMetricName = d.NormalizeMetricName

return metricTarget, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func (c *Operator) createServiceMonitorDiscovers(serviceMonitor *promv1.ServiceM
Client: c.client,
RelabelRule: serviceMonitor.Annotations[annotationRelabelRule],
RelabelIndex: serviceMonitor.Annotations[annotationRelabelIndex],
NormalizeMetricName: kits.CheckIfNormalizeMetricName(serviceMonitor.Annotations),
AnnotationMatchSelector: parseSelector(serviceMonitor.Annotations[annotationMonitorMatchSelector]),
AnnotationDropSelector: parseSelector(serviceMonitor.Annotations[annotationMonitorDropSelector]),
Name: monitorMeta.ID(),
Expand Down Expand Up @@ -770,6 +771,7 @@ func (c *Operator) createPodMonitorDiscovers(podMonitor *promv1.PodMonitor) []di
Client: c.client,
RelabelRule: podMonitor.Annotations[annotationRelabelRule],
RelabelIndex: podMonitor.Annotations[annotationRelabelIndex],
NormalizeMetricName: kits.CheckIfNormalizeMetricName(podMonitor.Annotations),
AnnotationMatchSelector: parseSelector(podMonitor.Annotations[annotationMonitorMatchSelector]),
AnnotationDropSelector: parseSelector(podMonitor.Annotations[annotationMonitorDropSelector]),
Name: monitorMeta.ID(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/operator/target/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type MetricTarget struct {
Meta define.MonitorMeta
RelabelRule string
RelabelIndex string
NormalizeMetricName bool
Address string
NodeName string
Scheme string
Expand Down Expand Up @@ -149,6 +150,7 @@ func (t *MetricTarget) YamlBytes() ([]byte, error) {
module = append(module, *remoteRelabel)
}
module = append(module, yaml.MapItem{Key: "disable_custom_timestamp", Value: t.DisableCustomTimestamp})
module = append(module, yaml.MapItem{Key: "normalize_metric_name", Value: t.NormalizeMetricName})

address := t.Address
if !strings.HasPrefix(address, "http://") && !strings.HasPrefix(address, "https://") {
Expand Down

0 comments on commit aa0d810

Please sign in to comment.