Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict YAML parsing to reject unknown fields #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions pkg/mapper/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mapper
import (
"regexp"
"time"
"errors"

"github.com/prometheus/client_golang/prometheus"

Expand Down Expand Up @@ -76,9 +77,39 @@ func (m *MetricMapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
m.ObserverType = tmp.TimerType
}

// Implement strict YAML parsing
var raw map[string]interface{}
if err := unmarshal(&raw); err != nil {
return err
}

// List of valid fields in MetricMapping struct
validFields := []string{
"match", "name", "labels", "honor_labels", "observer_type", "legacy_buckets",
"legacy_quantiles", "match_type", "help", "action", "match_metric_type", "ttl",
"summary_options", "histogram_options", "scale",
}
Comment on lines +87 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause some burden to update the structs. We can use yaml.UnmarshalStrict. Also, @matthiasr what do you think of dropping those deprecated fields? They have been here a while.


// Check for unexpected fields
for field := range raw {
if !contains(validFields, field) {
return errors.New("unknown field: " + field)
}
}

return nil
}

// Helper function to check if a field is in the list of valid fields
func contains(slice []string, item string) bool {
for _, a := range slice {
if a == item {
return true
}
}
return false
}

type MaybeFloat64 struct {
Set bool
Val float64
Expand Down