forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics gotemplate support, debug bundle features (hashicorp#9067)
* add goroutine text profiles to nomad operator debug * add server-id=all to nomad operator debug * fix bug from changing metrics from string to []byte * Add function to return MetricsSummary struct, metrics gotemplate support * fix bug resolving 'server-id=all' when no servers are available * add url to operator_debug tests * removed test section which is used for future operator_debug.go changes * separate metrics from operator, use only structs from go-metrics * ensure parent directories are created as needed * add suggested comments for text debug pprof * move check down to where it is used * add WaitForFiles helper function to wait for multiple files to exist * compact metrics check Co-authored-by: Drew Bailey <2614075+drewbailey@users.noreply.github.com> * fix github's silly apply suggestion Co-authored-by: Drew Bailey <2614075+drewbailey@users.noreply.github.com>
- Loading branch information
1 parent
5204afc
commit 5ae7925
Showing
14 changed files
with
398 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package api | ||
|
||
import ( | ||
"io/ioutil" | ||
"time" | ||
) | ||
|
||
// MetricsSummary holds a roll-up of metrics info for a given interval | ||
type MetricsSummary struct { | ||
Timestamp string | ||
Gauges []GaugeValue | ||
Points []PointValue | ||
Counters []SampledValue | ||
Samples []SampledValue | ||
} | ||
|
||
type GaugeValue struct { | ||
Name string | ||
Hash string `json:"-"` | ||
Value float32 | ||
|
||
Labels []Label `json:"-"` | ||
DisplayLabels map[string]string `json:"Labels"` | ||
} | ||
|
||
type PointValue struct { | ||
Name string | ||
Points []float32 | ||
} | ||
|
||
type SampledValue struct { | ||
Name string | ||
Hash string `json:"-"` | ||
*AggregateSample | ||
Mean float64 | ||
Stddev float64 | ||
|
||
Labels []Label `json:"-"` | ||
DisplayLabels map[string]string `json:"Labels"` | ||
} | ||
|
||
// AggregateSample is used to hold aggregate metrics | ||
// about a sample | ||
type AggregateSample struct { | ||
Count int // The count of emitted pairs | ||
Rate float64 // The values rate per time unit (usually 1 second) | ||
Sum float64 // The sum of values | ||
SumSq float64 `json:"-"` // The sum of squared values | ||
Min float64 // Minimum value | ||
Max float64 // Maximum value | ||
LastUpdated time.Time `json:"-"` // When value was last updated | ||
} | ||
|
||
type Label struct { | ||
Name string | ||
Value string | ||
} | ||
|
||
// Metrics returns a slice of bytes containing metrics, optionally formatted as either json or prometheus | ||
func (op *Operator) Metrics(q *QueryOptions) ([]byte, error) { | ||
if q == nil { | ||
q = &QueryOptions{} | ||
} | ||
|
||
metricsReader, err := op.c.rawQuery("/v1/metrics", q) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
metricsBytes, err := ioutil.ReadAll(metricsReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return metricsBytes, nil | ||
} | ||
|
||
// MetricsSummary returns a MetricsSummary struct and query metadata | ||
func (op *Operator) MetricsSummary(q *QueryOptions) (*MetricsSummary, *QueryMeta, error) { | ||
var resp *MetricsSummary | ||
qm, err := op.c.query("/v1/metrics", &resp, q) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return resp, qm, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOperator_MetricsSummary(t *testing.T) { | ||
t.Parallel() | ||
c, s := makeClient(t, nil, nil) | ||
defer s.Stop() | ||
|
||
operator := c.Operator() | ||
qo := &QueryOptions{ | ||
Params: map[string]string{ | ||
"pretty": "1", | ||
}, | ||
} | ||
|
||
metrics, qm, err := operator.MetricsSummary(qo) | ||
require.NoError(t, err) | ||
require.NotNil(t, metrics) | ||
require.NotNil(t, qm) | ||
require.NotNil(t, metrics.Timestamp) // should always get a TimeStamp | ||
require.GreaterOrEqual(t, len(metrics.Points), 0) // may not have points yet | ||
require.GreaterOrEqual(t, len(metrics.Gauges), 1) // should have at least 1 gauge | ||
require.GreaterOrEqual(t, len(metrics.Counters), 1) // should have at least 1 counter | ||
require.GreaterOrEqual(t, len(metrics.Samples), 1) // should have at least 1 sample | ||
} | ||
|
||
func TestOperator_Metrics_Prometheus(t *testing.T) { | ||
t.Parallel() | ||
c, s := makeClient(t, nil, nil) | ||
defer s.Stop() | ||
|
||
operator := c.Operator() | ||
qo := &QueryOptions{ | ||
Params: map[string]string{ | ||
"format": "prometheus", | ||
}, | ||
} | ||
|
||
metrics, err := operator.Metrics(qo) | ||
require.NoError(t, err) | ||
require.NotNil(t, metrics) | ||
metricString := string(metrics[:]) | ||
require.Containsf(t, metricString, "# HELP", "expected Prometheus format containing \"# HELP\", got: \n%s", metricString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.