Skip to content

Commit bfc59bb

Browse files
authored
[addon-operator] Pass metrics storage in constructor (#699)
Signed-off-by: Pavel Okhlopkov <[email protected]>
1 parent 5f385be commit bfc59bb

File tree

25 files changed

+695
-218
lines changed

25 files changed

+695
-218
lines changed

cmd/addon-operator/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import (
1717
addon_operator "github.com/flant/addon-operator/pkg/addon-operator"
1818
"github.com/flant/addon-operator/pkg/app"
1919
"github.com/flant/addon-operator/pkg/kube_config_manager/backend/configmap"
20+
"github.com/flant/addon-operator/pkg/metrics"
2021
"github.com/flant/addon-operator/pkg/utils/stdliblogtolog"
2122
"github.com/flant/kube-client/klogtolog"
2223
shapp "github.com/flant/shell-operator/pkg/app"
2324
"github.com/flant/shell-operator/pkg/debug"
25+
shmetrics "github.com/flant/shell-operator/pkg/metrics"
2426
utils_signal "github.com/flant/shell-operator/pkg/utils/signal"
2527
)
2628

@@ -71,7 +73,12 @@ func start(logger *log.Logger) func(_ *kingpin.ParseContext) error {
7173

7274
ctx := context.Background()
7375

74-
operator := addon_operator.NewAddonOperator(ctx, addon_operator.WithLogger(logger.Named("addon-operator")))
76+
// Initialize metric names with the configured prefix
77+
shmetrics.InitMetrics(shapp.PrometheusMetricsPrefix)
78+
// Initialize addon-operator specific metrics
79+
metrics.InitMetrics(shapp.PrometheusMetricsPrefix)
80+
81+
operator := addon_operator.NewAddonOperator(ctx, nil, nil, addon_operator.WithLogger(logger.Named("addon-operator")))
7582

7683
operator.StartAPIServer()
7784

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.24.0
55
require (
66
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
77
github.com/deckhouse/deckhouse/pkg/log v0.1.0
8-
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.2.1
8+
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0
99
github.com/deckhouse/module-sdk v0.5.0
1010
github.com/dominikbraun/graph v0.23.0
1111
github.com/ettle/strcase v0.2.0
1212
github.com/flant/kube-client v1.5.0
13-
github.com/flant/shell-operator v1.12.0
13+
github.com/flant/shell-operator v1.12.1
1414
github.com/go-chi/chi/v5 v5.2.2
1515
github.com/go-openapi/loads v0.23.1
1616
github.com/go-openapi/spec v0.22.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
100100
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
101101
github.com/deckhouse/deckhouse/pkg/log v0.1.0 h1:2aPfyiHHSIJlX4x7ysyPOaIb7CLmyY+hUf9uDb8TYd8=
102102
github.com/deckhouse/deckhouse/pkg/log v0.1.0/go.mod h1:pbAxTSDcPmwyl3wwKDcEB3qdxHnRxqTV+J0K+sha8bw=
103-
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.2.1 h1:DhMHRzmaVNXASH+koIy9gK25GU/EaK+zC6sVpacqWRw=
104-
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.2.1/go.mod h1:Rz++SzCLkFW03WGgftnn91TimGU2shiKb5S/YuxcBuE=
103+
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0 h1:xZvbKuexrSQGEw6CB4n3UC7XbOb9QNLbm8UhcGZ2R1I=
104+
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0/go.mod h1:Rz++SzCLkFW03WGgftnn91TimGU2shiKb5S/YuxcBuE=
105105
github.com/deckhouse/module-sdk v0.5.0 h1:b2GJUzMKQLr7oJVJy5lXHvyymNyvNiFXpBie7MwEWwE=
106106
github.com/deckhouse/module-sdk v0.5.0/go.mod h1:+EbBnP8z+poIihgL4l1oxHng5ePqDUK44c39u7sEBss=
107107
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
@@ -155,8 +155,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
155155
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
156156
github.com/flant/kube-client v1.5.0 h1:6QZOZy3uk58Bh9YUn4CnhEz13og/cEGXB2uBZ1gWwtM=
157157
github.com/flant/kube-client v1.5.0/go.mod h1:hpJZ0FnDKHW3r5q5SYQgBrTw9k94q4+dcnJ4uOGYBHc=
158-
github.com/flant/shell-operator v1.12.0 h1:VAi6EEqG5aCKgPlE+XaXmpdG5dvGPUkrHSstAREvSiU=
159-
github.com/flant/shell-operator v1.12.0/go.mod h1:MlEVeHh88sL5LCiMmLmtYJZSV7XL/WHIJBKJeNviExE=
158+
github.com/flant/shell-operator v1.12.1 h1:ARrZaPiiQVjPjXombUt//doaHFCBlzGATgsUpQHT85o=
159+
github.com/flant/shell-operator v1.12.1/go.mod h1:RFXNvfK/v18J5u4RMqfFSDFhwJvMNPv5OR3BpioyxI0=
160160
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
161161
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
162162
github.com/fluxcd/flagger v1.36.1 h1:X2PumtNwZz9YSGaOtZLFm2zAKLgHhFkbNv8beg7ifyc=

pkg/addon-operator/bootstrap.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/flant/addon-operator/pkg/app"
1010
"github.com/flant/addon-operator/pkg/kube_config_manager"
1111
"github.com/flant/addon-operator/pkg/kube_config_manager/backend"
12+
"github.com/flant/addon-operator/pkg/metrics"
1213
"github.com/flant/addon-operator/pkg/module_manager"
1314
taskservice "github.com/flant/addon-operator/pkg/task/service"
1415
shapp "github.com/flant/shell-operator/pkg/app"
@@ -75,8 +76,8 @@ func (op *AddonOperator) Assemble(debugServer *debug.Server) error {
7576
}
7677

7778
// Start background updaters for metrics
78-
StartLiveTicksUpdater(op.engine.MetricStorage)
79-
StartTasksQueueLengthUpdater(op.engine.MetricStorage, op.engine.TaskQueues)
79+
metrics.StartLiveTicksUpdater(op.engine.MetricStorage)
80+
metrics.StartTasksQueueLengthUpdater(op.engine.MetricStorage, op.engine.TaskQueues)
8081

8182
// Register debug HTTP endpoints to inspect internal state
8283
op.engine.RegisterDebugQueueRoutes(debugServer)

pkg/addon-operator/handler_module_manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/flant/addon-operator/pkg/addon-operator/converge"
1212
"github.com/flant/addon-operator/pkg/kube_config_manager/config"
13+
"github.com/flant/addon-operator/pkg/metrics"
1314
dynamic_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled"
1415
"github.com/flant/addon-operator/pkg/task"
1516
"github.com/flant/addon-operator/pkg/utils"
@@ -196,13 +197,13 @@ func (op *AddonOperator) StartModuleManagerEventHandler() {
196197
additionalDescription := fmt.Sprintf("%d absent module resources", len(HelmReleaseStatusEvent.Absent))
197198
// helm reslease in unexpected state event
198199
if HelmReleaseStatusEvent.UnexpectedStatus {
199-
op.engine.MetricStorage.CounterAdd("{PREFIX}modules_helm_release_redeployed_total", 1.0, map[string]string{"module": HelmReleaseStatusEvent.ModuleName})
200+
op.engine.MetricStorage.CounterAdd(metrics.ModulesHelmReleaseRedeployedTotal, 1.0, map[string]string{"module": HelmReleaseStatusEvent.ModuleName})
200201
eventDescription = "HelmReleaseUnexpectedStatus"
201202
additionalDescription = "unexpected helm release status"
202203
} else {
203204
// some resources are missing and metrics are provided
204205
for _, manifest := range HelmReleaseStatusEvent.Absent {
205-
op.engine.MetricStorage.CounterAdd("{PREFIX}modules_absent_resources_total", 1.0, map[string]string{"module": HelmReleaseStatusEvent.ModuleName, "resource": fmt.Sprintf("%s/%s/%s", manifest.Namespace(""), manifest.Kind(), manifest.Name())})
206+
op.engine.MetricStorage.CounterAdd(metrics.ModulesAbsentResourcesTotal, 1.0, map[string]string{"module": HelmReleaseStatusEvent.ModuleName, "resource": fmt.Sprintf("%s/%s/%s", manifest.Namespace(""), manifest.Kind(), manifest.Name())})
206207
}
207208
}
208209

pkg/addon-operator/metrics.go

Lines changed: 0 additions & 160 deletions
This file was deleted.

pkg/addon-operator/operator.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/flant/addon-operator/pkg/helm_resources_manager"
2222
"github.com/flant/addon-operator/pkg/kube_config_manager"
2323
"github.com/flant/addon-operator/pkg/kube_config_manager/config"
24+
"github.com/flant/addon-operator/pkg/metrics"
2425
"github.com/flant/addon-operator/pkg/module_manager"
2526
gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook"
2627
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
@@ -117,7 +118,7 @@ func WithOnConvergeFinish(callback func()) Option {
117118
}
118119
}
119120

120-
func NewAddonOperator(ctx context.Context, opts ...Option) *AddonOperator {
121+
func NewAddonOperator(ctx context.Context, metricsStorage, hookMetricStorage metricsstorage.Storage, opts ...Option) *AddonOperator {
121122
cctx, cancel := context.WithCancel(ctx)
122123

123124
ao := &AddonOperator{
@@ -136,7 +137,24 @@ func NewAddonOperator(ctx context.Context, opts ...Option) *AddonOperator {
136137
ao.Logger = log.NewLogger().Named("addon-operator")
137138
}
138139

139-
so := shell_operator.NewShellOperator(cctx, shell_operator.WithLogger(ao.Logger.Named("shell-operator")))
140+
if metricsStorage == nil {
141+
ao.Logger.Warn("MetricStorage is not provided, creating a new one")
142+
143+
metricsStorage = metricsstorage.NewMetricStorage(
144+
metricsstorage.WithLogger(ao.Logger.Named("metric-storage")),
145+
)
146+
}
147+
148+
if hookMetricStorage == nil {
149+
ao.Logger.Warn("HookMetricStorage is not provided, creating a new one")
150+
151+
hookMetricStorage = metricsstorage.NewMetricStorage(
152+
metricsstorage.WithNewRegistry(),
153+
metricsstorage.WithLogger(ao.Logger.Named("hook-metric-storage")),
154+
)
155+
}
156+
157+
so := shell_operator.NewShellOperator(cctx, metricsStorage, hookMetricStorage, shell_operator.WithLogger(ao.Logger.Named("shell-operator")))
140158

141159
// initialize logging before Assemble
142160
rc := runtimeConfig.NewConfig(ao.Logger)
@@ -155,7 +173,10 @@ func NewAddonOperator(ctx context.Context, opts ...Option) *AddonOperator {
155173
panic(err)
156174
}
157175

158-
registerHookMetrics(so.HookMetricStorage)
176+
// Register addon-operator specific metrics
177+
if err := metrics.RegisterHookMetrics(so.HookMetricStorage); err != nil {
178+
panic(fmt.Errorf("register hook metrics: %w", err))
179+
}
159180

160181
labelSelector, err := metav1.ParseToLabelSelector(app.ExtraLabels)
161182
if err != nil {

pkg/addon-operator/operator_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func assembleTestAddonOperator(t *testing.T, configPath string) (*AddonOperator,
110110
prometheus.DefaultRegisterer = registry
111111

112112
// Assemble AddonOperator.
113-
op := NewAddonOperator(context.Background(), WithLogger(log.NewNop()))
113+
op := NewAddonOperator(context.Background(), nil, nil, WithLogger(log.NewNop()))
114114
op.engine.KubeClient = kubeClient
115115
// Mock helm client for ModuleManager
116116
result.helmClient = &mockhelm.Client{}
@@ -139,12 +139,10 @@ func assembleTestAddonOperator(t *testing.T, configPath string) (*AddonOperator,
139139
Helm: op.Helm,
140140
HelmResourcesManager: op.HelmResourcesManager,
141141
MetricStorage: metricstorage.NewMetricStorage(
142-
metricstorage.WithPrefix("addon_operator_"),
143142
metricstorage.WithLogger(log.NewNop()),
144143
metricstorage.WithNewRegistry(),
145144
),
146145
HookMetricStorage: metricstorage.NewMetricStorage(
147-
metricstorage.WithPrefix("addon_operator_"),
148146
metricstorage.WithLogger(log.NewNop()),
149147
metricstorage.WithNewRegistry(),
150148
),

0 commit comments

Comments
 (0)