Skip to content

Commit

Permalink
Add generate command
Browse files Browse the repository at this point in the history
A command that reads a folder with ServiceLevelObjectives and output files with the corresponding Prometheus Rules

Signed-off-by: ArthurSens <arthursens2005@gmail.com>
  • Loading branch information
ArthurSens committed Jul 6, 2022
1 parent 3730405 commit dc21dc3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 44 deletions.
92 changes: 48 additions & 44 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,53 +170,11 @@ func cmdFilesystem(logger log.Logger, reg *prometheus.Registry, promClient api.C
level.Debug(logger).Log("msg", "reading", "file", f)
reconcilesTotal.Inc()

bytes, err := ioutil.ReadFile(f)
objective, err := objectiveAsRuleFile(f, prometheusFolder)
if err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to read file %q: %w", f, err)
return fmt.Errorf("failed to create rule file %q: %w", f, err)
}

var config v1alpha1.ServiceLevelObjective
if err := yaml.UnmarshalStrict(bytes, &config); err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to unmarshal objective %q: %w", f, err)
}

objective, err := config.Internal()
if err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to get objective: %w", err)
}

increases, err := objective.IncreaseRules()
if err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to get increase rules: %w", err)
}
burnrates, err := objective.Burnrates()
if err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to get burn rate rules: %w", err)
}

rule := monitoringv1.PrometheusRuleSpec{
Groups: []monitoringv1.RuleGroup{increases, burnrates},
}

bytes, err = yaml.Marshal(rule)
if err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to marshal recording rules: %w", err)
}

_, file := filepath.Split(f)
path := filepath.Join(prometheusFolder, file)

if err := ioutil.WriteFile(path, bytes, 0o644); err != nil {
reconcilesErrors.Inc()
return fmt.Errorf("failed to write file %q: %w", path, err)
}

objectives.Set(objective)

reload <- struct{}{} // Trigger a Prometheus reload
Expand Down Expand Up @@ -353,3 +311,49 @@ func (f FilesystemObjectiveServer) GetREDRequests(ctx context.Context, expr, gro
func (f FilesystemObjectiveServer) GetREDErrors(ctx context.Context, expr, grouping string, i, i2 int32) (openapiserver.ImplResponse, error) {
return openapiserver.ImplResponse{}, errEndpointNotImplemented
}

// objectiveAsRuleFile reads a ServiceLevelObjective YAML manifest and outputs the corresponding
// Prometheus rules as a file in the desired directory.
func objectiveAsRuleFile(file, prometheusFolder string) (slo.Objective, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return slo.Objective{}, fmt.Errorf("failed to read file %q: %w", file, err)
}

var config v1alpha1.ServiceLevelObjective
if err := yaml.UnmarshalStrict(bytes, &config); err != nil {
return slo.Objective{}, fmt.Errorf("failed to unmarshal objective %q: %w", file, err)
}

if err != nil {
return slo.Objective{}, fmt.Errorf("failed to get objective: %w", err)
}

objective, err := config.Internal()
increases, err := objective.IncreaseRules()
if err != nil {
return slo.Objective{}, fmt.Errorf("failed to get increase rules: %w", err)
}
burnrates, err := objective.Burnrates()
if err != nil {
return slo.Objective{}, fmt.Errorf("failed to get burn rate rules: %w", err)
}

rule := monitoringv1.PrometheusRuleSpec{
Groups: []monitoringv1.RuleGroup{increases, burnrates},
}

bytes, err = yaml.Marshal(rule)
if err != nil {
return slo.Objective{}, fmt.Errorf("failed to marshal recording rules: %w", err)
}

_, f := filepath.Split(file)
path := filepath.Join(prometheusFolder, f)

if err := ioutil.WriteFile(path, bytes, 0o644); err != nil {
return slo.Objective{}, fmt.Errorf("failed to write file %q: %w", path, err)
}

return objective, nil
}
43 changes: 43 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2021 Pyrra Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"io/ioutil"
"path/filepath"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
)

func cmdGenerate(logger log.Logger, configFiles, prometheusFolder string) int {
fs, err := ioutil.ReadDir(configFiles)
if err != nil {
level.Error(logger).Log("msg", "failed to read config-files directory", "err", err)
return 1
}
for _, file := range fs {
if !file.IsDir() {
_, err := objectiveAsRuleFile(filepath.Join(configFiles, file.Name()), prometheusFolder)
if err != nil {
level.Error(logger).Log("msg", "failed generating rule files", "err", err)
return 1
}
}
}
return 0
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var CLI struct {
MetricsAddr string `default:":8080" help:"The address the metric endpoint binds to."`
ConfigMapMode bool `default:"false" help:"If the generated recording rules should instead be saved to config maps in the default Prometheus format."`
} `cmd:"" help:"Runs Pyrra's Kubernetes operator and backend for the API."`
Generate struct {
ConfigFiles string `default:"/etc/pyrra/*.yaml" help:"The folder where Pyrra finds the config files to use."`
PrometheusFolder string `default:"/etc/prometheus/pyrra/" help:"The folder where Pyrra writes the generates Prometheus rules and alerts."`
} `cmd:"" help:"Read SLO config files and rewrites them as Prometheus rules and alerts."`
}

func main() {
Expand Down Expand Up @@ -126,6 +130,8 @@ func main() {
code = cmdFilesystem(logger, reg, client, CLI.Filesystem.ConfigFiles, CLI.Filesystem.PrometheusFolder)
case "kubernetes":
code = cmdKubernetes(logger, CLI.Kubernetes.MetricsAddr, CLI.Kubernetes.ConfigMapMode)
case "generate":
code = cmdGenerate(logger, CLI.Generate.ConfigFiles, CLI.Generate.PrometheusFolder)
}
os.Exit(code)
}
Expand Down

0 comments on commit dc21dc3

Please sign in to comment.