Skip to content

Commit b133f22

Browse files
authored
[shell-operator] Add default group to every hook (#777)
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
1 parent 92e0495 commit b133f22

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

pkg/hook/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (h *Hook) Run(ctx context.Context, _ htypes.BindingType, context []bctx.Bin
175175
return result, fmt.Errorf("%s FAILED: %s", h.Name, err)
176176
}
177177

178-
result.Metrics, err = operation.MetricOperationsFromFile(metricsPath)
178+
result.Metrics, err = operation.MetricOperationsFromFile(metricsPath, h.Name)
179179
if err != nil {
180180
return result, fmt.Errorf("got bad metrics: %s", err)
181181
}

pkg/metric_storage/operation/operation.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (m MetricOperation) String() string {
5656
return "[" + strings.Join(parts, ", ") + "]"
5757
}
5858

59-
func MetricOperationsFromReader(r io.Reader) ([]MetricOperation, error) {
59+
func MetricOperationsFromReader(r io.Reader, defaultGroup string) ([]MetricOperation, error) {
6060
operations := make([]MetricOperation, 0)
6161

6262
dec := json.NewDecoder(r)
@@ -73,22 +73,27 @@ func MetricOperationsFromReader(r io.Reader) ([]MetricOperation, error) {
7373
metricOperation.Action = "set"
7474
metricOperation.Value = metricOperation.Set
7575
}
76+
7677
if metricOperation.Add != nil && metricOperation.Set == nil {
7778
metricOperation.Action = "add"
7879
metricOperation.Value = metricOperation.Add
7980
}
8081

82+
if metricOperation.Group == "" {
83+
metricOperation.Group = defaultGroup
84+
}
85+
8186
operations = append(operations, metricOperation)
8287
}
8388

8489
return operations, nil
8590
}
8691

87-
func MetricOperationsFromBytes(data []byte) ([]MetricOperation, error) {
88-
return MetricOperationsFromReader(bytes.NewReader(data))
92+
func MetricOperationsFromBytes(data []byte, defaultGroup string) ([]MetricOperation, error) {
93+
return MetricOperationsFromReader(bytes.NewReader(data), defaultGroup)
8994
}
9095

91-
func MetricOperationsFromFile(filePath string) ([]MetricOperation, error) {
96+
func MetricOperationsFromFile(filePath, defaultGroup string) ([]MetricOperation, error) {
9297
data, err := os.ReadFile(filePath)
9398
if err != nil {
9499
return nil, fmt.Errorf("cannot read %s: %s", filePath, err)
@@ -97,7 +102,7 @@ func MetricOperationsFromFile(filePath string) ([]MetricOperation, error) {
97102
if len(data) == 0 {
98103
return nil, nil
99104
}
100-
return MetricOperationsFromBytes(data)
105+
return MetricOperationsFromBytes(data, defaultGroup)
101106
}
102107

103108
func ValidateOperations(ops []MetricOperation) error {

pkg/metric_storage/operation/operation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Test_ValidateMetricOperations(t *testing.T) {
3939
t.Run(tt.name, func(t *testing.T) {
4040
g := NewWithT(t)
4141

42-
ops, err := MetricOperationsFromBytes([]byte(tt.op))
42+
ops, err := MetricOperationsFromBytes([]byte(tt.op), "")
4343
g.Expect(err).ShouldNot(HaveOccurred())
4444

4545
err = ValidateOperations(ops)

pkg/metric_storage/vault/vault.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vault
22

33
import (
44
"fmt"
5+
"log/slog"
56
"sync"
67

78
"github.com/deckhouse/deckhouse/pkg/log"
@@ -67,9 +68,11 @@ func (v *GroupedVault) GetOrCreateCounterCollector(name string, labelNames []str
6768
} else if !IsSubset(collector.LabelNames(), labelNames) {
6869
collector.UpdateLabels(labelNames)
6970
}
71+
7072
if counter, ok := collector.(*metric.ConstCounterCollector); ok {
7173
return counter, nil
7274
}
75+
7376
return nil, fmt.Errorf("counter %v collector requested, but %s %v collector exists", labelNames, collector.Type(), collector.LabelNames())
7477
}
7578

@@ -98,18 +101,34 @@ func (v *GroupedVault) CounterAdd(group string, name string, value float64, labe
98101
metricName := v.resolveMetricNameFunc(name)
99102
c, err := v.GetOrCreateCounterCollector(metricName, LabelNames(labels))
100103
if err != nil {
101-
log.Error("CounterAdd", log.Err(err))
104+
log.Error(
105+
"CounterAdd",
106+
slog.String("group", group),
107+
slog.String("name", name),
108+
slog.Any("labels", labels),
109+
log.Err(err),
110+
)
111+
102112
return
103113
}
114+
104115
c.Add(group, value, labels)
105116
}
106117

107118
func (v *GroupedVault) GaugeSet(group string, name string, value float64, labels map[string]string) {
108119
metricName := v.resolveMetricNameFunc(name)
109120
c, err := v.GetOrCreateGaugeCollector(metricName, LabelNames(labels))
110121
if err != nil {
111-
log.Error("GaugeSet", log.Err(err))
122+
log.Error(
123+
"GaugeSet",
124+
slog.String("group", group),
125+
slog.String("name", name),
126+
slog.Any("labels", labels),
127+
log.Err(err),
128+
)
129+
112130
return
113131
}
132+
114133
c.Set(group, value, labels)
115134
}

0 commit comments

Comments
 (0)