Skip to content

Commit 443e565

Browse files
FyuselGokceGK
andauthored
fix(observability): Metrics retention was not checked correctly (#1148)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de> Co-authored-by: Gökce Gök Klingel <161626272+GokceGK@users.noreply.github.com>
1 parent 3f6d273 commit 443e565

File tree

1 file changed

+41
-28
lines changed
  • stackit/internal/services/observability/instance

1 file changed

+41
-28
lines changed

stackit/internal/services/observability/instance/resource.go

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -908,13 +908,20 @@ func (r *instanceResource) ModifyPlan(ctx context.Context, req resource.ModifyPl
908908
}
909909
}
910910

911+
// Plan does not support metrics retention
912+
if plan.GetTotalMetricSamples() == 0 {
913+
metricsRetentionDays := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays)
914+
metricsRetentionDays5mDownsampling := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays5mDownsampling)
915+
metricsRetentionDays1hDownsampling := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays1hDownsampling)
916+
if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil {
917+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error validating plan", fmt.Sprintf("Plan (%s) does not support configuring metrics retention days. Remove this from your config or use a different plan.", *plan.Name))
918+
}
919+
}
920+
911921
// Plan does not support log storage and trace storage
912922
if plan.GetLogsStorage() == 0 && plan.GetTracesStorage() == 0 {
913923
logsRetentionDays := conversion.Int64ValueToPointer(configModel.LogsRetentionDays)
914924
tracesRetentionDays := conversion.Int64ValueToPointer(configModel.TracesRetentionDays)
915-
metricsRetentionDays := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays)
916-
metricsRetentionDays5mDownsampling := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays5mDownsampling)
917-
metricsRetentionDays1hDownsampling := conversion.Int64ValueToPointer(configModel.MetricsRetentionDays1hDownsampling)
918925
// If logs retention days are set, return an error to the user
919926
if logsRetentionDays != nil {
920927
resp.Diagnostics.AddAttributeError(path.Root("logs_retention_days"), "Error validating plan", fmt.Sprintf("Plan (%s) does not support configuring logs retention days. Remove this from your config or use a different plan.", *plan.Name))
@@ -923,10 +930,6 @@ func (r *instanceResource) ModifyPlan(ctx context.Context, req resource.ModifyPl
923930
if tracesRetentionDays != nil {
924931
resp.Diagnostics.AddAttributeError(path.Root("traces_retention_days"), "Error validating plan", fmt.Sprintf("Plan (%s) does not support configuring trace retention days. Remove this from your config or use a different plan.", *plan.Name))
925932
}
926-
// If any of the metrics retention days are set, return an error to the user
927-
if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil {
928-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error validating plan", fmt.Sprintf("Plan (%s) does not support configuring metrics retention days. Remove this from your config or use a different plan.", *plan.Name))
929-
}
930933
}
931934
}
932935

@@ -1030,9 +1033,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
10301033
return
10311034
}
10321035

1033-
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1034-
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
1035-
err := r.getMetricsRetention(ctx, &model)
1036+
// There are some plans which does not offer to set or get the metrics retention e.g. like Observability-Metrics-Endpoint-100k-EU01
1037+
if plan.GetTotalMetricSamples() != 0 {
1038+
err = r.getMetricsRetention(ctx, &model)
10361039
if err != nil {
10371040
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("%v", err))
10381041
}
@@ -1043,7 +1046,17 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
10431046
if resp.Diagnostics.HasError() {
10441047
return
10451048
}
1049+
} else {
1050+
// Set metric retention days to zero
1051+
diags = setMetricsRetentionsZero(ctx, &resp.State)
1052+
resp.Diagnostics.Append(diags...)
1053+
if resp.Diagnostics.HasError() {
1054+
return
1055+
}
1056+
}
10461057

1058+
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1059+
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
10471060
err = r.getLogsRetention(ctx, &model)
10481061
if err != nil {
10491062
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("%v", err))
@@ -1066,12 +1079,6 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
10661079
return
10671080
}
10681081
} else {
1069-
// Set metric retention days to zero
1070-
diags = setMetricsRetentionsZero(ctx, &resp.State)
1071-
resp.Diagnostics.Append(diags...)
1072-
if resp.Diagnostics.HasError() {
1073-
return
1074-
}
10751082
// Set logs retention days to zero
10761083
diags = setLogsRetentionsZero(ctx, &resp.State)
10771084
resp.Diagnostics.Append(diags...)
@@ -1178,8 +1185,8 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
11781185
return
11791186
}
11801187

1181-
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1182-
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
1188+
// There are some plans which does not offer to set or get the metrics retention e.g. like Observability-Metrics-Endpoint-100k-EU01
1189+
if plan.GetTotalMetricSamples() != 0 {
11831190
metricsRetentionResp, err := r.client.GetMetricsStorageRetention(ctx, instanceId, projectId).Execute()
11841191
if err != nil {
11851192
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get metrics retention: %v", err))
@@ -1197,7 +1204,10 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
11971204
if resp.Diagnostics.HasError() {
11981205
return
11991206
}
1207+
}
12001208

1209+
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1210+
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
12011211
logsRetentionResp, err := r.client.GetLogsConfigs(ctx, instanceId, projectId).Execute()
12021212
if err != nil {
12031213
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get logs retention: %v", err))
@@ -1377,9 +1387,9 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
13771387
return
13781388
}
13791389

1380-
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1381-
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
1382-
err := r.getMetricsRetention(ctx, &model)
1390+
// There are some plans which does not offer to set or get the metrics retention e.g. like Observability-Metrics-Endpoint-100k-EU01
1391+
if plan.GetTotalMetricSamples() != 0 {
1392+
err = r.getMetricsRetention(ctx, &model)
13831393
if err != nil {
13841394
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("%v", err))
13851395
}
@@ -1390,7 +1400,17 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
13901400
if resp.Diagnostics.HasError() {
13911401
return
13921402
}
1403+
} else {
1404+
// Set metric retention days to zero
1405+
diags = setMetricsRetentionsZero(ctx, &resp.State)
1406+
resp.Diagnostics.Append(diags...)
1407+
if resp.Diagnostics.HasError() {
1408+
return
1409+
}
1410+
}
13931411

1412+
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
1413+
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
13941414
err = r.getLogsRetention(ctx, &model)
13951415
if err != nil {
13961416
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("%v", err))
@@ -1413,13 +1433,6 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
14131433
return
14141434
}
14151435
} else {
1416-
// Set metric retention days to zero
1417-
diags = setMetricsRetentionsZero(ctx, &resp.State)
1418-
resp.Diagnostics.Append(diags...)
1419-
if resp.Diagnostics.HasError() {
1420-
return
1421-
}
1422-
14231436
diags = setLogsRetentionsZero(ctx, &resp.State)
14241437
resp.Diagnostics.Append(diags...)
14251438
if resp.Diagnostics.HasError() {

0 commit comments

Comments
 (0)