diff --git a/main.go b/main.go index 81d745b..0dc033d 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,9 @@ import ( ) const ( - gVersion = "0.2.3" + gVersion = "0.2.4" + // Default datasource timeout is 10 seconds + gDefaultDSTimeout = 10 ) type PromDataSourceConfig struct { @@ -120,7 +122,7 @@ func (d *PromDataSourceConfig) GetData(timeRange *MyRange) (float64, error) { result, warnings, err = v1api.QueryRange(ctx, d.Query, rng, v1.WithTimeout(time.Duration(d.Timeout)*time.Second)) } if err != nil { - log.Errorf("DataSourceConfig.GetData: Error querying Prometheus: %v\n", err) + log.Errorf("DataSourceConfig.GetData: Error querying Prometheus: %v. Query is: %s\n", err, d.Query) return 0.0, err } if len(warnings) > 0 { @@ -425,6 +427,48 @@ func toggleDebug() { } } +// Deep copy src Node into a new memory space +func newNodeClone(src *Node) *Node { + return &Node{ + Name: src.Name, + Id: src.Id, + Title: src.Title, + Subtitle: src.Subtitle, + MainStat: src.MainStat, + MainStatQuery: src.MainStatQuery, + MainStatFormat: src.MainStatFormat, + SecondaryStat: src.SecondaryStat, + SecondaryStatQuery: src.SecondaryStatQuery, + SecondaryStatFormat: src.SecondaryStatFormat, + Color: src.Color, + Icon: src.Icon, + NodeRadius: src.NodeRadius, + Highlighted: src.Highlighted, + HighlightedQuery: src.HighlightedQuery, + } +} + +// Deep copy src Edge into a new memory space +func newEdgeClone(src *Edge) *Edge { + return &Edge{ + Id: src.Id, + Source: src.Source, + Target: src.Target, + MainStat: src.MainStat, + MainStatQuery: src.MainStatQuery, + MainStatFormat: src.MainStatFormat, + SecondaryStat: src.SecondaryStat, + SecondaryStatQuery: src.SecondaryStatQuery, + SecondaryStatFormat: src.SecondaryStatFormat, + Color: src.Color, + Thickness: src.Thickness, + ThicknessQuery: src.ThicknessQuery, + Highlighted: src.Highlighted, + HighlightedQuery: src.HighlightedQuery, + StrokeDashArray: src.StrokeDashArray, + } +} + func reloadConfigFile() { // First reread config file if err := viper.ReadInConfig(); err != nil { @@ -451,7 +495,7 @@ func reloadConfigFile() { case "chinese": gPrinter = message.NewPrinter(language.Chinese) default: - log.Errorf("Language not implented: %s. Fallback to english\n", viper.Get("language").(string)) + log.Errorf("Language not implemented: %s. Fallback to english\n", viper.Get("language").(string)) gPrinter = message.NewPrinter(language.English) } @@ -478,19 +522,17 @@ func reloadConfigFile() { Edges []Edge `yaml:"edges"` }{} yaml.Unmarshal(yd, &tmp) - var graphNodes []Item - var graphEdges []Item - for _, n := range tmp.Nodes { - graphNodes = append(graphNodes, &n) - } - for _, e := range tmp.Edges { - graphEdges = append(graphEdges, &e) - } - + graph := Graph{ Name: tmp.Name, - Nodes: graphNodes, - Edges: graphEdges, + } + for _, n := range tmp.Nodes { + // Deep copy Node so garbage collecting tmp won't pull the carpet under our feet + graph.Nodes = append(graph.Nodes, newNodeClone(&n)) + } + for _, e := range tmp.Edges { + // Deep copy Edge + graph.Edges = append(graph.Edges, newEdgeClone(&e)) } gGraphs = append(gGraphs, graph) } @@ -504,6 +546,10 @@ func reloadConfigFile() { yd, _ := yaml.Marshal(d) var ds PromDataSourceConfig yaml.Unmarshal(yd, &ds) + // Set default Values + if ds.Timeout == 0 { + ds.Timeout = gDefaultDSTimeout + } gDataSources = append(gDataSources, ds) } }