Skip to content
Merged
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
32 changes: 28 additions & 4 deletions metrics/prometheus/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
typeCounterTpl = "# TYPE %s counter\n"
typeSummaryTpl = "# TYPE %s summary\n"
keyValueTpl = "%s %v\n\n"
keyCounterTpl = "%s %v\n"
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n"
)

Expand Down Expand Up @@ -61,11 +62,16 @@ func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) {
func (c *collector) addHistogram(name string, m metrics.Histogram) {
pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
ps := m.Percentiles(pv)
c.writeSummaryCounter(name, m.Count())

var sum float64 = 0
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name)))
for i := range pv {
c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i])
sum += ps[i]
}

c.writeSummarySum(name, fmt.Sprintf("%f", sum))
c.writeSummaryCounter(name, len(ps))
c.buff.WriteRune('\n')
}

Expand All @@ -76,7 +82,7 @@ func (c *collector) addMeter(name string, m metrics.Meter) {
func (c *collector) addTimer(name string, m metrics.Timer) {
pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
ps := m.Percentiles(pv)
c.writeSummaryCounter(name, m.Count())
c.writeCounter(name, m.Count())
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name)))
for i := range pv {
c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i])
Expand All @@ -90,11 +96,19 @@ func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) {
}
ps := m.Percentiles([]float64{50, 95, 99})
val := m.Values()
c.writeSummaryCounter(name, len(val))
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name)))
c.writeSummaryPercentile(name, "0.50", ps[0])
c.writeSummaryPercentile(name, "0.95", ps[1])
c.writeSummaryPercentile(name, "0.99", ps[2])

var sum int64 = 0

for _, v := range val {
sum += v
}

c.writeSummarySum(name, fmt.Sprintf("%d", sum))
c.writeSummaryCounter(name, len(val))
c.buff.WriteRune('\n')
}

Expand All @@ -104,17 +118,27 @@ func (c *collector) writeGaugeCounter(name string, value interface{}) {
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
}

func (c *collector) writeSummaryCounter(name string, value interface{}) {
func (c *collector) writeCounter(name string, value interface{}) {
name = mutateKey(name + "_count")
c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name))
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
}

func (c *collector) writeSummaryCounter(name string, value interface{}) {
name = mutateKey(name + "_count")
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value))
}

func (c *collector) writeSummaryPercentile(name, p string, value interface{}) {
name = mutateKey(name)
c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value))
}

func (c *collector) writeSummarySum(name string, value string) {
name = mutateKey(name + "_sum")
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value))
}

func mutateKey(key string) string {
return strings.Replace(key, "/", "_", -1)
}
13 changes: 7 additions & 6 deletions metrics/prometheus/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ test_gauge 23456
# TYPE test_gauge_float64 gauge
test_gauge_float64 34567.89

# TYPE test_histogram_count counter
test_histogram_count 0

# TYPE test_histogram summary
test_histogram {quantile="0.5"} 0
test_histogram {quantile="0.75"} 0
test_histogram {quantile="0.95"} 0
test_histogram {quantile="0.99"} 0
test_histogram {quantile="0.999"} 0
test_histogram {quantile="0.9999"} 0
test_histogram_sum 0.000000
test_histogram_count 6

# TYPE test_meter gauge
test_meter 9999999
Expand All @@ -92,15 +91,17 @@ test_timer {quantile="0.99"} 1.2e+08
test_timer {quantile="0.999"} 1.2e+08
test_timer {quantile="0.9999"} 1.2e+08

# TYPE test_resetting_timer_count counter
test_resetting_timer_count 6

# TYPE test_resetting_timer summary
test_resetting_timer {quantile="0.50"} 12000000
test_resetting_timer {quantile="0.95"} 120000000
test_resetting_timer {quantile="0.99"} 120000000
test_resetting_timer_sum 180000000
test_resetting_timer_count 6

`

c.addResettingTimer("test/empty_resetting_timer", emptyResettingTimer)

exp := c.buff.String()
if exp != expectedOutput {
t.Log("Expected Output:\n", expectedOutput)
Expand Down