Skip to content

Commit

Permalink
Decode logs target
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jan 5, 2022
1 parent 77aaa4b commit 25794f6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
26 changes: 26 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestUnmarshalYAML(t *testing.T) {
graphPanelWithInfluxdbTarget(),
timeseriesPanel(),
collapseRow(),
logsPanel(),
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -652,6 +653,31 @@ rows:
}
}

func logsPanel() testCase {
yaml := `title: Awesome dashboard
rows:
- name: Test row
panels:
- logs:
title: Kubernetes logs
description: Everything okay?
span: 12
visualization:
deduplication: exact
targets:
- loki:
query: "{namespace=\"default\"}"
ref: A
`

return testCase{
name: "single row with logs panel",
yaml: yaml,
expectedGrafanaJSON: "logs_panel.json",
}
}

func collapseRow() testCase {
yaml := `title: Awesome dashboard
Expand Down
21 changes: 21 additions & 0 deletions decoder/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ type DashboardLogs struct {
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Targets []LogsTarget `yaml:",omitempty"`
Visualization *LogsVisualization `yaml:",omitempty"`
}

type LogsTarget struct {
Loki *LokiTarget `yaml:",omitempty"`
}

func (panel DashboardLogs) toOption() (row.Option, error) {
opts := []logs.Option{}

Expand Down Expand Up @@ -50,10 +55,26 @@ func (panel DashboardLogs) toOption() (row.Option, error) {

opts = append(opts, vizOpts...)
}
for _, t := range panel.Targets {
opt, err := panel.target(t)
if err != nil {
return nil, err
}

opts = append(opts, opt)
}

return row.WithLogs(panel.Title, opts...), nil
}

func (panel DashboardLogs) target(t LogsTarget) (logs.Option, error) {
if t.Loki != nil {
return logs.WithLokiTarget(t.Loki.Query, t.Loki.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}

type LogsVisualization struct {
Time bool `yaml:",omitempty"`
UniqueLabels bool `yaml:"unique_labels,omitempty"`
Expand Down
21 changes: 21 additions & 0 deletions decoder/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/loki"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/target/stackdriver"
)
Expand Down Expand Up @@ -60,6 +61,26 @@ func (t PrometheusTarget) toOptions() []prometheus.Option {
return opts
}

type LokiTarget struct {
Query string
Legend string `yaml:",omitempty"`
Ref string `yaml:",omitempty"`
Hidden bool `yaml:",omitempty"`
}

func (t LokiTarget) toOptions() []loki.Option {
opts := []loki.Option{
loki.Legend(t.Legend),
loki.Ref(t.Ref),
}

if t.Hidden {
opts = append(opts, loki.Hide())
}

return opts
}

type GraphiteTarget struct {
Query string
Ref string `yaml:",omitempty"`
Expand Down
62 changes: 62 additions & 0 deletions decoder/testdata/logs_panel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"slug": "",
"title": "Awesome dashboard",
"originalTitle": "",
"tags": null,
"style": "dark",
"timezone": "",
"editable": false,
"hideControls": false,
"sharedCrosshair": false,
"templating": {"list": null},
"annotations": {"list": null},
"links": null,
"panels": null,
"rows": [
{
"title": "Test row",
"collapse": false,
"editable": true,
"height": "250px",
"repeat": null,
"showTitle": true,
"panels": [
{
"editable": false,
"error": false,
"gridPos": {},
"id": 12,
"isNew": false,
"span": 12,
"title": "Kubernetes logs",
"description": "Everything okay?",
"transparent": false,
"type": "logs",
"options": {
"dedupStrategy": "exact",
"enableLogDetails": true,
"prettifyLogMessage": false,
"showCommonLabels": false,
"showLabels": false,
"showTime": false,
"sortOrder": "Descending",
"wrapLogMessage": false
},
"targets": [
{
"refId": "A",
"expr": "{namespace=\"default\"}"
}
]
}
]
}
],
"time": {"from": "now-3h", "to": "now"},
"timepicker": {
"refresh_intervals": ["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],
"time_options": ["5m","15m","1h","6h","12h","24h","2d","7d","30d"]
},
"schemaVersion": 0,
"version": 0
}

0 comments on commit 25794f6

Please sign in to comment.