diff --git a/cmd/promlinter/main.go b/cmd/promlinter/main.go index 271a0f8..a12f426 100644 --- a/cmd/promlinter/main.go +++ b/cmd/promlinter/main.go @@ -13,7 +13,7 @@ import ( "text/tabwriter" "gopkg.in/alecthomas/kingpin.v2" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "github.com/yeya24/promlinter" ) @@ -196,7 +196,7 @@ func (p *printer) printDefault() { help := "N/A" if m.MetricFamily.Help != nil { - help = *m.MetricFamily.Help + help = m.MetricFamily.GetHelp() } labels := strings.Join(m.Labels(), ",") @@ -206,16 +206,16 @@ func (p *printer) printDefault() { var lineArr []string - mname := *m.MetricFamily.Name + mname := m.MetricFamily.GetName() if p.fmt == "md" { - mname = fmt.Sprintf("`%s`", *m.MetricFamily.Name) + mname = fmt.Sprintf("`%s`", m.MetricFamily.GetName()) labels = fmt.Sprintf("`%s`", labels) } if (p.addPosition || p.addModule) && p.addHelp { lineArr = []string{ p.pos(m.Pos.String()), - MetricType[int32(*m.MetricFamily.Type)], + MetricType[int32(m.MetricFamily.GetType())], mname, labels, help, @@ -223,21 +223,21 @@ func (p *printer) printDefault() { } else if p.addPosition || p.addModule { lineArr = []string{ p.pos(m.Pos.String()), - MetricType[int32(*m.MetricFamily.Type)], + MetricType[int32(m.MetricFamily.GetType())], mname, labels, } } else if p.addHelp { lineArr = []string{ - MetricType[int32(*m.MetricFamily.Type)], + MetricType[int32(m.MetricFamily.GetType())], mname, labels, help, } } else { lineArr = []string{ - MetricType[int32(*m.MetricFamily.Type)], + MetricType[int32(m.MetricFamily.GetType())], mname, labels, } @@ -314,35 +314,27 @@ type MetricForPrinting struct { } func toPrint(metrics []promlinter.MetricFamilyWithPos) []MetricForPrinting { - p := []MetricForPrinting{} + var p []MetricForPrinting + for _, m := range metrics { if m.MetricFamily != nil && *m.MetricFamily.Name != "" { if m.MetricFamily.Type == nil { continue } - n := "" - h := "" - - if m.MetricFamily.Name != nil { - n = *m.MetricFamily.Name - } - if m.MetricFamily.Help != nil { - h = *m.MetricFamily.Help - } var labels []string - for _, m := range m.MetricFamily.Metric { - for idx, _ := range m.Label { + for _, m := range m.MetricFamily.GetMetric() { + for idx, _ := range m.GetLabel() { if m.Label[idx].Name != nil { - labels = append(labels, strings.Trim(*m.Label[idx].Name, `"`)) + labels = append(labels, strings.Trim(m.Label[idx].GetName(), `"`)) } } } i := MetricForPrinting{ - Name: n, - Help: h, - Type: MetricType[int32(*m.MetricFamily.Type)], + Name: m.MetricFamily.GetName(), + Help: m.MetricFamily.GetHelp(), + Type: MetricType[int32(m.MetricFamily.GetType())], Filename: m.Pos.Filename, Line: m.Pos.Line, Column: m.Pos.Column, diff --git a/go.mod b/go.mod index e8e6009..3bdadf1 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/prometheus/client_model v0.2.0 github.com/stretchr/testify v1.8.4 gopkg.in/alecthomas/kingpin.v2 v2.2.6 - gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -23,5 +23,4 @@ require ( github.com/prometheus/procfs v0.7.3 // indirect golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect google.golang.org/protobuf v1.26.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 28e1006..754ce6e 100644 --- a/go.sum +++ b/go.sum @@ -460,7 +460,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/promlinter.go b/promlinter.go index 17a7f4c..986fdb2 100644 --- a/promlinter.go +++ b/promlinter.go @@ -84,15 +84,15 @@ type MetricFamilyWithPos struct { func (m *MetricFamilyWithPos) Labels() []string { var arr []string - if len(m.MetricFamily.Metric) > 0 { - for _, label := range m.MetricFamily.Metric[0].Label { + if len(m.MetricFamily.GetMetric()) > 0 { + for _, label := range m.MetricFamily.Metric[0].GetLabel() { if label.Value != nil { arr = append(arr, fmt.Sprintf("%s=%s", - strings.Trim(*label.Name, `"`), - strings.Trim(*label.Value, `"`))) + strings.Trim(label.GetName(), `"`), + strings.Trim(label.GetValue(), `"`))) } else { - arr = append(arr, strings.Trim(*label.Name, `"`)) + arr = append(arr, strings.Trim(label.GetName(), `"`)) } } } @@ -301,7 +301,7 @@ func (v *visitor) parseOpts(optArgs []ast.Expr, metricType dto.MetricType) ast.V // parse labels if labelOpts := v.parseOptsExpr(optArgs[1]); labelOpts != nil && len(labelOpts.labels) > 0 { metric = &dto.Metric{} - for idx, _ := range labelOpts.labels { + for idx := range labelOpts.labels { metric.Label = append(metric.Label, &dto.LabelPair{ Name: &labelOpts.labels[idx], @@ -423,14 +423,14 @@ func (v *visitor) parseSendMetricChanExpr(chExpr *ast.SendStmt) ast.Visitor { if len(descCall.labels) > 0 { m := &dto.Metric{} - for idx, _ := range descCall.labels { + for idx := range descCall.labels { m.Label = append(m.Label, &dto.LabelPair{ Name: &descCall.labels[idx], }) } - for idx, _ := range descCall.constLabels { + for idx := range descCall.constLabels { m.Label = append(m.Label, &dto.LabelPair{ Name: &descCall.constLabels[idx][0],