Skip to content

Commit

Permalink
Merge pull request #178 from K-Phoen/logs-converter
Browse files Browse the repository at this point in the history
Support "logs" panels in converter
  • Loading branch information
K-Phoen authored Aug 27, 2022
2 parents fba78ff + 3c5a621 commit ef83c51
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ func (converter *JSON) convertDataPanel(panel sdk.Panel) (grabana.DashboardPanel
return converter.convertTimeSeries(panel), true
case "gauge":
return converter.convertGauge(panel), true
case "logs":
return converter.convertLogs(panel), true
default:
converter.logger.Warn("unhandled panel type: skipped", zap.String("type", panel.Type), zap.String("title", panel.Title))
}
Expand Down
90 changes: 90 additions & 0 deletions internal/pkg/converter/logs.go
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,
},
}
}
84 changes: 84 additions & 0 deletions internal/pkg/converter/logs_test.go
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)
}

0 comments on commit ef83c51

Please sign in to comment.