Skip to content

Commit 3e9348d

Browse files
committed
refactor
1 parent 5de4f72 commit 3e9348d

File tree

1 file changed

+130
-97
lines changed

1 file changed

+130
-97
lines changed

internal/castai/client.go

Lines changed: 130 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ func convertPrometheusMetricFamilies(gatherTime time.Time, podName string, metri
198198
timeseries := []PrometheusTimeseries{}
199199
for _, family := range metricFamilies {
200200
for _, metric := range family.Metric {
201-
// Right now we support only export of counter and histogram metrics.
202-
if metric.Counter == nil && metric.Histogram == nil && metric.Summary == nil {
203-
continue
204-
}
205-
206201
commonLabels := []PrometheusLabel{
207202
{
208203
Name: "pod_name",
@@ -222,104 +217,19 @@ func convertPrometheusMetricFamilies(gatherTime time.Time, podName string, metri
222217
}
223218

224219
if metric.Counter != nil {
225-
timeseries = append(timeseries, PrometheusTimeseries{
226-
Labels: copyLabelsWithName(commonLabels, family.GetName()),
227-
Samples: []PrometheusSample{
228-
{
229-
Timestamp: timestamp,
230-
Value: metric.Counter.GetValue(),
231-
},
232-
},
233-
})
220+
timeseries = append(timeseries,
221+
convertPrometheusCounterMetric(commonLabels, family, metric, timestamp)...,
222+
)
234223
}
235224

236225
if metric.Histogram != nil {
237-
h := metric.Histogram
238-
239-
for _, b := range h.Bucket {
240-
timeseries = append(timeseries, PrometheusTimeseries{
241-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_bucket", PrometheusLabel{
242-
Name: "le", Value: fmt.Sprintf("%f", b.GetUpperBound()),
243-
}),
244-
Samples: []PrometheusSample{
245-
{
246-
Timestamp: timestamp,
247-
Value: float64(b.GetCumulativeCount()),
248-
},
249-
},
250-
})
251-
}
252-
// We need this +Inf bucket for histogram_quantile query.
253-
timeseries = append(timeseries, PrometheusTimeseries{
254-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_bucket", PrometheusLabel{
255-
Name: "le", Value: "+Inf",
256-
}),
257-
Samples: []PrometheusSample{
258-
{
259-
Timestamp: timestamp,
260-
Value: float64(h.GetSampleCount()),
261-
},
262-
},
263-
})
264-
265-
timeseries = append(timeseries, PrometheusTimeseries{
266-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_sum"),
267-
Samples: []PrometheusSample{
268-
{
269-
Timestamp: timestamp,
270-
Value: h.GetSampleSum(),
271-
},
272-
},
273-
})
274-
275-
timeseries = append(timeseries, PrometheusTimeseries{
276-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_count"),
277-
Samples: []PrometheusSample{
278-
{
279-
Timestamp: timestamp,
280-
Value: float64(h.GetSampleCount()),
281-
},
282-
},
283-
})
226+
timeseries = append(timeseries,
227+
convertPrometheusHistogramMetric(commonLabels, family, metric, timestamp)...)
284228
}
285229

286230
if metric.Summary != nil {
287-
s := metric.Summary
288-
289-
for _, quantile := range s.Quantile {
290-
timeseries = append(timeseries, PrometheusTimeseries{
291-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_quantile", PrometheusLabel{
292-
Name: "quantile",
293-
Value: fmt.Sprintf("%f", quantile.GetQuantile()),
294-
}),
295-
Samples: []PrometheusSample{
296-
{
297-
Timestamp: timestamp,
298-
Value: quantile.GetValue(),
299-
},
300-
},
301-
})
302-
}
303-
304-
timeseries = append(timeseries, PrometheusTimeseries{
305-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_sum"),
306-
Samples: []PrometheusSample{
307-
{
308-
Timestamp: timestamp,
309-
Value: s.GetSampleSum(),
310-
},
311-
},
312-
})
313-
314-
timeseries = append(timeseries, PrometheusTimeseries{
315-
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_count"),
316-
Samples: []PrometheusSample{
317-
{
318-
Timestamp: timestamp,
319-
Value: float64(s.GetSampleCount()),
320-
},
321-
},
322-
})
231+
timeseries = append(timeseries,
232+
convertPrometheusSummaryMetric(commonLabels, family, metric, timestamp)...)
323233
}
324234
}
325235
}
@@ -329,6 +239,129 @@ func convertPrometheusMetricFamilies(gatherTime time.Time, podName string, metri
329239
}
330240
}
331241

