Skip to content

Commit 4f3fd58

Browse files
authored
Merge pull request nalbury#36 from nalbury/na-dedupe-and-document-metrics
deduplicate metric names, set default metric query, update docs
2 parents 510edd0 + 40f27ef commit 4f3fd58

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ go_memstats_frees_total
147147
148148
```
149149

150+
A query can be provided to narrow the list of metrics returned, for example to return all metrics that have the string `gc` in their name you can run:
151+
152+
```
153+
➜ ~ promql metrics '{__name__=~".+gc.+"}'
154+
METRICS
155+
go_gc_duration_seconds
156+
go_gc_duration_seconds_count
157+
go_gc_duration_seconds_sum
158+
go_memstats_gc_sys_bytes
159+
go_memstats_last_gc_time_seconds
160+
go_memstats_next_gc_bytes
161+
prometheus_tsdb_head_gc_duration_seconds_count
162+
prometheus_tsdb_head_gc_duration_seconds_sum
163+
```
164+
150165
You can also view the metadata information for a metric (or all metrics) with the `promql meta` command.
151166

152167
```

cmd/metrics.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,10 +23,13 @@ import (
2323
// metricsCmd represents the metrics command
2424
var metricsCmd = &cobra.Command{
2525
Use: "metrics [query_string]",
26-
Short: "Get a list of all prometheus metric names",
27-
Long: `Get a list of all prometheus metric names`,
26+
Short: "Get a list of prometheus metric names matching the provided query",
27+
Long: `Get a list of prometheus metric names matching the provided query. If no query is provided, all metric names will be returned.`,
2828
Run: func(cmd *cobra.Command, args []string) {
2929
var r writer.SeriesResult
30+
if query == "" {
31+
query = `{job=~".+"}`
32+
}
3033
result, warnings, err := pql.SeriesQuery(query)
3134
if len(warnings) > 0 {
3235
errlog.Printf("Warnings: %v\n", warnings)

pkg/writer/writer.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,16 @@ type SeriesResult []model.LabelSet
530530

531531
// Metrics creates a MetricsResult from a SeriesResult
532532
func (r *SeriesResult) Metrics() MetricsResult {
533-
var metrics MetricsResult = make([]string, 0, len(*r))
533+
u := make(map[string]struct{})
534+
var m MetricsResult
534535
for _, l := range *r {
535-
metrics = append(metrics, string(l["__name__"]))
536+
name := string(l["__name__"])
537+
if _, ok := u[name]; !ok {
538+
u[name] = struct{}{}
539+
m = append(m, name)
540+
}
536541
}
537-
return metrics
542+
return m
538543
}
539544

540545
// WriteInstant writes out the results of the query to an

0 commit comments

Comments
 (0)