@@ -13,8 +13,8 @@ import (
1313type PrometheusMapping struct {}
1414
1515func (pm * PrometheusMapping ) MapCounter (ms * metricsStorage , sample metrics.Sample , labels []prompb.Label ) []prompb.TimeSeries {
16- sample = ms .update (sample , nil )
17- aggr := sample . Metric .Sink .Format (0 )
16+ metric : = ms .update (sample , nil )
17+ aggr := metric .Sink .Format (0 )
1818
1919return []prompb.TimeSeries {
2020{
@@ -33,9 +33,6 @@ func (pm *PrometheusMapping) MapCounter(ms *metricsStorage, sample metrics.Sampl
3333}
3434
3535func (pm * PrometheusMapping ) MapGauge (ms * metricsStorage , sample metrics.Sample , labels []prompb.Label ) []prompb.TimeSeries {
36- sample = ms .update (sample , nil )
37- aggr := sample .Metric .Sink .Format (0 )
38-
3936return []prompb.TimeSeries {
4037{
4138Labels : append (labels , prompb.Label {
@@ -44,7 +41,9 @@ func (pm *PrometheusMapping) MapGauge(ms *metricsStorage, sample metrics.Sample,
4441}),
4542Samples : []prompb.Sample {
4643{
47- Value : aggr ["value" ],
44+ // Gauge is just the latest value
45+ // so we can skip the sink using directly the value from the sample.
46+ Value : sample .Value ,
4847Timestamp : timestamp .FromTime (sample .Time ),
4948},
5049},
@@ -53,8 +52,8 @@ func (pm *PrometheusMapping) MapGauge(ms *metricsStorage, sample metrics.Sample,
5352}
5453
5554func (pm * PrometheusMapping ) MapRate (ms * metricsStorage , sample metrics.Sample , labels []prompb.Label ) []prompb.TimeSeries {
56- sample = ms .update (sample , nil )
57- aggr := sample . Metric .Sink .Format (0 )
55+ metric : = ms .update (sample , nil )
56+ aggr := metric .Sink .Format (0 )
5857
5958return []prompb.TimeSeries {
6059{
@@ -73,9 +72,13 @@ func (pm *PrometheusMapping) MapRate(ms *metricsStorage, sample metrics.Sample,
7372}
7473
7574func (pm * PrometheusMapping ) MapTrend (ms * metricsStorage , sample metrics.Sample , labels []prompb.Label ) []prompb.TimeSeries {
76- sample = ms .update (sample , trendAdd )
75+ metric := ms .update (sample , trendAdd )
76+
77+ // Prometheus metric system does not support Trend so this mapping will store gauges
78+ // to keep track of key values.
79+ // TODO: when Prometheus implements support for sparse histograms, re-visit this implementation
7780
78- s := sample . Metric .Sink .(* metrics.TrendSink )
81+ s := metric .Sink .(* metrics.TrendSink )
7982aggr := map [string ]float64 {
8083"min" : s .Min ,
8184"max" : s .Max ,
@@ -85,10 +88,6 @@ func (pm *PrometheusMapping) MapTrend(ms *metricsStorage, sample metrics.Sample,
8588"p(95)" : p (s , 0.95 ),
8689}
8790
88- // Prometheus metric system does not support Trend so this mapping will store gauges
89- // to keep track of key values.
90- // TODO: when Prometheus implements support for sparse histograms, re-visit this implementation
91-
9291return []prompb.TimeSeries {
9392{
9493Labels : append (labels , prompb.Label {
@@ -169,8 +168,8 @@ func (pm *PrometheusMapping) MapTrend(ms *metricsStorage, sample metrics.Sample,
169168// and are a partial copy-paste from k6/metrics.
170169// TODO: re-write & refactor this once metrics refactoring progresses in k6.
171170
172- func trendAdd (current , s metrics.Sample ) metrics. Sample {
173- t := current .Metric . Sink .(* metrics.TrendSink )
171+ func trendAdd (current * metrics. Metric , s metrics.Sample ) {
172+ t := current .Sink .(* metrics.TrendSink )
174173
175174// insert into sorted array instead of sorting anew on each addition
176175index := sort .Search (len (t .Values ), func (i int ) bool {
@@ -197,8 +196,7 @@ func trendAdd(current, s metrics.Sample) metrics.Sample {
197196t .Med = t .Values [t .Count / 2 ]
198197}
199198
200- current .Metric .Sink = t
201- return current
199+ current .Sink = t
202200}
203201
204202func p (t * metrics.TrendSink , pct float64 ) float64 {
0 commit comments