Skip to content

Commit

Permalink
Merge pull request #54 from K-Phoen/yaml-graph-legend
Browse files Browse the repository at this point in the history
Support graph legends definition in YAML
  • Loading branch information
K-Phoen authored Mar 29, 2020
2 parents 23a476e + b8b900c commit 0a0ef53
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ linters:
- depguard
- dupl
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
Expand All @@ -32,10 +31,11 @@ linters:
disable:
- lll
- maligned
- goconst

linters-settings:
errcheck:
ignore: fmt:.*

run:
modules-download-mode: vendor
modules-download-mode: vendor
4 changes: 2 additions & 2 deletions dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
type TagAnnotation struct {
Name string
Datasource string
IconColor string `yaml:"color"`
Tags []string
IconColor string `yaml:"color"`
Tags []string `yaml:",flow"`
}

// Option represents an option that can be used to configure a
Expand Down
31 changes: 25 additions & 6 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ rows:
require.Equal(t, ErrNoAlertThresholdDefined, err)
}

func TestUnmarshalYAMLWithInvalidLegendGraph(t *testing.T) {
payload := `
rows:
- name: Test row
panels:
- graph:
title: Heap allocations
legend: [invalid_attribute]
targets:
- prometheus: { query: "go_memstats_heap_alloc_bytes" }
`

_, err := UnmarshalYAML(bytes.NewBufferString(payload))

require.Error(t, err)
require.Equal(t, ErrInvalidLegendAttribute, err)
}

func TestUnmarshalYAMLWithInvalidAlertValueFunctionGraph(t *testing.T) {
payload := `
rows:
Expand Down Expand Up @@ -602,6 +620,7 @@ rows:
height: 400px
span: 4
datasource: prometheus-default
legend: [avg, current, min, max, as_table, no_null_series, no_zero_series]
alert:
title: Too many heap allocations
evaluate_every: 1m
Expand Down Expand Up @@ -704,17 +723,17 @@ rows:
"percentage": false,
"nullPointMode": "null as zero",
"legend": {
"alignAsTable": false,
"avg": false,
"current": false,
"alignAsTable": true,
"avg": true,
"current": true,
"hideEmpty": true,
"hideZero": true,
"max": false,
"min": false,
"max": true,
"min": true,
"rightSide": false,
"show": true,
"total": false,
"values": false
"values": true
},
"targets": [
{
Expand Down
68 changes: 59 additions & 9 deletions decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var ErrNoAlertThresholdDefined = fmt.Errorf("no threshold defined")
var ErrInvalidAlertValueFunc = fmt.Errorf("invalid alert value function")
var ErrInvalidLegendAttribute = fmt.Errorf("invalid legend attribute")

type DashboardGraph struct {
Title string
Expand All @@ -19,6 +20,7 @@ type DashboardGraph struct {
Datasource string `yaml:",omitempty"`
Targets []Target
Axes *GraphAxes `yaml:",omitempty"`
Legend []string `yaml:",omitempty"`
Alert *GraphAlert `yaml:",omitempty"`
}

Expand All @@ -43,6 +45,14 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) {
if graphPanel.Axes != nil && graphPanel.Axes.Bottom != nil {
opts = append(opts, graph.XAxis(graphPanel.Axes.Bottom.toOptions()...))
}
if len(graphPanel.Legend) != 0 {
legendOpts, err := graphPanel.legend()
if err != nil {
return nil, err
}

opts = append(opts, graph.Legend(legendOpts...))
}
if graphPanel.Alert != nil {
alertOpts, err := graphPanel.Alert.toOptions()
if err != nil {
Expand All @@ -64,6 +74,43 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) {
return row.WithGraph(graphPanel.Title, opts...), nil
}

func (graphPanel *DashboardGraph) legend() ([]graph.LegendOption, error) {
opts := make([]graph.LegendOption, 0, len(graphPanel.Legend))

for _, attribute := range graphPanel.Legend {
var opt graph.LegendOption

switch attribute {
case "hide":
opt = graph.Hide
case "as_table":
opt = graph.AsTable
case "to_the_right":
opt = graph.ToTheRight
case "min":
opt = graph.Min
case "max":
opt = graph.Max
case "avg":
opt = graph.Avg
case "current":
opt = graph.Current
case "total":
opt = graph.Total
case "no_null_series":
opt = graph.NoNullSeries
case "no_zero_series":
opt = graph.NoZeroSeries
default:
return nil, ErrInvalidLegendAttribute
}

opts = append(opts, opt)
}

return opts, nil
}

func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) {
if t.Prometheus != nil {
return graph.WithPrometheusTarget(t.Prometheus.Query, t.Prometheus.toOptions()...), nil
Expand All @@ -73,12 +120,12 @@ func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) {
}

type GraphAxis struct {
Hidden *bool
Label string
Unit *string
Min *float64
Max *float64
LogBase int `yaml:"log_base"`
Hidden *bool `yaml:",omitempty"`
Label string `yaml:",omitempty"`
Unit *string `yaml:",omitempty"`
Min *float64 `yaml:",omitempty"`
Max *float64 `yaml:",omitempty"`
LogBase int `yaml:"log_base"`
}

func (a GraphAxis) toOptions() []axis.Option {
Expand All @@ -99,14 +146,17 @@ func (a GraphAxis) toOptions() []axis.Option {
if a.Max != nil {
opts = append(opts, axis.Max(*a.Max))
}
if a.LogBase != 0 {
opts = append(opts, axis.LogBase(a.LogBase))
}

return opts
}

type GraphAxes struct {
Left *GraphAxis
Right *GraphAxis
Bottom *GraphAxis
Left *GraphAxis `yaml:",omitempty"`
Right *GraphAxis `yaml:",omitempty"`
Bottom *GraphAxis `yaml:",omitempty"`
}

type GraphAlert struct {
Expand Down
2 changes: 1 addition & 1 deletion decoder/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type DashboardTable struct {
Height string `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Targets []Target
HiddenColumns []string `yaml:"hidden_columns"`
HiddenColumns []string `yaml:"hidden_columns,flow"`
TimeSeriesAggregations []table.Aggregation `yaml:"time_series_aggregations"`
}

Expand Down
2 changes: 2 additions & 0 deletions doc/graph_panels_yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ rows:
query: "go_memstats_heap_alloc_bytes"
legend: "{{job}}"
ref: A
# Valid values are: hide, as_table, to_the_right, min, max, avg, current, total, no_null_series, no_zero_series
legend: [avg, current, no_null_series, no_zero_series]
alert:
title: Too many heap allocations
evaluate_every: 1m
Expand Down

0 comments on commit 0a0ef53

Please sign in to comment.