Skip to content

Commit

Permalink
Merge pull request #127 from grafana/126-improve-prometheus-text-metrics
Browse files Browse the repository at this point in the history
Improve Prometheus text metrics
  • Loading branch information
szkiba authored Dec 12, 2024
2 parents a745adb + edce8a7 commit da07304
Show file tree
Hide file tree
Showing 37 changed files with 377 additions and 345 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Registry generated from the source above.
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down Expand Up @@ -618,15 +618,12 @@ docs/example-api
├── community-catalog.json
├── community.json
├── community-metrics.json
├── community-metrics.txt
├── official-catalog.json
├── official.json
├── official-metrics.json
├── official-metrics.txt
├── partner-catalog.json
├── partner.json
├── partner-metrics.json
├── partner-metrics.txt
└── at-least
├── community-catalog.json
├── community.json
Expand Down
55 changes: 23 additions & 32 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,41 @@ func writeAPIGroupGlobal(registry k6registry.Registry, target string) error {
return err
}

if err := writeMetrics(registry, target, "", func(_ k6registry.Extension) bool { return true }); err != nil {
return err
}

for _, tier := range k6registry.Tiers {
err := writeMetrics(registry, filepath.Join(target, "tier"), string(tier)+"-",
func(ext k6registry.Extension) bool {
return ext.Tier == tier
})
if err != nil {
return err
}
}

return nil
return writeMetrics(registry, target)
}

func writeMetrics(
registry k6registry.Registry,
target string,
prefix string,
cond func(k6registry.Extension) bool,
) error {
metrics := k6registry.CalculateMetricsCond(registry, cond)
const promPrefix = "registry_"

basename := "metrics"
func writeMetrics(registry k6registry.Registry, target string) error {
metrics := k6registry.CalculateMetrics(registry)

if len(prefix) > 0 {
basename = prefix + basename
if err := writeJSON(filepath.Join(target, "metrics.json"), metrics); err != nil {
return err
}

basename = filepath.Join(target, basename)
var buff bytes.Buffer

err := writeJSON(basename+".json", metrics)
if err != nil {
if err := metrics.WritePrometheus(&buff, promPrefix, ""); err != nil {
return err
}

data, err := metrics.MarshalPrometheus()
if err != nil {
return err
for _, tier := range k6registry.Tiers {
stier := string(tier)

metrics := k6registry.CalculateMetricsCond(registry, func(ext k6registry.Extension) bool {
return ext.Tier == tier
})

if err := writeJSON(filepath.Join(target, "tier", stier+"-metrics.json"), metrics); err != nil {
return err
}

if err := metrics.WritePrometheus(&buff, promPrefix+"tier_"+stier+"_", "Tier "+stier+": "); err != nil {
return err
}
}

return os.WriteFile(basename+".txt", data, permFile) //nolint:forbidigo
return writeData(filepath.Join(target, "metrics.txt"), buff.Bytes())
}

const gradesvg = `<svg xmlns="http://www.w3.org/2000/svg" width="17" height="20"><clipPath id="B"><rect width="17" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#B)"><path fill="%s" d="M0 0h17v20H0z"/><path fill="url(#A)" d="M0 0h17v20H0z"/></g><g text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text x="85" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="70">%s</text><text x="85" y="140" transform="scale(.1)" fill="#fff" textLength="70">%s</text></g></svg>` //nolint:lll
Expand Down
3 changes: 0 additions & 3 deletions docs/example-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ docs/example-api
├── community-catalog.json
├── community.json
├── community-metrics.json
├── community-metrics.txt
├── official-catalog.json
├── official.json
├── official-metrics.json
├── official-metrics.txt
├── partner-catalog.json
├── partner.json
├── partner-metrics.json
├── partner-metrics.txt
└── at-least
├── community-catalog.json
├── community.json
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/category/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/B.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/at-least/B.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/at-least/C.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/at-least/D.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/at-least/E.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/example-api/grade/at-least/F.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"owner": "grafana",
"public": true,
"stars": 63,
"timestamp": 1733730893,
"timestamp": 1733979175,
"topics": [
"xk6"
],
Expand Down
36 changes: 18 additions & 18 deletions docs/example-api/metrics.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"registry_category_data_count": 2,
"registry_category_kubernetes_count": 1,
"registry_category_misc_count": 1,
"registry_category_observability_count": 1,
"registry_category_reporting_count": 1,
"registry_extension_count": 5,
"registry_grade_a_count": 1,
"registry_grade_b_count": 2,
"registry_grade_c_count": 2,
"registry_issue_examples_count": 1,
"registry_issue_smoke_count": 4,
"registry_issue_types_count": 2,
"registry_product_cloud_count": 1,
"registry_product_oss_count": 5,
"registry_tier_community_count": 1,
"registry_tier_official_count": 4,
"registry_type_javascript_count": 4,
"registry_type_output_count": 1
"category_data_count": 2,
"category_kubernetes_count": 1,
"category_misc_count": 1,
"category_observability_count": 1,
"category_reporting_count": 1,
"extension_count": 5,
"grade_a_count": 1,
"grade_b_count": 2,
"grade_c_count": 2,
"issue_examples_count": 1,
"issue_smoke_count": 4,
"issue_types_count": 2,
"product_cloud_count": 1,
"product_oss_count": 5,
"tier_community_count": 1,
"tier_official_count": 4,
"type_javascript_count": 4,
"type_output_count": 1
}
Loading

0 comments on commit da07304

Please sign in to comment.