Skip to content

Commit 8e06a06

Browse files
committed
feat: otel - init
1 parent dcb16dc commit 8e06a06

File tree

7 files changed

+162
-247
lines changed

7 files changed

+162
-247
lines changed

internal/clientlite/dtclient.go

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

internal/clientlite/dtclient_test.go

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

internal/controller/compoundmetric_controller.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
insight "github.com/SAP/metrics-operator/api/v1alpha1"
3434
"github.com/SAP/metrics-operator/api/v1beta1"
35+
"github.com/SAP/metrics-operator/internal/clientoptl" // Added
3536
"github.com/SAP/metrics-operator/internal/common"
3637
orc "github.com/SAP/metrics-operator/internal/orchestrator"
3738
)
@@ -144,10 +145,26 @@ func (r *CompoundMetricReconciler) Reconcile(ctx context.Context, req ctrl.Reque
144145
return ctrl.Result{RequeueAfter: RequeueAfterError}, err
145146
}
146147

148+
metricClient, errCli := clientoptl.NewMetricClient(ctx, credentials.Host, credentials.Path, credentials.Token)
149+
if errCli != nil {
150+
l.Error(errCli, fmt.Sprintf("compound metric '%s' failed to create OTel client, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
151+
// TODO: Update status?
152+
return ctrl.Result{RequeueAfter: RequeueAfterError}, errCli
153+
}
154+
defer metricClient.Close(ctx) // Ensure exporter is shut down
155+
156+
metricClient.SetMeter("compound")
157+
158+
gaugeMetric, errGauge := metricClient.NewMetric(metric.Name)
159+
if errGauge != nil {
160+
l.Error(errGauge, fmt.Sprintf("compound metric '%s' failed to create OTel gauge, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
161+
// TODO: Update status?
162+
return ctrl.Result{RequeueAfter: RequeueAfterError}, errGauge
163+
}
147164
/*
148165
2. Create a new orchestrator
149166
*/
150-
orchestrator, errOrch := orc.NewOrchestrator(credentials, queryConfig).WithCompound(metric)
167+
orchestrator, errOrch := orc.NewOrchestrator(credentials, queryConfig).WithCompound(metric, gaugeMetric) // Pass gaugeMetric
151168
if errOrch != nil {
152169
l.Error(errOrch, "unable to create compound metric orchestrator monitor")
153170
r.Recorder.Event(&metric, "Warning", "OrchestratorCreation", "unable to create orchestrator")
@@ -157,10 +174,21 @@ func (r *CompoundMetricReconciler) Reconcile(ctx context.Context, req ctrl.Reque
157174
result, errMon := orchestrator.Handler.Monitor(ctx)
158175

159176
if errMon != nil {
177+
metric.Status.Ready = "False"
160178
l.Error(errMon, fmt.Sprintf("compound metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
179+
// Update status before returning
180+
_ = r.getClient().Status().Update(ctx, &metric) // Best effort status update on error
161181
return ctrl.Result{RequeueAfter: RequeueAfterError}, errMon
162182
}
163183

184+
errExport := metricClient.ExportMetrics(ctx)
185+
if errExport != nil {
186+
metric.Status.Ready = "False"
187+
l.Error(errExport, fmt.Sprintf("compound metric '%s' failed to export, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
188+
} else {
189+
metric.Status.Ready = "True"
190+
}
191+
164192
/*
165193
3. Update the status of the metric with conditions and phase
166194
*/
@@ -179,7 +207,10 @@ func (r *CompoundMetricReconciler) Reconcile(ctx context.Context, req ctrl.Reque
179207

180208
cObs := result.Observation.(*v1beta1.MetricObservation)
181209

182-
metric.Status.Ready = boolToString(result.Phase == insight.PhaseActive)
210+
// Override Ready status if export failed
211+
if errExport != nil {
212+
metric.Status.Ready = "False"
213+
}
183214
metric.Status.Observation = v1beta1.MetricObservation{Timestamp: result.Observation.GetTimestamp(), Dimensions: cObs.Dimensions, LatestValue: cObs.LatestValue}
184215

185216
// Update LastReconcileTime
@@ -189,21 +220,21 @@ func (r *CompoundMetricReconciler) Reconcile(ctx context.Context, req ctrl.Reque
189220
// conditions are not persisted until the status is updated
190221
errUp := r.getClient().Status().Update(ctx, &metric)
191222
if errUp != nil {
192-
l.Error(errMon, fmt.Sprintf("generic metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
223+
l.Error(errUp, fmt.Sprintf("compound metric '%s' failed to update status, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
193224
return ctrl.Result{RequeueAfter: RequeueAfterError}, errUp
194225
}
195226

196227
/*
197228
4. Requeue the metric after the frequency or after 2 minutes if an error occurred
198229
*/
199230
var requeueTime time.Duration
200-
if result.Error != nil {
231+
if result.Error != nil || errExport != nil { // Requeue faster on monitor or export error
201232
requeueTime = RequeueAfterError
202233
} else {
203234
requeueTime = metric.Spec.CheckInterval.Duration
204235
}
205236

206-
l.Info(fmt.Sprintf("generic metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, requeueTime))
237+
l.Info(fmt.Sprintf("compound metric '%s' re-queued for execution in %v\n", metric.Spec.Name, requeueTime))
207238

208239
return ctrl.Result{
209240
Requeue: true,

internal/controller/singlemetric_controller.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
insight "github.com/SAP/metrics-operator/api/v1alpha1"
3434
"github.com/SAP/metrics-operator/api/v1beta1"
35+
"github.com/SAP/metrics-operator/internal/clientoptl" // Added
3536
"github.com/SAP/metrics-operator/internal/common"
3637
"github.com/SAP/metrics-operator/internal/config"
3738
orc "github.com/SAP/metrics-operator/internal/orchestrator"
@@ -145,10 +146,26 @@ func (r *SingleMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request
145146
return ctrl.Result{RequeueAfter: RequeueAfterError}, err
146147
}
147148

149+
metricClient, errCli := clientoptl.NewMetricClient(ctx, credentials.Host, credentials.Path, credentials.Token)
150+
if errCli != nil {
151+
l.Error(errCli, fmt.Sprintf("single metric '%s' failed to create OTel client, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
152+
// TODO: Update status?
153+
return ctrl.Result{RequeueAfter: RequeueAfterError}, errCli
154+
}
155+
defer metricClient.Close(ctx) // Ensure exporter is shut down
156+
157+
metricClient.SetMeter("single")
158+
159+
gaugeMetric, errGauge := metricClient.NewMetric(metric.Name)
160+
if errGauge != nil {
161+
l.Error(errGauge, fmt.Sprintf("single metric '%s' failed to create OTel gauge, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
162+
// TODO: Update status?
163+
return ctrl.Result{RequeueAfter: RequeueAfterError}, errGauge
164+
}
148165
/*
149166
2. Create a new orchestrator
150167
*/
151-
orchestrator, errOrch := orc.NewOrchestrator(credentials, queryConfig).WithSingle(metric)
168+
orchestrator, errOrch := orc.NewOrchestrator(credentials, queryConfig).WithSingle(metric, gaugeMetric) // Pass gaugeMetric
152169
if errOrch != nil {
153170
l.Error(errOrch, "unable to create single metric orchestrator monitor")
154171
r.Recorder.Event(&metric, "Warning", "OrchestratorCreation", "unable to create orchestrator")
@@ -158,10 +175,21 @@ func (r *SingleMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request
158175
result, errMon := orchestrator.Handler.Monitor(ctx)
159176

160177
if errMon != nil {
178+
metric.Status.Ready = "False"
161179
l.Error(errMon, fmt.Sprintf("single metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
180+
// Update status before returning
181+
_ = r.getClient().Status().Update(ctx, &metric) // Best effort status update on error
162182
return ctrl.Result{RequeueAfter: RequeueAfterError}, errMon
163183
}
164184

185+
errExport := metricClient.ExportMetrics(ctx)
186+
if errExport != nil {
187+
metric.Status.Ready = "False"
188+
l.Error(errExport, fmt.Sprintf("single metric '%s' failed to export, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
189+
} else {
190+
metric.Status.Ready = "True"
191+
}
192+
165193
/*
166194
3. Update the status of the metric with conditions and phase
167195
*/
@@ -178,7 +206,10 @@ func (r *SingleMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request
178206
r.Recorder.Event(&metric, "Normal", "MetricPending", result.Message)
179207
}
180208

181-
metric.Status.Ready = boolToString(result.Phase == insight.PhaseActive)
209+
// Override Ready status if export failed
210+
if errExport != nil {
211+
metric.Status.Ready = "False"
212+
}
182213
metric.Status.Observation = v1beta1.MetricObservation{Timestamp: result.Observation.GetTimestamp(), LatestValue: result.Observation.GetValue()}
183214

184215
// Update LastReconcileTime
@@ -188,21 +219,21 @@ func (r *SingleMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request
188219
// conditions are not persisted until the status is updated
189220
errUp := r.getClient().Status().Update(ctx, &metric)
190221
if errUp != nil {
191-
l.Error(errMon, fmt.Sprintf("generic metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
222+
l.Error(errUp, fmt.Sprintf("single metric '%s' failed to update status, re-queued for execution in %v minutes\n", metric.Spec.Name, RequeueAfterError))
192223
return ctrl.Result{RequeueAfter: RequeueAfterError}, errUp
193224
}
194225

195226
/*
196227
4. Requeue the metric after the frequency or after 2 minutes if an error occurred
197228
*/
198229
var requeueTime time.Duration
199-
if result.Error != nil {
230+
if result.Error != nil || errExport != nil { // Requeue faster on monitor or export error
200231
requeueTime = RequeueAfterError
201232
} else {
202233
requeueTime = metric.Spec.CheckInterval.Duration
203234
}
204235

205-
l.Info(fmt.Sprintf("generic metric '%s' re-queued for execution in %v minutes\n", metric.Spec.Name, requeueTime))
236+
l.Info(fmt.Sprintf("single metric '%s' re-queued for execution in %v\n", metric.Spec.Name, requeueTime))
206237

207238
return ctrl.Result{
208239
Requeue: true,

0 commit comments

Comments
 (0)