@@ -171,68 +171,17 @@ func cmdFilesystem(logger log.Logger, reg *prometheus.Registry, promClient api.C
171171 level .Debug (logger ).Log ("msg" , "reading" , "file" , f )
172172 reconcilesTotal .Inc ()
173173
174- bytes , err := os . ReadFile ( f )
174+ err := writeRuleFile ( logger , f , prometheusFolder , genericRules )
175175 if err != nil {
176176 reconcilesErrors .Inc ()
177- return fmt . Errorf ( "failed to read file %q: %w " , f , err )
177+ level . Error ( logger ). Log ( "msg" , "error creating rule file " , "file" , f )
178178 }
179179
180- var config v1alpha1.ServiceLevelObjective
181- if err := yaml .UnmarshalStrict (bytes , & config ); err != nil {
182- reconcilesErrors .Inc ()
183- return fmt .Errorf ("failed to unmarshal objective %q: %w" , f , err )
184- }
185-
186- objective , err := config .Internal ()
187- if err != nil {
188- reconcilesErrors .Inc ()
189- return fmt .Errorf ("failed to get objective: %w" , err )
190- }
191-
192- increases , err := objective .IncreaseRules ()
193- if err != nil {
194- reconcilesErrors .Inc ()
195- return fmt .Errorf ("failed to get increase rules: %w" , err )
196- }
197- burnrates , err := objective .Burnrates ()
198- if err != nil {
199- reconcilesErrors .Inc ()
200- return fmt .Errorf ("failed to get burn rate rules: %w" , err )
201- }
202-
203- rule := monitoringv1.PrometheusRuleSpec {
204- Groups : []monitoringv1.RuleGroup {increases , burnrates },
205- }
206-
207- if genericRules {
208- rules , err := objective .GenericRules ()
209- if err == nil {
210- rule .Groups = append (rule .Groups , rules )
211- } else {
212- if err != slo .ErrGroupingUnsupported {
213- return fmt .Errorf ("failed to get generic rules: %w" , err )
214- }
215- level .Warn (logger ).Log (
216- "msg" , "objective with grouping unsupported with generic rules" ,
217- "objective" , objective .Name (),
218- )
219- }
220- }
221-
222- bytes , err = yaml .Marshal (rule )
180+ objective , err := objectiveFromFile (f )
223181 if err != nil {
224182 reconcilesErrors .Inc ()
225- return fmt .Errorf ("failed to marshal recording rules: %w" , err )
226- }
227-
228- _ , file := filepath .Split (f )
229- path := filepath .Join (prometheusFolder , file )
230-
231- if err := os .WriteFile (path , bytes , 0o644 ); err != nil {
232- reconcilesErrors .Inc ()
233- return fmt .Errorf ("failed to write file %q: %w" , path , err )
183+ level .Error (logger ).Log ("msg" , "failed to get objective from file" , "file" , f )
234184 }
235-
236185 objectives .Set (objective )
237186
238187 reload <- struct {}{} // Trigger a Prometheus reload
@@ -343,3 +292,65 @@ func (s *FilesystemObjectiveServer) List(ctx context.Context, req *connect.Reque
343292 Objectives : objectives ,
344293 }), nil
345294}
295+
296+ func writeRuleFile (logger log.Logger , file , prometheusFolder string , genericRules bool ) error {
297+ objective , err := objectiveFromFile (file )
298+ if err != nil {
299+ return fmt .Errorf ("failed to get objective: %w" , err )
300+ }
301+
302+ increases , err := objective .IncreaseRules ()
303+ if err != nil {
304+ return fmt .Errorf ("failed to get increase rules: %w" , err )
305+ }
306+ burnrates , err := objective .Burnrates ()
307+ if err != nil {
308+ return fmt .Errorf ("failed to get burn rate rules: %w" , err )
309+ }
310+
311+ rule := monitoringv1.PrometheusRuleSpec {
312+ Groups : []monitoringv1.RuleGroup {increases , burnrates },
313+ }
314+
315+ if genericRules {
316+ rules , err := objective .GenericRules ()
317+ if err == nil {
318+ rule .Groups = append (rule .Groups , rules )
319+ } else {
320+ if err != slo .ErrGroupingUnsupported {
321+ return fmt .Errorf ("failed to get generic rules: %w" , err )
322+ }
323+ level .Warn (logger ).Log (
324+ "msg" , "objective with grouping unsupported with generic rules" ,
325+ "objective" , objective .Name (),
326+ )
327+ }
328+ }
329+
330+ bytes , err := yaml .Marshal (rule )
331+ if err != nil {
332+ return fmt .Errorf ("failed to marshal recording rules: %w" , err )
333+ }
334+
335+ _ , f := filepath .Split (file )
336+ path := filepath .Join (prometheusFolder , f )
337+
338+ if err := os .WriteFile (path , bytes , 0o644 ); err != nil {
339+ return fmt .Errorf ("failed to write file %q: %w" , path , err )
340+ }
341+ return nil
342+ }
343+
344+ func objectiveFromFile (file string ) (slo.Objective , error ) {
345+ bytes , err := os .ReadFile (file )
346+ if err != nil {
347+ return slo.Objective {}, fmt .Errorf ("failed to read file %q: %w" , file , err )
348+ }
349+
350+ var config v1alpha1.ServiceLevelObjective
351+ if err := yaml .UnmarshalStrict (bytes , & config ); err != nil {
352+ return slo.Objective {}, fmt .Errorf ("failed to unmarshal objective %q: %w" , file , err )
353+ }
354+
355+ return config .Internal ()
356+ }
0 commit comments