Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

丢弃value中带有Nan,+Inf,-Inf的metric #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions collector/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ func Parse(buf []byte) ([]*dataobj.MetricValue, error) {

metrics := []*dataobj.MetricValue{}
for _, m := range mf.Metric {
// 丢弃value中带有Nan,+Inf和-Inf的metric
gv := m.Gauge.GetValue()
if math.IsInf(gv, 0) || math.IsNaN(gv) {
continue
}

// pass ignore metric
if filterIgnoreMetric(basename) {
continue
}

switch mf.GetType() {
case dto.MetricType_GAUGE:
// gauge metric
Expand Down Expand Up @@ -89,21 +86,30 @@ func makeQuantiles(basename string, m *dto.Metric) []*dataobj.MetricValue {
metrics := []*dataobj.MetricValue{}
tags := makeLabels(m)

countName := fmt.Sprintf("%s_count", basename)
metrics = append(metrics, model.NewCumulativeMetric(countName, m.GetSummary().SampleCount, now, tags))
// 不能保证取到的metric一定是按照标准输出的,只能尽量去兼容

sumName := fmt.Sprintf("%s_sum", basename)
metrics = append(metrics, model.NewCumulativeMetric(sumName, m.GetSummary().SampleSum, now, tags))
if m.GetSummary().SampleCount != nil {
if !math.IsNaN(float64(m.GetSummary().GetSampleCount())) && !math.IsInf(float64(m.GetSummary().GetSampleCount()), 0) {
countName := fmt.Sprintf("%s_count", basename)
metrics = append(metrics, model.NewCumulativeMetric(countName, m.GetSummary().SampleCount, now, tags))
}
}
if m.GetSummary().SampleSum != nil {
if !math.IsNaN(m.GetSummary().GetSampleSum()) && !math.IsInf(m.GetSummary().GetSampleSum(), 0) {
sumName := fmt.Sprintf("%s_sum", basename)
metrics = append(metrics, model.NewCumulativeMetric(sumName, m.GetSummary().SampleSum, now, tags))
}
}

for _, q := range m.GetSummary().Quantile {
tagsNew := make(map[string]string)
for tagKey, tagValue := range tags {
tagsNew[tagKey] = tagValue
}
if !math.IsNaN(q.GetValue()) {
if !math.IsNaN(q.GetValue()) && !math.IsInf(q.GetValue(), 0) {
tagsNew := make(map[string]string)
for tagKey, tagValue := range tags {
tagsNew[tagKey] = tagValue
}
tagsNew["quantile"] = fmt.Sprint(q.GetQuantile())

metrics = append(metrics, model.NewGaugeMetric(basename, float64(q.GetValue()), now, tagsNew))
metrics = append(metrics, model.NewGaugeMetric(basename, q.GetValue(), now, tagsNew))
}
}

Expand All @@ -115,22 +121,31 @@ func makeBuckets(basename string, m *dto.Metric) []*dataobj.MetricValue {
metrics := []*dataobj.MetricValue{}
tags := makeLabels(m)

countName := fmt.Sprintf("%s_count", basename)
metrics = append(metrics, model.NewCumulativeMetric(countName, m.GetHistogram().SampleCount, now, tags))
if m.GetHistogram().SampleCount != nil {
if !math.IsNaN(float64(m.GetHistogram().GetSampleCount())) && !math.IsInf(float64(m.GetHistogram().GetSampleCount()), 0) {
countName := fmt.Sprintf("%s_count", basename)
metrics = append(metrics, model.NewCumulativeMetric(countName, m.GetHistogram().SampleCount, now, tags))
}
}

sumName := fmt.Sprintf("%s_sum", basename)
metrics = append(metrics, model.NewCumulativeMetric(sumName, m.GetHistogram().SampleSum, now, tags))
if m.GetHistogram().SampleSum != nil {
if !math.IsNaN(m.GetHistogram().GetSampleSum()) && !math.IsInf(m.GetHistogram().GetSampleSum(), 0) {
sumName := fmt.Sprintf("%s_sum", basename)
metrics = append(metrics, model.NewCumulativeMetric(sumName, m.GetHistogram().SampleSum, now, tags))
}
}

for _, b := range m.GetHistogram().Bucket {
//if !math.IsNaN(float64(b.GetCumulativeCount())) && !math.IsInf(float64(b.GetCumulativeCount()), 0) {
tagsNew := make(map[string]string)
for tagKey, tagValue := range tags {
tagsNew[tagKey] = tagValue
}
tagsNew["le"] = fmt.Sprint(b.GetUpperBound())

bucketName := fmt.Sprintf("%s_bucket", basename)
metrics = append(metrics, model.NewGaugeMetric(bucketName, float64(b.GetCumulativeCount()), now, tagsNew))
}
//}

return metrics
}
Expand All @@ -141,18 +156,18 @@ func makeCommon(metricName string, m *dto.Metric) []*dataobj.MetricValue {
metrics := []*dataobj.MetricValue{}
tags := makeLabels(m)
if m.Gauge != nil {
if !math.IsNaN(m.GetGauge().GetValue()) {
if !math.IsNaN(m.GetGauge().GetValue()) && !math.IsInf(m.GetGauge().GetValue(), 0) {
val = float64(m.GetGauge().GetValue())
metrics = append(metrics, model.NewGaugeMetric(metricName, val, now, tags))
}
} else if m.Counter != nil {
if !math.IsNaN(m.GetCounter().GetValue()) {
if !math.IsNaN(m.GetCounter().GetValue()) && !math.IsInf(m.GetCounter().GetValue(), 0) {
val = float64(m.GetCounter().GetValue())
metrics = append(metrics, model.NewCumulativeMetric(metricName, val, now, tags))
}
} else if m.Untyped != nil {
// untyped as gauge
if !math.IsNaN(m.GetUntyped().GetValue()) {
if !math.IsNaN(m.GetUntyped().GetValue()) && !math.IsInf(m.GetUntyped().GetValue(), 0) {
val = float64(m.GetUntyped().GetValue())
metrics = append(metrics, model.NewGaugeMetric(metricName, val, now, tags))
}
Expand Down
40 changes: 40 additions & 0 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,46 @@ const validPromUntypedMetric = `# HELP mysql_global_status_aborted_clients Gener
# TYPE mysql_global_status_aborted_clients untyped
mysql_global_status_aborted_clients 62539
`
const validPromAbnormalMetric = `# HELP aggregated_results aggregated_results counter
# TYPE aggregated_results counter
aggregated_results{namespace="default",type="Nan"} Nan
aggregated_results{namespace="default",type="-Inf"} -Inf
aggregated_results{namespace="default",type="+Inf"} +Inf
aggregated_results{namespace="default",type="value"} 6.5928518e+07
# HELP block_pool_free block_pool_free gauge
# TYPE block_pool_free gauge
block_pool_free{namespace="default"} Nan
block_pool_free{namespace="default"} -Inf
block_pool_free{namespace="default"} +Inf
block_pool_free{namespace="default"} 2111
# TYPE bootstrapper_commitlog_commitlog_duration summary
bootstrapper_commitlog_commitlog_duration{quantile="0.5"} 100
bootstrapper_commitlog_commitlog_duration{quantile="0.75"} Nan
bootstrapper_commitlog_commitlog_duration{quantile="0.95"} +Inf
bootstrapper_commitlog_commitlog_duration{quantile="0.99"} -Inf
bootstrapper_commitlog_commitlog_duration_count 100
bootstrapper_commitlog_commitlog_duration_sum Nan
# TYPE mysql_global_status_aborted_clients untyped
mysql_global_status_aborted_clients{namespace="default",type="Nan"} Nan
mysql_global_status_aborted_clients{namespace="default",type="-Inf"} -Inf
mysql_global_status_aborted_clients{namespace="default",type="+Inf"} +Inf
mysql_global_status_aborted_clients{namespace="default",type="value"} 62539
# HELP database_bootstrap_errors_latency database_bootstrap_errors_latency histogram
# TYPE database_bootstrap_errors_latency histogram
database_bootstrap_errors_latency_bucket{namespace="default",le="0.002",type="Nan"} Nan
database_bootstrap_errors_latency_bucket{namespace="default",le="0.004",type="-Inf"} -Inf
database_bootstrap_errors_latency_bucket{namespace="default",le="0.006",type="+Inf"} +Inf
database_bootstrap_errors_latency_bucket{namespace="default",le="0.008",type="value"} 0
`

func TestPromAbnormalMetricParser(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
assert.NoError(t, err)

metrics, err := collector.Parse([]byte(validPromAbnormalMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 9)
}

func TestPromMetricParser(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ require (
github.com/open-falcon/falcon-plus v0.2.2
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.10.0
github.com/prometheus/node_exporter v1.0.1 // indirect
github.com/stretchr/testify v1.4.0
github.com/toolkits/time v0.0.0-20160524122720-c274716e8d7f // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)