pangolin_exporter

Форк
0
/
collector_test.go 
62 строки · 1.9 Кб
1
// Copyright 2023 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
package collector
14

15
import (
16
	"strings"
17

18
	"github.com/prometheus/client_golang/prometheus"
19
	dto "github.com/prometheus/client_model/go"
20
)
21

22
type labelMap map[string]string
23

24
type MetricResult struct {
25
	labels     labelMap
26
	value      float64
27
	metricType dto.MetricType
28
}
29

30
func readMetric(m prometheus.Metric) MetricResult {
31
	pb := &dto.Metric{}
32
	m.Write(pb)
33
	labels := make(labelMap, len(pb.Label))
34
	for _, v := range pb.Label {
35
		labels[v.GetName()] = v.GetValue()
36
	}
37
	if pb.Gauge != nil {
38
		return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE}
39
	}
40
	if pb.Counter != nil {
41
		return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER}
42
	}
43
	if pb.Untyped != nil {
44
		return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED}
45
	}
46
	panic("Unsupported metric type")
47
}
48

49
func sanitizeQuery(q string) string {
50
	q = strings.Join(strings.Fields(q), " ")
51
	q = strings.Replace(q, "(", "\\(", -1)
52
	q = strings.Replace(q, "?", "\\?", -1)
53
	q = strings.Replace(q, ")", "\\)", -1)
54
	q = strings.Replace(q, "[", "\\[", -1)
55
	q = strings.Replace(q, "]", "\\]", -1)
56
	q = strings.Replace(q, "{", "\\{", -1)
57
	q = strings.Replace(q, "}", "\\}", -1)
58
	q = strings.Replace(q, "*", "\\*", -1)
59
	q = strings.Replace(q, "^", "\\^", -1)
60
	q = strings.Replace(q, "$", "\\$", -1)
61
	return q
62
}
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.