242+
func convertPrometheusCounterMetric(
243+
commonLabels []PrometheusLabel,
244+
family *dto.MetricFamily,
245+
metric *dto.Metric,
246+
timestamp int64,
247+
) []PrometheusTimeseries {
248+
return []PrometheusTimeseries{
249+
{
250+
Labels: copyLabelsWithName(commonLabels, family.GetName()),
251+
Samples: []PrometheusSample{
252+
{
253+
Timestamp: timestamp,
254+
Value: metric.Counter.GetValue(),
255+
},
256+
},
257+
},
258+
}
259+
}
260+
261+
func convertPrometheusHistogramMetric(
262+
commonLabels []PrometheusLabel,
263+
family *dto.MetricFamily,
264+
metric *dto.Metric,
265+
timestamp int64,
266+
) []PrometheusTimeseries {
267+
timeseries := make([]PrometheusTimeseries, 0)
268+
h := metric.Histogram
269+
270+
for _, b := range h.Bucket {
271+
timeseries = append(timeseries, PrometheusTimeseries{
272+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_bucket", PrometheusLabel{
273+
Name: "le", Value: fmt.Sprintf("%f", b.GetUpperBound()),
274+
}),
275+
Samples: []PrometheusSample{
276+
{
277+
Timestamp: timestamp,
278+
Value: float64(b.GetCumulativeCount()),
279+
},
280+
},
281+
})
282+
}
283+
// We need this +Inf bucket for histogram_quantile query.
284+
timeseries = append(timeseries, PrometheusTimeseries{
285+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_bucket", PrometheusLabel{
286+
Name: "le", Value: "+Inf",
287+
}),
288+
Samples: []PrometheusSample{
289+
{
290+
Timestamp: timestamp,
291+
Value: float64(h.GetSampleCount()),
292+
},
293+
},
294+
})
295+
296+
timeseries = append(timeseries, PrometheusTimeseries{
297+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_sum"),
298+
Samples: []PrometheusSample{
299+
{
300+
Timestamp: timestamp,
301+
Value: h.GetSampleSum(),
302+
},
303+
},
304+
})
305+
306+
timeseries = append(timeseries, PrometheusTimeseries{
307+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_count"),
308+
Samples: []PrometheusSample{
309+
{
310+
Timestamp: timestamp,
311+
Value: float64(h.GetSampleCount()),
312+
},
313+
},
314+
})
315+
316+
return timeseries
317+
}
318+
319+
func convertPrometheusSummaryMetric(
320+
commonLabels []PrometheusLabel,
321+
family *dto.MetricFamily,
322+
metric *dto.Metric,
323+
timestamp int64,
324+
) []PrometheusTimeseries {
325+
timeseries := make([]PrometheusTimeseries, 0)
326+
s := metric.Summary
327+
328+
for _, quantile := range s.Quantile {
329+
timeseries = append(timeseries, PrometheusTimeseries{
330+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_quantile", PrometheusLabel{
331+
Name: "quantile",
332+
Value: fmt.Sprintf("%f", quantile.GetQuantile()),
333+
}),
334+
Samples: []PrometheusSample{
335+
{
336+
Timestamp: timestamp,
337+
Value: quantile.GetValue(),
338+
},
339+
},
340+
})
341+
}
342+
343+
timeseries = append(timeseries, PrometheusTimeseries{
344+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_sum"),
345+
Samples: []PrometheusSample{
346+
{
347+
Timestamp: timestamp,
348+
Value: s.GetSampleSum(),
349+
},
350+
},
351+
})
352+
353+
timeseries = append(timeseries, PrometheusTimeseries{
354+
Labels: copyLabelsWithName(commonLabels, family.GetName()+"_count"),
355+
Samples: []PrometheusSample{
356+
{
357+
Timestamp: timestamp,
358+
Value: float64(s.GetSampleCount()),
359+
},
360+
},
361+
})
362+
return timeseries
363+
}
364+
332365
func copyLabelsWithName(baseLabels []PrometheusLabel, name string, additionalLabels ...PrometheusLabel) []PrometheusLabel {
333366
labels := make([]PrometheusLabel, 0, len(baseLabels)+1)
334367
labels = append(labels, baseLabels...)

0 commit comments

Comments
 (0)