Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitrySenin committed Oct 4, 2020
1 parent d6db28e commit fc571ad
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 88 deletions.
13 changes: 5 additions & 8 deletions plugins/parsers/prometheus/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ func ValueType(mt dto.MetricType) telegraf.ValueType {
// Get labels from metric
func MakeLabels(m *dto.Metric, defaultTags map[string]string) map[string]string {
result := map[string]string{}
for _, lp := range m.Label {
result[lp.GetName()] = lp.GetValue()

for key, value := range defaultTags {
result[key] = value
}

if defaultTags != nil {
for k, v := range defaultTags {
if _, exists := result[k]; exists {
result[k] = v
}
}
for _, lp := range m.Label {
result[lp.GetName()] = lp.GetValue()
}

return result
Expand Down
33 changes: 15 additions & 18 deletions plugins/parsers/prometheus/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
fields = getNameAndValue(m, metricName)
// converting to telegraf metric
if len(fields) > 0 {
var t time.Time
if m.TimestampMs != nil && *m.TimestampMs > 0 {
t = time.Unix(0, *m.TimestampMs*1000000)
} else {
t = now
}
t := getTimestamp(m, now)
metric, err := metric.New("prometheus", tags, fields, t, ValueType(mf.GetType()))
if err == nil {
metrics = append(metrics, metric)
Expand Down Expand Up @@ -102,12 +97,8 @@ func (p *Parser) SetDefaultTags(tags map[string]string) {
func makeQuantiles(m *dto.Metric, tags map[string]string, metricName string, metricType dto.MetricType, now time.Time) []telegraf.Metric {
var metrics []telegraf.Metric
fields := make(map[string]interface{})
var t time.Time
if m.TimestampMs != nil && *m.TimestampMs > 0 {
t = time.Unix(0, *m.TimestampMs*1000000)
} else {
t = now
}
t := getTimestamp(m, now)

fields[metricName+"_count"] = float64(m.GetSummary().GetSampleCount())
fields[metricName+"_sum"] = float64(m.GetSummary().GetSampleSum())
met, err := metric.New("prometheus", tags, fields, t, ValueType(metricType))
Expand All @@ -134,12 +125,8 @@ func makeQuantiles(m *dto.Metric, tags map[string]string, metricName string, met
func makeBuckets(m *dto.Metric, tags map[string]string, metricName string, metricType dto.MetricType, now time.Time) []telegraf.Metric {
var metrics []telegraf.Metric
fields := make(map[string]interface{})
var t time.Time
if m.TimestampMs != nil && *m.TimestampMs > 0 {
t = time.Unix(0, *m.TimestampMs*1000000)
} else {
t = now
}
t := getTimestamp(m, now)

fields[metricName+"_count"] = float64(m.GetHistogram().GetSampleCount())
fields[metricName+"_sum"] = float64(m.GetHistogram().GetSampleSum())

Expand Down Expand Up @@ -180,3 +167,13 @@ func getNameAndValue(m *dto.Metric, metricName string) map[string]interface{} {
}
return fields
}

func getTimestamp(m *dto.Metric, now time.Time) time.Time {
var t time.Time
if m.TimestampMs != nil && *m.TimestampMs > 0 {
t = time.Unix(0, m.GetTimestampMs()*1000000)
} else {
t = now
}
return t
}
Loading

0 comments on commit fc571ad

Please sign in to comment.