Skip to content

Commit

Permalink
fix: Harvest should log errors when grafana import fails
Browse files Browse the repository at this point in the history
Thanks to Chris Gautcher (@gautcher) for raising.
  • Loading branch information
cgrinds committed Jun 3, 2024
1 parent 9780168 commit 3d4810c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
15 changes: 9 additions & 6 deletions cmd/tools/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func doImport(_ *cobra.Command, _ []string) {
opts.command = "import"
_, err := conf.LoadHarvestConfig(opts.config)
if err != nil {
return
printErrorAndExit(err)
}

adjustOptions()
Expand All @@ -392,6 +392,11 @@ func doImport(_ *cobra.Command, _ []string) {
importDashboards(opts)
}

func printErrorAndExit(err error) {
fmt.Println(err)
os.Exit(1)
}

func validateImport() {
// default case
if opts.dir == "" && opts.serverfolder.name == "" {
Expand All @@ -418,7 +423,7 @@ func initImportVars() {
switch {
case opts.dir == "grafana/dashboards" && opts.serverfolder.name == "":
m[filepath.Join(opts.dir, "cmode")] = &Folder{name: "Harvest-" + harvestRelease + "-cDOT"}
m[filepath.Join(opts.dir, "cmode", "details")] = &Folder{name: "Harvest-" + harvestRelease + "-cDOT Details"}
m[filepath.Join(opts.dir, "cmode-details")] = &Folder{name: "Harvest-" + harvestRelease + "-cDOT Details"}
m[filepath.Join(opts.dir, "7mode")] = &Folder{name: "Harvest-" + harvestRelease + "-7mode"}
m[filepath.Join(opts.dir, "storagegrid")] = &Folder{name: "Harvest-" + harvestRelease + "-StorageGrid"}
case opts.dir != "" && opts.serverfolder.name != "":
Expand All @@ -431,8 +436,7 @@ func initImportVars() {
if opts.customizeDir == "" {
err := checkAndCreateServerFolder(v)
if err != nil {
fmt.Print(err)
os.Exit(1)
printErrorAndExit(err)
}
}
opts.dirGrafanaFolderMap[k] = v
Expand Down Expand Up @@ -488,8 +492,7 @@ func importFiles(dir string, folder *Folder) {
return
}
if files, err = os.ReadDir(dir); err != nil {
// TODO check for not exist
return
printErrorAndExit(err)
}

for _, file := range files {
Expand Down
2 changes: 1 addition & 1 deletion integration/test/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGrafanaAndPrometheusAreConfigured(t *testing.T) {
cDotFolder = "Harvest-" + version.VERSION + "-cDOT"
sevenModeFolder = "Harvest-" + version.VERSION + "-7mode"
log.Info().Str("cMode", cDotFolder).Str("7mode", sevenModeFolder).Msg("Folder name details")
status, out := new(grafana.Mgr).Import("") // send empty so that it will import all dashboards
status, out := new(grafana.Mgr).Import()
if !status {
t.Errorf("Grafana import operation failed out=%s", out)
}
Expand Down
8 changes: 2 additions & 6 deletions integration/test/grafana/grafana_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Mgr struct {
}

func (g *Mgr) Import(jsonDir string) (bool, string) {
func (g *Mgr) Import() (bool, string) {
var (
importOutput string
status bool
Expand All @@ -31,15 +31,11 @@ func (g *Mgr) Import(jsonDir string) (bool, string) {
if err != nil {
panic(err)
}
directoryOption := ""
if jsonDir != "" {
directoryOption = "--directory"
}
grafanaURL := utils.GetGrafanaURL()
if docker.IsDockerBasedPoller() {
grafanaURL = "grafana:3000"
}
importCmds := []string{"grafana", "import", "--overwrite", "--addr", grafanaURL, directoryOption, jsonDir}
importCmds := []string{"grafana", "import", "--overwrite", "--addr", grafanaURL}
if docker.IsDockerBasedPoller() {
params := []string{"exec", containerIDs[0].ID, "bin/harvest"}
params = append(params, importCmds...)
Expand Down

0 comments on commit 3d4810c

Please sign in to comment.