pangolin_exporter
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.
13package collector
14
15import (
16"strings"
17
18"github.com/prometheus/client_golang/prometheus"
19dto "github.com/prometheus/client_model/go"
20)
21
22type labelMap map[string]string
23
24type MetricResult struct {
25labels labelMap
26value float64
27metricType dto.MetricType
28}
29
30func readMetric(m prometheus.Metric) MetricResult {
31pb := &dto.Metric{}
32m.Write(pb)
33labels := make(labelMap, len(pb.Label))
34for _, v := range pb.Label {
35labels[v.GetName()] = v.GetValue()
36}
37if pb.Gauge != nil {
38return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE}
39}
40if pb.Counter != nil {
41return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER}
42}
43if pb.Untyped != nil {
44return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED}
45}
46panic("Unsupported metric type")
47}
48
49func sanitizeQuery(q string) string {
50q = strings.Join(strings.Fields(q), " ")
51q = strings.Replace(q, "(", "\\(", -1)
52q = strings.Replace(q, "?", "\\?", -1)
53q = strings.Replace(q, ")", "\\)", -1)
54q = strings.Replace(q, "[", "\\[", -1)
55q = strings.Replace(q, "]", "\\]", -1)
56q = strings.Replace(q, "{", "\\{", -1)
57q = strings.Replace(q, "}", "\\}", -1)
58q = strings.Replace(q, "*", "\\*", -1)
59q = strings.Replace(q, "^", "\\^", -1)
60q = strings.Replace(q, "$", "\\$", -1)
61return q
62}
63