Skip to content

Commit

Permalink
refactor: add resiliency & concurrency and optimize code
Browse files Browse the repository at this point in the history
Fixes #3062

Signed-off-by: Anıl Mısırlıoğlu <[email protected]>
Co-authered-by: Furkan Türkal <[email protected]>
Co-authered-by: Ayşe Sert <[email protected]>
  • Loading branch information
anilmisirlioglu committed Nov 8, 2022
1 parent 7dfc2eb commit b4ce498
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 151 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
### Mimirtool

* [ENHANCEMENT] Added `mimirtool rules delete-namespace` command to delete all of the rule groups in a namespace including the namespace itself. #3136
* [ENHANCEMENT] Refactor `mimirtool analyze prometheus`: add concurrency and resiliency #3062
* Add `--concurrency` flag. Default: number of logical CPUs
* [BUGFIX] `--log.level=debug` now correctly prints the response from the remote endpoint when a request fails. #3180

### Documentation
Expand Down
4 changes: 2 additions & 2 deletions pkg/mimirtool/analyze/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (

"github.com/grafana/regexp"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql/parser"
log "github.com/sirupsen/logrus"

"github.com/grafana/mimir/pkg/mimirtool/minisdk"
)

type MetricsInGrafana struct {
MetricsUsed []string `json:"metricsUsed"`
MetricsUsed model.LabelValues `json:"metricsUsed"`
OverallMetrics map[string]struct{} `json:"-"`
Dashboards []DashboardMetrics `json:"dashboards"`
}
Expand Down Expand Up @@ -80,7 +81,6 @@ func ParseMetricsInBoard(mig *MetricsInGrafana, board minisdk.Board) {
Metrics: metricsInBoard,
ParseErrors: parseErrs,
})

}

func metricsFromTemplating(templating minisdk.Templating, metrics map[string]struct{}) []error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/mimirtool/analyze/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package analyze

type MetricsInPrometheus struct {
TotalActiveSeries int `json:"total_active_series"`
InUseActiveSeries int `json:"in_use_active_series"`
AdditionalActiveSeries int `json:"additional_active_series"`
TotalActiveSeries uint64 `json:"total_active_series"`
InUseActiveSeries uint64 `json:"in_use_active_series"`
AdditionalActiveSeries uint64 `json:"additional_active_series"`

InUseMetricCounts []MetricCount `json:"in_use_metric_counts"`
AdditionalMetricCounts []MetricCount `json:"additional_metric_counts"`
Expand Down
3 changes: 2 additions & 1 deletion pkg/mimirtool/analyze/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"sort"

"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql/parser"
log "github.com/sirupsen/logrus"

"github.com/grafana/mimir/pkg/mimirtool/rules/rwrulefmt"
)

type MetricsInRuler struct {
MetricsUsed []string `json:"metricsUsed"`
MetricsUsed model.LabelValues `json:"metricsUsed"`
OverallMetrics map[string]struct{} `json:"-"`
RuleGroups []RuleGroupMetrics `json:"ruleGroups"`
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/mimirtool/commands/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package commands

import (
"runtime"
"strconv"

"gopkg.in/alecthomas/kingpin.v2"
)

type AnalyzeCommand struct {
}
type AnalyzeCommand struct{}

func (cmd *AnalyzeCommand) Register(app *kingpin.Application, envVars EnvVarNames) {
analyzeCmd := app.Command("analyze", "Run analysis against your Prometheus, Grafana, and Grafana Mimir to see which metrics are being used and exported.")
Expand Down Expand Up @@ -38,6 +40,9 @@ func (cmd *AnalyzeCommand) Register(app *kingpin.Application, envVars EnvVarName
prometheusAnalyzeCmd.Flag("ruler-metrics-file", "The path for the input file containing the metrics from ruler-analyze command").
Default("metrics-in-ruler.json").
StringVar(&paCmd.rulerMetricsFile)
prometheusAnalyzeCmd.Flag("concurrency", "Concurrency (Default: runtime.NumCPU())").
Default(strconv.Itoa(runtime.NumCPU())).
IntVar(&paCmd.concurrency)
prometheusAnalyzeCmd.Flag("output", "The path for the output file").
Default("prometheus-metrics.json").
StringVar(&paCmd.outputFile)
Expand Down
10 changes: 6 additions & 4 deletions pkg/mimirtool/commands/analyse_grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sort"
"time"

"github.com/prometheus/common/model"

"github.com/grafana-tools/sdk"
"gopkg.in/alecthomas/kingpin.v2"

Expand Down Expand Up @@ -77,19 +79,19 @@ func unmarshalDashboard(data []byte, link sdk.FoundBoard) (minisdk.Board, error)
}

func writeOut(mig *analyze.MetricsInGrafana, outputFile string) error {
var metricsUsed []string
var metricsUsed model.LabelValues
for metric := range mig.OverallMetrics {
metricsUsed = append(metricsUsed, metric)
metricsUsed = append(metricsUsed, model.LabelValue(metric))
}
sort.Strings(metricsUsed)
sort.Sort(metricsUsed)

mig.MetricsUsed = metricsUsed
out, err := json.MarshalIndent(mig, "", " ")
if err != nil {
return err
}

if err := os.WriteFile(outputFile, out, os.FileMode(int(0666))); err != nil {
if err := os.WriteFile(outputFile, out, os.FileMode(int(0o666))); err != nil {
return err
}

Expand Down
Loading

0 comments on commit b4ce498

Please sign in to comment.