-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
198 lines (168 loc) · 3.66 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package k6registry
import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
)
// CalculateMetrics calculates registry metrics for all extensions.
func CalculateMetrics(reg Registry) *Metrics {
return CalculateMetricsCond(reg, func(_ Extension) bool { return true })
}
// CalculateMetricsCond calculates registry metrics for subset of extensions.
func CalculateMetricsCond(reg Registry, predicate func(Extension) bool) *Metrics {
const k6Module = "go.k6.io/k6"
m := new(Metrics)
for _, ext := range reg {
if ext.Module == k6Module || !predicate(ext) {
continue
}
m.ExtensionCount++
m.tier(ext.Tier)
if ext.Compliance != nil {
m.grade(ext.Compliance.Grade)
}
for _, prod := range ext.Products {
m.product(prod)
}
if len(ext.Products) == 0 {
m.ProductOSSCount++
}
if len(ext.Imports) > 0 {
m.TypeJavaScriptCount++
}
if len(ext.Outputs) > 0 {
m.TypeOutputCount++
}
if strings.HasPrefix(ext.Module, "github.com/grafana/") && ext.Tier != TierOfficial {
m.TierUnofficialCount++
}
for _, cat := range ext.Categories {
m.category(cat)
}
if len(ext.Categories) == 0 {
m.CategoryMiscCount++
}
for _, issue := range ext.Compliance.Issues {
m.issue(issue)
}
if ext.Cgo {
m.CgoCount++
}
}
return m
}
func (m *Metrics) tier(tier Tier) {
switch tier {
case TierOfficial:
m.TierOfficialCount++
case TierPartner:
m.TierPartnerCount++
case TierCommunity:
fallthrough
default:
m.TierCommunityCount++
}
}
func (m *Metrics) grade(grade Grade) {
switch grade {
case GradeA:
m.GradeACount++
case GradeB:
m.GradeBCount++
case GradeC:
m.GradeCCount++
case GradeD:
m.GradeDCount++
case GradeE:
m.GradeECount++
case GradeF:
m.GradeFCount++
default:
}
}
func (m *Metrics) product(product Product) {
switch product {
case ProductCloud:
m.ProductCloudCount++
case ProductSynthetic:
m.ProductSyntheticCount++
case ProductOSS:
fallthrough
default:
m.ProductOSSCount++
}
}
func (m *Metrics) category(category Category) {
switch category {
case CategoryAuthentication:
m.CategoryAuthenticationCount++
case CategoryBrowser:
m.CategoryBrowserCount++
case CategoryData:
m.CategoryDataCount++
case CategoryKubernetes:
m.CategoryKubernetesCount++
case CategoryMessaging:
m.CategoryMessagingCount++
case CategoryObservability:
m.CategoryObservabilityCount++
case CategoryProtocol:
m.CategoryProtocolCount++
case CategoryReporting:
m.CategoryReportingCount++
case CategoryMisc:
fallthrough
default:
m.CategoryMiscCount++
}
}
func (m *Metrics) issue(issue string) {
switch issue {
case "module":
m.IssueModuleCount++
case "replace":
m.IssueReplaceCount++
case "readme":
m.IssueReadmeCount++
case "examples":
m.IssueExamplesCount++
case "license":
m.IssueLicenseCount++
case "git":
m.IssueGitCount++
case "versions":
m.IssueVersionsCount++
case "build":
m.IssueBuildCount++
case "smoke":
m.IssueSmokeCount++
case "types":
m.IssueTypesCount++
case "codeowners":
m.IssueCodeownersCount++
default:
}
}
// WritePrometheus marshals metrics in Prometheus text format.
func (m *Metrics) WritePrometheus(out io.Writer, namePrefix string, helpPrefix string) error {
data, err := json.Marshal(m)
if err != nil {
return err
}
var dict map[string]int
if err := json.Unmarshal(data, &dict); err != nil {
return err
}
now := time.Now().UnixMilli()
for name, value := range dict {
fullname := namePrefix + name
if help, hasHelp := metricsHelps[name]; hasHelp {
fmt.Fprintf(out, "# HELP %s %s\n", fullname, helpPrefix+help)
}
fmt.Fprintf(out, "# TYPE %s counter\n", fullname)
fmt.Fprintf(out, "%s %d %d\n\n", fullname, value, now)
}
return nil
}