-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yaroslavborbat <[email protected]>
- Loading branch information
1 parent
1d8fa75
commit 174ce01
Showing
8 changed files
with
402 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
images/virtualization-artifact/pkg/monitoring/metrics/util/dynamic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
type dynamicMetric struct { | ||
desc *prometheus.Desc | ||
metric *dto.Metric | ||
} | ||
|
||
func (m dynamicMetric) Desc() *prometheus.Desc { | ||
return m.desc | ||
} | ||
|
||
func (m dynamicMetric) Write(out *dto.Metric) error { | ||
out.Label = m.metric.Label | ||
out.Counter = m.metric.Counter | ||
out.Gauge = m.metric.Gauge | ||
out.Untyped = m.metric.Untyped | ||
return nil | ||
} | ||
|
||
func NewDynamicMetric(desc *prometheus.Desc, valueType prometheus.ValueType, value float64, labelValues []string, extraLabels prometheus.Labels) (prometheus.Metric, error) { | ||
metric := &dto.Metric{} | ||
if err := populateMetric(valueType, value, makeLabelPairs(desc, labelValues, extraLabels), nil, metric, nil); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &dynamicMetric{ | ||
desc: desc, | ||
metric: metric, | ||
}, nil | ||
} | ||
|
||
func makeLabelPairs(desc *prometheus.Desc, labelValues []string, extraLabels prometheus.Labels) []*dto.LabelPair { | ||
pairs := prometheus.MakeLabelPairs(desc, labelValues) | ||
if extraLabels == nil { | ||
return pairs | ||
} | ||
for k, v := range extraLabels { | ||
pairs = append(pairs, &dto.LabelPair{ | ||
Name: proto.String(k), | ||
Value: proto.String(v), | ||
}) | ||
} | ||
return pairs | ||
} | ||
|
||
func populateMetric( | ||
t prometheus.ValueType, | ||
v float64, | ||
labelPairs []*dto.LabelPair, | ||
e *dto.Exemplar, | ||
m *dto.Metric, | ||
ct *timestamppb.Timestamp, | ||
) error { | ||
m.Label = labelPairs | ||
switch t { | ||
case prometheus.CounterValue: | ||
m.Counter = &dto.Counter{Value: proto.Float64(v), Exemplar: e, CreatedTimestamp: ct} | ||
case prometheus.GaugeValue: | ||
m.Gauge = &dto.Gauge{Value: proto.Float64(v)} | ||
case prometheus.UntypedValue: | ||
m.Untyped = &dto.Untyped{Value: proto.Float64(v)} | ||
default: | ||
return fmt.Errorf("encountered unknown type %v", t) | ||
} | ||
return nil | ||
} |
80 changes: 80 additions & 0 deletions
80
images/virtualization-artifact/pkg/monitoring/metrics/util/labels.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`) | ||
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") | ||
) | ||
|
||
type SkipLabel func(key, value string) bool | ||
|
||
func WrapPrometheusLabels(labels map[string]string, prefix string, skip SkipLabel) map[string]string { | ||
wrapLabels := make(map[string]string, len(labels)) | ||
conflicts := make(map[string]int, len(labels)) | ||
|
||
for k, v := range labels { | ||
if skip != nil && skip(k, v) { | ||
continue | ||
} | ||
labelKey := labelName(prefix, k) | ||
if conflictCount, ok := conflicts[labelKey]; ok { | ||
if conflictCount == 1 { | ||
// this is the first conflict for the label, | ||
// so we have to go back and rename the initial label that we've already added | ||
|
||
value := wrapLabels[labelKey] | ||
delete(wrapLabels, labelKey) | ||
wrapLabels[labelConflictSuffix(labelKey, conflictCount)] = value | ||
} | ||
conflicts[labelKey]++ | ||
labelKey = labelConflictSuffix(labelKey, conflicts[labelKey]) | ||
} else { | ||
conflicts[labelKey] = 1 | ||
} | ||
|
||
wrapLabels[labelKey] = v | ||
} | ||
return wrapLabels | ||
} | ||
|
||
func labelName(prefix, labelName string) string { | ||
return prefix + "_" + lintLabelName(SanitizeLabelName(labelName)) | ||
} | ||
|
||
func SanitizeLabelName(s string) string { | ||
return invalidLabelCharRE.ReplaceAllString(s, "_") | ||
} | ||
|
||
func lintLabelName(s string) string { | ||
return toSnakeCase(s) | ||
} | ||
|
||
func toSnakeCase(s string) string { | ||
snake := matchAllCap.ReplaceAllString(s, "${1}_${2}") | ||
return strings.ToLower(snake) | ||
} | ||
|
||
func labelConflictSuffix(label string, count int) string { | ||
return fmt.Sprintf("%s_conflict%d", label, count) | ||
} |
51 changes: 51 additions & 0 deletions
51
images/virtualization-artifact/pkg/monitoring/metrics/util/labels_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWrapPrometheusLabels(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
labels map[string]string | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "should resolve conflicted labels", | ||
labels: map[string]string{ | ||
"key-1": "value1", | ||
"key_1": "value1", | ||
"key-2": "value2", | ||
}, | ||
expected: map[string]string{ | ||
"label_key_1_conflict1": "value1", | ||
"label_key_1_conflict2": "value1", | ||
"label_key_2": "value2", | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
wrapped := WrapPrometheusLabels(test.labels, "label", nil) | ||
require.Equal(t, test.expected, wrapped) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.