Skip to content

Commit

Permalink
fix: Fix panic for non-existing metric names (#9757)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Sep 15, 2021
1 parent 0e9391d commit c076398
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 76 deletions.
2 changes: 1 addition & 1 deletion plugins/parsers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func NewXPathParserConfigs(metricName string, cfgs []XPathConfig) []xpath.Config
configs := make([]xpath.Config, 0, len(cfgs))
for _, cfg := range cfgs {
config := xpath.Config(cfg)
config.MetricName = metricName
config.MetricDefaultName = metricName
configs = append(configs, config)
}
return configs
Expand Down
26 changes: 16 additions & 10 deletions plugins/parsers/xpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ type Parser struct {
}

type Config struct {
MetricName string
MetricQuery string `toml:"metric_name"`
Selection string `toml:"metric_selection"`
Timestamp string `toml:"timestamp"`
TimestampFmt string `toml:"timestamp_format"`
Tags map[string]string `toml:"tags"`
Fields map[string]string `toml:"fields"`
FieldsInt map[string]string `toml:"fields_int"`
MetricDefaultName string `toml:"-"`
MetricQuery string `toml:"metric_name"`
Selection string `toml:"metric_selection"`
Timestamp string `toml:"timestamp"`
TimestampFmt string `toml:"timestamp_format"`
Tags map[string]string `toml:"tags"`
Fields map[string]string `toml:"fields"`
FieldsInt map[string]string `toml:"fields_int"`

FieldSelection string `toml:"field_selection"`
FieldNameQuery string `toml:"field_name"`
Expand Down Expand Up @@ -160,13 +160,19 @@ func (p *Parser) parseQuery(starttime time.Time, doc, selected dataNode, config

// Determine the metric name. If a query was specified, use the result of this query and the default metric name
// otherwise.
metricname = config.MetricName
metricname = config.MetricDefaultName
if len(config.MetricQuery) > 0 {
v, err := p.executeQuery(doc, selected, config.MetricQuery)
if err != nil {
return nil, fmt.Errorf("failed to query metric name: %v", err)
}
metricname = v.(string)
var ok bool
if metricname, ok = v.(string); !ok {
if v == nil {
p.Log.Infof("Hint: Empty metric-name-node. If you wanted to set a constant please use `metric_name = \"'name'\"`.")
}
return nil, fmt.Errorf("failed to query metric name: query result is of type %T not 'string'", v)
}
}

// By default take the time the parser was invoked and override the value
Expand Down
Loading

0 comments on commit c076398

Please sign in to comment.