From f3650278f8258a64e1ab077b75f9db6d04c40b51 Mon Sep 17 00:00:00 2001 From: KAcper Perschke Date: Wed, 13 Nov 2024 19:43:01 +0100 Subject: [PATCH 1/3] More extensive diagnostics. In case of OpenMetrics fetch. --- collector/openMetrics.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/collector/openMetrics.go b/collector/openMetrics.go index c72c3b9..c8b8f6d 100644 --- a/collector/openMetrics.go +++ b/collector/openMetrics.go @@ -1,6 +1,7 @@ package collector import ( + "fmt" "strings" "github.com/prometheus/client_golang/prometheus" @@ -26,7 +27,15 @@ func (e *Exporter) exportOpenMetrics(ch chan<- prometheus.Metric) error { parser := expfmt.TextParser{} metrics, err := parser.TextToMetricFamilies(strings.NewReader(openMetricsString)) if err != nil { - return err + e.logger.Error( + "Openmetrics downloaded from artifactory cannot be parsed using the “github.com/prometheus/common/expfmt”.", + "err", err.Error(), + "response.body", openMetricsString, + ) + return fmt.Errorf( + "problem when parsing openmetrics downloaded from artifactory: %w", + err, + ) } for _, family := range metrics { From faea723de03a031599b6002fec61a8b97eb86a34 Mon Sep 17 00:00:00 2001 From: KAcper Perschke Date: Wed, 13 Nov 2024 19:55:54 +0100 Subject: [PATCH 2/3] Minor cosmetics. --- collector/openMetrics.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/collector/openMetrics.go b/collector/openMetrics.go index c8b8f6d..322936e 100644 --- a/collector/openMetrics.go +++ b/collector/openMetrics.go @@ -17,13 +17,11 @@ func (e *Exporter) exportOpenMetrics(ch chan<- prometheus.Metric) error { return err } + openMetricsString := openMetrics.PromMetrics e.logger.Debug( "OpenMetrics from Artifactory util", - "body", openMetrics.PromMetrics, + "body", openMetricsString, ) - - openMetricsString := openMetrics.PromMetrics - parser := expfmt.TextParser{} metrics, err := parser.TextToMetricFamilies(strings.NewReader(openMetricsString)) if err != nil { From 9103dae642ce558370c0d63ac8c12644d990d959 Mon Sep 17 00:00:00 2001 From: KAcper Perschke Date: Wed, 13 Nov 2024 20:30:27 +0100 Subject: [PATCH 3/3] Major cosmetics. --- collector/openMetrics.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/collector/openMetrics.go b/collector/openMetrics.go index 322936e..a4fb980 100644 --- a/collector/openMetrics.go +++ b/collector/openMetrics.go @@ -36,28 +36,31 @@ func (e *Exporter) exportOpenMetrics(ch chan<- prometheus.Metric) error { ) } + createDesc := func(fn, fh string, m *ioPrometheusClient.Metric) *prometheus.Desc { + labels := make(map[string]string) + for _, label := range m.Label { + labels[*label.Name] = *label.Value + } + return prometheus.NewDesc(fn, fh, nil, labels) + } for _, family := range metrics { + fName := family.GetName() + fHelp := family.GetHelp() for _, metric := range family.Metric { - // create labels map - labels := make(map[string]string) - for _, label := range metric.Label { - labels[*label.Name] = *label.Value - } - - // create a new descriptor - desc := prometheus.NewDesc( - family.GetName(), - family.GetHelp(), - nil, - labels, - ) - - // create a new metric and collect it + desc := createDesc(fName, fHelp, metric) switch family.GetType() { case ioPrometheusClient.MetricType_COUNTER: - ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, metric.GetCounter().GetValue()) + ch <- prometheus.MustNewConstMetric( + desc, + prometheus.CounterValue, + metric.GetCounter().GetValue(), + ) case ioPrometheusClient.MetricType_GAUGE: - ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, metric.GetGauge().GetValue()) + ch <- prometheus.MustNewConstMetric( + desc, + prometheus.GaugeValue, + metric.GetGauge().GetValue(), + ) } } }