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

feat: add multiple features and fix #39

Merged
merged 14 commits into from
Apr 22, 2024
Merged

feat: add multiple features and fix #39

merged 14 commits into from
Apr 22, 2024

Conversation

coanor
Copy link
Contributor

@coanor coanor commented Aug 15, 2023

Close #37/#38/#40.

- Enable list metrics in vendor packages(--with-vendor)
- List labels of metrics(if no labels, output `N/A')
- Fix panic on --add-help if metric got no help info
@coanor
Copy link
Contributor Author

coanor commented Aug 15, 2023

The output example seems like this:

TYPE                NAME                    LABELS       HELP
COUNTER             http_api_signed_total   api,method   API signature count
COUNTER             http_api_dropped_total  api,method   API request dropped when sinker rule match failed

Labels are , splited string. If there're const labels, the label displayed as:

  • some_const_label=some_value: The const label got a string literal value
  • some_const_label=?: While the const label value are not string literal(such as from fmt.Sprintf() or someModule.SomeValue)

And the JSON format like this:

[
  {
    "Name": "http_api_dropped_total",
    "Help": "API request dropped when sinker rule match failed",
    "Type": "COUNTER",
    "Filename": "/path/to/metrics.go",
    "Labels": [
      "api",
      "method"
    ],
    "Line": 105,
    "Column": 3
  },
  {
    "Name": "http_api_signed_total",
    "Help": "API signature count",
    "Type": "COUNTER",
    "Filename": "/path/to/metrics.go",
    "Labels": [
      "api",
      "method"
    ],
    "Line": 118,
    "Column": 3
  }
]

The markdown table seems like this:

| TYPE    | NAME                     | LABELS       | HELP                                              |
| ---     | ---                      | ---          | ---                                               |
| COUNTER | `http_api_signed_total`  | `api,method` | API signature count                               |
| COUNTER | `http_api_dropped_total` | `api,method` | API request dropped when sinker rule match failed |

@yeya24
Copy link
Owner

yeya24 commented Aug 16, 2023

Thanks for the good work! @coanor Are you able to add some unit tests about the labels feature?

- Fix duplicated metric name output
@coanor
Copy link
Contributor Author

coanor commented Aug 20, 2023

Thanks for the good work! @coanor Are you able to add some unit tests about the labels feature?

I find the PR can't find labels within NewDesc, I need some time to refactor current parseXXX functions. And current PR closed, I'll reopen it after variableLabels and constLabels able to parse.

@coanor coanor closed this Aug 20, 2023
@coanor coanor changed the title feat: add multiple features feat: add multiple features and fix Aug 20, 2023
@coanor coanor reopened this Aug 20, 2023
@coanor
Copy link
Contributor Author

coanor commented Aug 20, 2023

Thanks for the good work! @coanor Are you able to add some unit tests about the labels feature?

I've add label discovery within NewDesc and basic test case, but it seems that the original case within promlinter_test.go failed, there missing a issue in current MR, currently I have no idea about what happend.

Will you give me some hint about why the case failed?

Print position(file name or path) in italic under markdown table output
@yeya24
Copy link
Owner

yeya24 commented Sep 18, 2023

It seems like the error case below was missing.

	if issues[2].Metric != "test_metric_total" && issues[2].Text != `no help text` {
		t.Fatal()
	}

@coanor
Copy link
Contributor Author

coanor commented Sep 24, 2023

It seems like the error case case below was missing.

	if issues[2].Metric != "test_metric_total" && issues[2].Text != `no help text` {
		t.Fatal()
	}

I've refactor and fixed the testings within promlinter_test.go, It seems that the issue's order not the key concerning.

}

if !opts.helpSet {
currentMetric.Help = nil
Copy link
Owner

Choose a reason for hiding this comment

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

We don't need this condition. We cannot set String to nil I guess.
Line 327 should be enough to check if help is set

Copy link
Contributor Author

@coanor coanor Oct 12, 2023

Choose a reason for hiding this comment

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

currentMetric.Help is type *string, we just assign nil to mean "There is no help available". If we set a point to empty string("", here is line 327's opts.help, it's default value is ""), some exists test cases will fail(empty string still a valid help, but not make sense.).

promlinter.go Outdated Show resolved Hide resolved
@yeya24
Copy link
Owner

yeya24 commented Oct 1, 2023

I've refactor and fixed the testings within promlinter_test.go, It seems that the issue's order not the key concerning.

I am ok to refactor the tests but I think it is still good to return ordered (at least order can be determined) issues from RunLint().

@coanor
Copy link
Contributor Author

coanor commented Oct 12, 2023

I've refactor and fixed the testings within promlinter_test.go, It seems that the issue's order not the key concerning.

I am ok to refactor the tests but I think it is still good to return ordered (at least order can be determined) issues from RunLint().

About the sort, I think we do not need to sort the result issue, because they are sorted already according to the AST traverse(And I removed the trailing sort on issues(fd4631d)).

For example, the raw issue list is(from testdata/testdata.go)

test_metric_name: ./testdata/testdata.go:23:3
test_metric_total: ./testdata/testdata.go:32:3
foo: ./testdata/testdata.go:39:30
foo_bar_total: ./testdata/testdata.go:78:8
kube_test_metric_count: ./testdata/testdata.go:92:8
test_histogram_duration_seconds: ./testdata/testdata.go:95:27
kube_daemonset_labels: ./testdata/testdata.go:119:4

After sort(based on pos.String()), the ording changed(and is not the AST order):

kube_daemonset_labels: ./testdata/testdata.go:119:4
test_metric_name: ./testdata/testdata.go:23:3
test_metric_total: ./testdata/testdata.go:32:3
foo: ./testdata/testdata.go:39:30
foo_bar_total: ./testdata/testdata.go:78:8
kube_test_metric_count: ./testdata/testdata.go:92:8
test_histogram_duration_seconds: ./testdata/testdata.go:95:27

Because string a.go:123 < a.go:99, and that not the order we need:

import (
  T "testing"

  "github.com/stretchr/testify/assert"
)

func TestStringOrder(t *T.T) {
	assert.True(t, "a.go:123" < "a.go:99")  // Pass
}

@yeya24
Copy link
Owner

yeya24 commented Apr 22, 2024

Sorry for taking it for so long. I am going to merge it. Great work! Thanks

@yeya24 yeya24 merged commit 66dbcb5 into yeya24:master Apr 22, 2024
1 check failed
@yeya24 yeya24 mentioned this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add label-name list in exported output
2 participants