Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ type MetricConfig struct {
QueryLiteral string `yaml:"query,omitempty"` // a literal query
QueryRef string `yaml:"query_ref,omitempty"` // references a query in the query map

NoPreparedStatement bool `yaml:"no_prepared_statement,omitempty"` // do not prepare statement
NoPreparedStatement bool `yaml:"no_prepared_statement,omitempty"` // do not prepare statement
StaticValue *float64 `yaml:"static_value,omitempty"`

valueType prometheus.ValueType // TypeString converted to prometheus.ValueType
query *QueryConfig // QueryConfig resolved from QueryRef or generated from Query
Expand Down Expand Up @@ -527,10 +528,14 @@ func (m *MetricConfig) UnmarshalYAML(unmarshal func(any) error) error {
}
}

if len(m.Values) == 0 {
if len(m.Values) == 0 && m.StaticValue == nil {
return fmt.Errorf("no values defined for metric %q", m.Name)
}

if len(m.Values) > 0 && m.StaticValue != nil {
return fmt.Errorf("metric %q cannot have both static_value and values defined", m.Name)
}

if len(m.Values) > 1 {
// Multiple value columns but no value label to identify them
if m.ValueLabel == "" {
Expand Down
6 changes: 5 additions & 1 deletion metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type MetricFamily struct {
func NewMetricFamily(logContext string, mc *config.MetricConfig, constLabels []*dto.LabelPair) (*MetricFamily, errors.WithContext) {
logContext = fmt.Sprintf("%s, metric=%q", logContext, mc.Name)

if len(mc.Values) == 0 {
if len(mc.Values) == 0 && mc.StaticValue == nil {
return nil, errors.New(logContext, "no value column defined")
}
if len(mc.Values) > 1 && mc.ValueLabel == "" {
Expand Down Expand Up @@ -86,6 +86,10 @@ func (mf MetricFamily) Collect(row map[string]any, ch chan<- Metric) {
value := row[v].(float64)
ch <- NewMetric(&mf, value, labelValues...)
}
if mf.config.StaticValue != nil {
value := *mf.config.StaticValue
ch <- NewMetric(&mf, value, labelValues...)
}
}

// Name implements MetricDesc.
Expand Down