Skip to content

Commit

Permalink
Merge pull request #367 from linusg/fix-openmetrics
Browse files Browse the repository at this point in the history
Make OpenMetrics output spec compliant
  • Loading branch information
boyter committed Jan 24, 2023
2 parents 5ee0790 + 0f3375b commit a5d95f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
15 changes: 6 additions & 9 deletions README.md
Expand Up @@ -627,15 +627,12 @@ The output includes a metadata header containing definitions of the returned met
# UNIT scc_lines lines
# HELP scc_lines Number of lines.
# TYPE scc_code count
# UNIT scc_code lines
# HELP scc_code Number of lines of actual code.
# TYPE scc_comments count
# HELP scc_comments Number of comments.
# TYPE scc_blanks count
# UNIT scc_blanks lines
# HELP scc_blanks Number of blank lines.
# TYPE scc_complexity count
# UNIT scc_complexity lines
# HELP scc_complexity Code complexity.
# TYPE scc_bytes count
# UNIT scc_bytes bytes
Expand All @@ -655,12 +652,12 @@ scc_bytes{language="Go"} 1000

or, if `--by-file` is present, in per file form:
```text
scc_lines{language="Go", file="./bbbb.go"} 1000
scc_code{language="Go", file="./bbbb.go"} 1000
scc_comments{language="Go", file="./bbbb.go"} 1000
scc_blanks{language="Go", file="./bbbb.go"} 1000
scc_complexity{language="Go", file="./bbbb.go"} 1000
scc_bytes{language="Go", file="./bbbb.go"} 1000
scc_lines{language="Go",file="./bbbb.go"} 1000
scc_code{language="Go",file="./bbbb.go"} 1000
scc_comments{language="Go",file="./bbbb.go"} 1000
scc_blanks{language="Go",file="./bbbb.go"} 1000
scc_complexity{language="Go",file="./bbbb.go"} 1000
scc_bytes{language="Go",file="./bbbb.go"} 1000
```

### Performance
Expand Down
7 changes: 2 additions & 5 deletions processor/formatters.go
Expand Up @@ -46,25 +46,21 @@ var wideFormatFileTruncate = 42
var openMetricsMetadata = `# TYPE scc_files count
# HELP scc_files Number of sourcecode files.
# TYPE scc_lines count
# UNIT scc_lines lines
# HELP scc_lines Number of lines.
# TYPE scc_code count
# UNIT scc_code lines
# HELP scc_code Number of lines of actual code.
# TYPE scc_comments count
# HELP scc_comments Number of comments.
# TYPE scc_blanks count
# UNIT scc_blanks lines
# HELP scc_blanks Number of blank lines.
# TYPE scc_complexity count
# UNIT scc_complexity lines
# HELP scc_complexity Code complexity.
# TYPE scc_bytes count
# UNIT scc_bytes bytes
# HELP scc_bytes Size in bytes.
`
var openMetricsSummaryRecordFormat = "scc_%s{language=\"%s\"} %d\n"
var openMetricsFileRecordFormat = "scc_%s{language=\"%s\", file=\"%s\"} %d\n"
var openMetricsFileRecordFormat = "scc_%s{language=\"%s\",file=\"%s\"} %d\n"

func sortSummaryFiles(summary *LanguageSummary) {
switch {
Expand Down Expand Up @@ -461,6 +457,7 @@ func toOpenMetricsFiles(input chan *FileJob) string {
sb.WriteString(fmt.Sprintf(openMetricsFileRecordFormat, "complexity", file.Language, filename, file.Complexity))
sb.WriteString(fmt.Sprintf(openMetricsFileRecordFormat, "bytes", file.Language, filename, file.Bytes))
}
sb.WriteString("# EOF")
return sb.String()
}

Expand Down
23 changes: 8 additions & 15 deletions processor/formatters_test.go
Expand Up @@ -743,15 +743,12 @@ func TestToOpenMetricsMultiple(t *testing.T) {
# UNIT scc_lines lines
# HELP scc_lines Number of lines.
# TYPE scc_code count
# UNIT scc_code lines
# HELP scc_code Number of lines of actual code.
# TYPE scc_comments count
# HELP scc_comments Number of comments.
# TYPE scc_blanks count
# UNIT scc_blanks lines
# HELP scc_blanks Number of blank lines.
# TYPE scc_complexity count
# UNIT scc_complexity lines
# HELP scc_complexity Code complexity.
# TYPE scc_bytes count
# UNIT scc_bytes bytes
Expand All @@ -763,6 +760,7 @@ scc_comments{language="Go"} 2000
scc_blanks{language="Go"} 2000
scc_complexity{language="Go"} 2000
scc_bytes{language="Go"} 2000
# EOF
`

if res != expectedResult {
Expand Down Expand Up @@ -979,15 +977,12 @@ func TestFileSummarizeOpenMetrics(t *testing.T) {
# UNIT scc_lines lines
# HELP scc_lines Number of lines.
# TYPE scc_code count
# UNIT scc_code lines
# HELP scc_code Number of lines of actual code.
# TYPE scc_comments count
# HELP scc_comments Number of comments.
# TYPE scc_blanks count
# UNIT scc_blanks lines
# HELP scc_blanks Number of blank lines.
# TYPE scc_complexity count
# UNIT scc_complexity lines
# HELP scc_complexity Code complexity.
# TYPE scc_bytes count
# UNIT scc_bytes bytes
Expand All @@ -999,6 +994,7 @@ scc_comments{language="Go"} 1000
scc_blanks{language="Go"} 1000
scc_complexity{language="Go"} 1000
scc_bytes{language="Go"} 1000
# EOF
`

if res != expectedResult {
Expand Down Expand Up @@ -1035,25 +1031,22 @@ func TestFileSummarizeOpenMetricsPerFile(t *testing.T) {
# UNIT scc_lines lines
# HELP scc_lines Number of lines.
# TYPE scc_code count
# UNIT scc_code lines
# HELP scc_code Number of lines of actual code.
# TYPE scc_comments count
# HELP scc_comments Number of comments.
# TYPE scc_blanks count
# UNIT scc_blanks lines
# HELP scc_blanks Number of blank lines.
# TYPE scc_complexity count
# UNIT scc_complexity lines
# HELP scc_complexity Code complexity.
# TYPE scc_bytes count
# UNIT scc_bytes bytes
# HELP scc_bytes Size in bytes.
scc_lines{language="Go", file="C:\\bbbb.go"} 1000
scc_code{language="Go", file="C:\\bbbb.go"} 1000
scc_comments{language="Go", file="C:\\bbbb.go"} 1000
scc_blanks{language="Go", file="C:\\bbbb.go"} 1000
scc_complexity{language="Go", file="C:\\bbbb.go"} 1000
scc_bytes{language="Go", file="C:\\bbbb.go"} 1000
scc_lines{language="Go",file="C:\\bbbb.go"} 1000
scc_code{language="Go",file="C:\\bbbb.go"} 1000
scc_comments{language="Go",file="C:\\bbbb.go"} 1000
scc_blanks{language="Go",file="C:\\bbbb.go"} 1000
scc_complexity{language="Go",file="C:\\bbbb.go"} 1000
scc_bytes{language="Go",file="C:\\bbbb.go"} 1000
`

if res != expectedResult {
Expand Down

0 comments on commit a5d95f3

Please sign in to comment.