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

fix: use methods instead of fields #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 16 additions & 24 deletions cmd/promlinter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"text/tabwriter"

"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/yeya24/promlinter"
)
Expand Down Expand Up @@ -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(), ",")
Expand All @@ -206,38 +206,38 @@ 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,
}
} 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,
}
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 8 additions & 8 deletions promlinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), `"`))
}
}
}
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down