-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from K-Phoen/logs-converter
Support "logs" panels in converter
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package converter | ||
|
||
import ( | ||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/grabana/logs" | ||
"github.com/K-Phoen/sdk" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (converter *JSON) convertLogs(panel sdk.Panel) grabana.DashboardPanel { | ||
convertedLogs := &grabana.DashboardLogs{ | ||
Title: panel.Title, | ||
Span: panelSpan(panel), | ||
Transparent: panel.Transparent, | ||
Visualization: converter.convertLogsVizualization(panel), | ||
} | ||
|
||
if panel.Description != nil { | ||
convertedLogs.Description = *panel.Description | ||
} | ||
if panel.Height != nil { | ||
convertedLogs.Height = *(panel.Height).(*string) | ||
} | ||
if panel.Datasource != nil { | ||
convertedLogs.Datasource = panel.Datasource.LegacyName | ||
} | ||
if len(panel.Links) != 0 { | ||
convertedLogs.Links = converter.convertPanelLinks(panel.Links) | ||
} | ||
|
||
for _, target := range panel.LogsPanel.Targets { | ||
logsTarget := converter.convertLogsTarget(target) | ||
|
||
convertedLogs.Targets = append(convertedLogs.Targets, logsTarget) | ||
} | ||
|
||
return grabana.DashboardPanel{Logs: convertedLogs} | ||
} | ||
|
||
func (converter *JSON) convertLogsVizualization(panel sdk.Panel) *grabana.LogsVisualization { | ||
return &grabana.LogsVisualization{ | ||
Time: panel.LogsPanel.Options.ShowTime, | ||
UniqueLabels: panel.LogsPanel.Options.ShowLabels, | ||
CommonLabels: panel.LogsPanel.Options.ShowCommonLabels, | ||
WrapLines: panel.LogsPanel.Options.WrapLogMessage, | ||
PrettifyJSON: panel.LogsPanel.Options.PrettifyLogMessage, | ||
HideLogDetails: !panel.LogsPanel.Options.EnableLogDetails, | ||
Order: converter.convertLogsSortOrder(panel.LogsPanel.Options.SortOrder), | ||
Deduplication: converter.convertLogsDedupStrategy(panel.LogsPanel.Options.DedupStrategy), | ||
} | ||
} | ||
|
||
func (converter *JSON) convertLogsDedupStrategy(strategy string) string { | ||
switch strategy { | ||
case string(logs.None): | ||
return "none" | ||
case string(logs.Exact): | ||
return "exact" | ||
case string(logs.Numbers): | ||
return "numbers" | ||
case string(logs.Signature): | ||
return "signature" | ||
default: | ||
converter.logger.Warn("unhandled logs dedup strategy: skipped", zap.String("strategy", strategy)) | ||
return "" | ||
} | ||
} | ||
|
||
func (converter *JSON) convertLogsSortOrder(order string) string { | ||
switch order { | ||
case string(logs.Asc): | ||
return "asc" | ||
case string(logs.Desc): | ||
return "desc" | ||
default: | ||
converter.logger.Warn("unhandled sort order: skipped", zap.String("order", order)) | ||
return "" | ||
} | ||
} | ||
|
||
func (converter *JSON) convertLogsTarget(target sdk.Target) grabana.LogsTarget { | ||
return grabana.LogsTarget{ | ||
Loki: &grabana.LokiTarget{ | ||
Query: target.Expr, | ||
Legend: target.LegendFormat, | ||
Ref: target.RefID, | ||
Hidden: target.Hide, | ||
}, | ||
} | ||
} |
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,84 @@ | ||
package converter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/K-Phoen/sdk" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestConvertLogsPanel(t *testing.T) { | ||
req := require.New(t) | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
height := "200px" | ||
datasource := "prometheus" | ||
|
||
logsPanel := sdk.Panel{ | ||
CommonPanel: sdk.CommonPanel{ | ||
Title: "Logs panel", | ||
Description: strPtr("panel desc"), | ||
Type: "logs", | ||
Transparent: true, | ||
Height: &height, | ||
Datasource: &sdk.DatasourceRef{LegacyName: datasource}, | ||
}, | ||
LogsPanel: &sdk.LogsPanel{ | ||
Targets: []sdk.Target{ | ||
{ | ||
Expr: "loki_query", | ||
LegendFormat: "Legend", | ||
RefID: "A", | ||
}, | ||
}, | ||
Options: sdk.LogsOptions{ | ||
DedupStrategy: "none", | ||
WrapLogMessage: true, | ||
ShowTime: true, | ||
ShowLabels: true, | ||
ShowCommonLabels: true, | ||
PrettifyLogMessage: true, | ||
SortOrder: "Descending", | ||
EnableLogDetails: true, | ||
}, | ||
}, | ||
} | ||
|
||
converted, ok := converter.convertDataPanel(logsPanel) | ||
|
||
req.True(ok) | ||
req.True(converted.Logs.Transparent) | ||
req.Equal("Logs panel", converted.Logs.Title) | ||
req.Equal("panel desc", converted.Logs.Description) | ||
req.Equal(height, converted.Logs.Height) | ||
req.Equal(datasource, converted.Logs.Datasource) | ||
req.False(converted.Logs.Visualization.HideLogDetails) | ||
req.True(converted.Logs.Visualization.PrettifyJSON) | ||
req.True(converted.Logs.Visualization.UniqueLabels) | ||
req.True(converted.Logs.Visualization.WrapLines) | ||
req.True(converted.Logs.Visualization.Time) | ||
req.True(converted.Logs.Visualization.CommonLabels) | ||
req.Equal("none", converted.Logs.Visualization.Deduplication) | ||
req.Equal("desc", converted.Logs.Visualization.Order) | ||
} | ||
|
||
func TestConvertLogsLinks(t *testing.T) { | ||
req := require.New(t) | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
sdkPanel := sdk.NewLogs("") | ||
sdkPanel.Links = []sdk.Link{ | ||
{Title: "logs title", URL: strPtr("logs url")}, | ||
} | ||
|
||
converted, ok := converter.convertDataPanel(*sdkPanel) | ||
|
||
req.True(ok) | ||
req.NotNil(converted.Logs) | ||
|
||
panel := converted.Logs | ||
req.Len(panel.Links, 1) | ||
req.Equal("logs title", panel.Links[0].Title) | ||
req.Equal("logs url", panel.Links[0].URL) | ||
} |