Skip to content

Commit

Permalink
Merge pull request #36 from nalbury/na-dedupe-and-document-metrics
Browse files Browse the repository at this point in the history
deduplicate metric names, set default metric query, update docs
  • Loading branch information
nalbury authored Dec 31, 2022
2 parents 510edd0 + 40f27ef commit 4f3fd58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ go_memstats_frees_total
```

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:

```
➜ ~ promql metrics '{__name__=~".+gc.+"}'
METRICS
go_gc_duration_seconds
go_gc_duration_seconds_count
go_gc_duration_seconds_sum
go_memstats_gc_sys_bytes
go_memstats_last_gc_time_seconds
go_memstats_next_gc_bytes
prometheus_tsdb_head_gc_duration_seconds_count
prometheus_tsdb_head_gc_duration_seconds_sum
```

You can also view the metadata information for a metric (or all metrics) with the `promql meta` command.

```
Expand Down
9 changes: 6 additions & 3 deletions cmd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -23,10 +23,13 @@ import (
// metricsCmd represents the metrics command
var metricsCmd = &cobra.Command{
Use: "metrics [query_string]",
Short: "Get a list of all prometheus metric names",
Long: `Get a list of all prometheus metric names`,
Short: "Get a list of prometheus metric names matching the provided query",
Long: `Get a list of prometheus metric names matching the provided query. If no query is provided, all metric names will be returned.`,
Run: func(cmd *cobra.Command, args []string) {
var r writer.SeriesResult
if query == "" {
query = `{job=~".+"}`
}
result, warnings, err := pql.SeriesQuery(query)
if len(warnings) > 0 {
errlog.Printf("Warnings: %v\n", warnings)
Expand Down
11 changes: 8 additions & 3 deletions pkg/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,16 @@ type SeriesResult []model.LabelSet

// Metrics creates a MetricsResult from a SeriesResult
func (r *SeriesResult) Metrics() MetricsResult {
var metrics MetricsResult = make([]string, 0, len(*r))
u := make(map[string]struct{})
var m MetricsResult
for _, l := range *r {
metrics = append(metrics, string(l["__name__"]))
name := string(l["__name__"])
if _, ok := u[name]; !ok {
u[name] = struct{}{}
m = append(m, name)
}
}
return metrics
return m
}

// WriteInstant writes out the results of the query to an
Expand Down

0 comments on commit 4f3fd58

Please sign in to comment.