Skip to content

Commit

Permalink
feat: Add graph and singlestat, support old style grafana dashboard (#22
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yufeiminds authored Aug 16, 2023
1 parent 0d89e8b commit 40b3d00
Show file tree
Hide file tree
Showing 34 changed files with 3,792 additions and 1,010 deletions.
2 changes: 1 addition & 1 deletion docs/references/guance.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Guance Cloud Command-Line Interface

- [guance iac](guance_iac.md) - Infrastructure as Code

###### Auto generated by spf13/cobra on 15-Aug-2023
###### Auto generated by spf13/cobra on 16-Aug-2023
2 changes: 1 addition & 1 deletion docs/references/guance_iac.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Infrastructure as Code
- [guance](guance.md) - Guance Cloud Command-Line Interface
- [guance iac import](guance_iac_import.md) - Import external resource as Guance Cloud IaC resource

###### Auto generated by spf13/cobra on 15-Aug-2023
###### Auto generated by spf13/cobra on 16-Aug-2023
2 changes: 1 addition & 1 deletion docs/references/guance_iac_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Import external resource as Guance Cloud IaC resource
- [guance iac import console](guance_iac_import_console.md) - Import Guance Cloud Console resources
- [guance iac import grafana](guance_iac_import_grafana.md) - (Alpha) Import Grafana Dashboard resources

###### Auto generated by spf13/cobra on 15-Aug-2023
###### Auto generated by spf13/cobra on 16-Aug-2023
2 changes: 1 addition & 1 deletion docs/references/guance_iac_import_console.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ guance iac import console [flags]

- [guance iac import](guance_iac_import.md) - Import external resource as Guance Cloud IaC resource

###### Auto generated by spf13/cobra on 15-Aug-2023
###### Auto generated by spf13/cobra on 16-Aug-2023
2 changes: 1 addition & 1 deletion docs/references/guance_iac_import_grafana.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ guance iac import grafana [flags]

- [guance iac import](guance_iac_import.md) - Import external resource as Guance Cloud IaC resource

###### Auto generated by spf13/cobra on 15-Aug-2023
###### Auto generated by spf13/cobra on 16-Aug-2023
2 changes: 1 addition & 1 deletion docs/references/man/guance-iac-import-console.1
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ Import Guance Cloud Console resources

.SH HISTORY
.PP
15-Aug-2023 Auto generated by spf13/cobra
16-Aug-2023 Auto generated by spf13/cobra
2 changes: 1 addition & 1 deletion docs/references/man/guance-iac-import-grafana.1
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ guance-iac-import-grafana - (Alpha) Import Grafana Dashboard resources

.SH HISTORY
.PP
15-Aug-2023 Auto generated by spf13/cobra
16-Aug-2023 Auto generated by spf13/cobra
2 changes: 1 addition & 1 deletion docs/references/man/guance-iac-import.1
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Import external resource as Guance Cloud IaC resource

.SH HISTORY
.PP
15-Aug-2023 Auto generated by spf13/cobra
16-Aug-2023 Auto generated by spf13/cobra
2 changes: 1 addition & 1 deletion docs/references/man/guance-iac.1
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Infrastructure as Code

.SH HISTORY
.PP
15-Aug-2023 Auto generated by spf13/cobra
16-Aug-2023 Auto generated by spf13/cobra
2 changes: 1 addition & 1 deletion docs/references/man/guance.1
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Guance Cloud Command-Line Interface

.SH HISTORY
.PP
15-Aug-2023 Auto generated by spf13/cobra
16-Aug-2023 Auto generated by spf13/cobra
7 changes: 6 additions & 1 deletion internal/grafana/addons/units/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ type Addon struct {
}

func (addon *Addon) PatchChart(panel *grafanaspec.Panel, chart map[string]any) (map[string]any, error) {
units := convertUnit(types.StringValue(panel.FieldConfig.Defaults.Unit))
grafanaUnit := types.StringValue(panel.FieldConfig.Defaults.Unit)
if grafanaUnit == "" {
grafanaUnit = types.StringValue(panel.Format)
}

units := convertUnit(grafanaUnit)
if units == nil {
return chart, nil
}
Expand Down
51 changes: 36 additions & 15 deletions internal/grafana/addons/variables/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package variables
import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/go-multierror"

Expand All @@ -18,7 +19,7 @@ func (addon *Addon) BuildVariables(variables []grafanaspec.VariableModel) ([]any
continue
}

label, err := getLabelFromVariable(variable)
dqlQuery, err := addon.toDQL(variable)
if err != nil {
mErr = multierror.Append(mErr, fmt.Errorf("failed to get label from variable: %w", err))
continue
Expand All @@ -36,7 +37,7 @@ func (addon *Addon) BuildVariables(variables []grafanaspec.VariableModel) ([]any
"metric": "",
"object": "",
"tag": "",
"value": fmt.Sprintf("SHOW_TAG_VALUE(from=['%s'], keyin=['%s'])", addon.Measurement, label),
"value": dqlQuery,
},
"hide": 0,
"isHiddenAsterisk": 0,
Expand All @@ -52,23 +53,43 @@ func (addon *Addon) BuildVariables(variables []grafanaspec.VariableModel) ([]any
return vars, nil
}

var labelFuncPattern = regexp.MustCompile(`label_values\((.+),\s*(.+)\)`)
var labelFuncPattern = regexp.MustCompile(`label_values\((.+),?\s*(.*)\)`)

func getLabelFromVariable(variable grafanaspec.VariableModel) (string, error) {
func (addon *Addon) toDQL(variable grafanaspec.VariableModel) (string, error) {
queryString, err := getPromExpr(variable)
if err != nil {
return "", fmt.Errorf("failed to get prometheus expression: %w", err)
}

switch {
case strings.HasPrefix(queryString, "label_values("):
match := labelFuncPattern.FindStringSubmatch(queryString)
if len(match) != 3 {
return "", fmt.Errorf("failed to get label from variable: %s", variable.Name)
}
return fmt.Sprintf("SHOW_TAG_VALUE(from=['%s'], keyin=['%s'])", addon.Measurement, match[2]), nil
case strings.HasPrefix(queryString, "query_result("):
return "", nil
default:
return "", fmt.Errorf("failed to get label from variable: %s", variable.Name)
}
}

func getPromExpr(variable grafanaspec.VariableModel) (string, error) {
if variable.Query == nil {
return "", fmt.Errorf("query %s is empty", variable.Name)
}
m, ok := (*variable.Query).(map[string]any)
if !ok {
return "", fmt.Errorf("failed to decode query %s map", variable.Name)
}
queryString, ok := m["query"].(string)
if !ok {

switch t := (*variable.Query).(type) {
case string:
return t, nil
case map[string]any:
queryString, ok := t["query"].(string)
if !ok {
return "", fmt.Errorf("failed to get query string from variable: %s", variable.Name)
}
return queryString, nil
default:
return "", fmt.Errorf("failed to get query string from variable: %s", variable.Name)
}
match := labelFuncPattern.FindStringSubmatch(queryString)
if len(match) != 3 {
return "", fmt.Errorf("failed to get label from variable: %s", variable.Name)
}
return match[2], nil
}
6 changes: 6 additions & 0 deletions internal/grafana/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/bargauge"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/base"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/gauge"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/graph"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/pie"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/singlestat"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/stat"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/table"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/text"
Expand Down Expand Up @@ -80,6 +82,10 @@ func NewChartBuilder(opts ...ChartOption) (charts.Builder, error) {
chartBuilder = &bar.Builder{Builder: baseBuilder}
case text.ChartTypeText:
chartBuilder = &text.Builder{Builder: baseBuilder}
case singlestat.ChartTypeSingleStat:
chartBuilder = &singlestat.Builder{Builder: baseBuilder}
case graph.ChartTypeGraph:
chartBuilder = &graph.Builder{Builder: baseBuilder}
default:
return nil, fmt.Errorf("unknown chart type: %s", options.Type)
}
Expand Down
49 changes: 49 additions & 0 deletions internal/grafana/charts/graph/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package graph

import (
"github.com/GuanceCloud/guance-cli/internal/grafana/charts"
"github.com/GuanceCloud/guance-cli/internal/grafana/charts/base"
grafanaspec "github.com/GuanceCloud/guance-cli/internal/grafana/spec"
"github.com/GuanceCloud/guance-cli/internal/helpers/types"
)

type Builder struct {
base.Builder
}

func (builder Builder) Build(panel grafanaspec.Panel) (map[string]any, error) {
return map[string]any{
"type": builder.Meta().GuanceType,
"name": types.StringValue(panel.Title),
"pos": map[string]any{
"h": panel.GridPos.H,
"w": panel.GridPos.W,
"x": panel.GridPos.X,
"y": panel.GridPos.Y,
},
"group": map[string]any{
"name": types.String(builder.Group),
},
"extend": map[string]any{
"settings": map[string]any{
"showTitle": true,
"titleDesc": types.StringValue(panel.Description),
"showFieldMapping": false,
"timeInterval": "auto",
"chartType": "areaLine",
},
},
}, nil
}

const (
ChartTypeGraph = "graph"
GuanceChartTypeSequence = "sequence"
)

func (builder Builder) Meta() charts.Meta {
return charts.Meta{
GuanceType: GuanceChartTypeSequence,
GrafanaType: ChartTypeGraph,
}
}
20 changes: 20 additions & 0 deletions internal/grafana/charts/graph/graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package graph_test

import (
"testing"

"github.com/GuanceCloud/guance-cli/internal/grafana/charts/graph"
"github.com/GuanceCloud/guance-cli/internal/grafana/testutil"
)

func TestChartBuilder_Build(t *testing.T) {
chart := &graph.Builder{Builder: testutil.BaseChartBuilder}
testutil.TestChartSnapshots(t, []testutil.ChartSnapshotTest{
{
Name: "ok",
GrafanaFile: "testdata/graph.grafana.json",
GuanceFile: "testdata/graph.guance.json",
Chart: chart,
},
})
}
99 changes: 99 additions & 0 deletions internal/grafana/charts/graph/testdata/graph.grafana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"description": "Virtual and Resident memory size in bytes, averages over 5 min interval",
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 7,
"w": 8,
"x": 8,
"y": 3
},
"id": 24,
"legend": {
"alignAsTable": true,
"avg": true,
"current": true,
"max": true,
"min": true,
"show": true,
"total": false,
"values": true
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"options": {
"dataLinks": []
},
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "avg(rate(process_resident_memory_bytes{release=\"$release\", instance=\"$instance\"}[5m]))",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "Resident Mem",
"refId": "A"
},
{
"expr": "avg(rate(process_virtual_memory_bytes{release=\"$release\", instance=\"$instance\"}[5m]))",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "Virtual Mem",
"refId": "B"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Average Memory Usage",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"decimals": null,
"format": "decbytes",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
Loading

0 comments on commit 40b3d00

Please sign in to comment.