Skip to content

Commit cdb79e1

Browse files
authored
feat: Support DisplayInfo in Panels (#210)
Today, panels created by the provider only support a promql query, but not the DisplayInfo fields. That means that dashboard panels are hard to read as they are the raw promql and not user friendly strings. This adds support for DisplayInfo fields in the resource.
1 parent 6aff8ac commit cdb79e1

File tree

5 files changed

+163
-12
lines changed

5 files changed

+163
-12
lines changed

CODEOWNERS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
# compliance
55
*benchmark* @haresh-suresh @nkraemer-sysdig
66

7+
78
# monitor
8-
*monitor*alert* @arturodilecce
9+
*monitor*dashboard* @brokenpip3
10+
*monitor*alert* @arturodilecce
11+

sysdig/internal/client/monitor/model/dashboard.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ type AdvancedQueries struct {
170170
ParentPanel *Panels `json:"-"`
171171
}
172172

173-
func NewPromqlQuery(query string, parentPanel *Panels) *AdvancedQueries {
173+
func NewPromqlQuery(query string, parentPanel *Panels, displayInfo DisplayInfo) *AdvancedQueries {
174174
newQuery := &AdvancedQueries{
175175
Enabled: true,
176176
DisplayInfo: DisplayInfo{
177-
DisplayName: "",
178-
TimeSeriesDisplayNameTemplate: "",
179-
Type: "lines",
177+
DisplayName: displayInfo.DisplayName,
178+
TimeSeriesDisplayNameTemplate: displayInfo.TimeSeriesDisplayNameTemplate,
179+
Type: displayInfo.Type,
180180
},
181181
Format: newPercentFormat(),
182182
Query: query,

sysdig/resource_sysdig_monitor_dashboard.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ func resourceSysdigMonitorDashboard() *schema.Resource {
183183
Required: true,
184184
ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"percent", "data", "data rate", "number", "number rate", "time"}, false)),
185185
},
186+
"display_info": {
187+
Type: schema.TypeSet,
188+
Optional: true,
189+
MinItems: 1,
190+
Elem: &schema.Resource{
191+
Schema: map[string]*schema.Schema{
192+
"display_name": {
193+
Type: schema.TypeString,
194+
Optional: true,
195+
},
196+
"time_series_display_name_template": {
197+
Type: schema.TypeString,
198+
Required: true,
199+
},
200+
"type": {
201+
Type: schema.TypeString,
202+
Required: true,
203+
ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"lines", "stackedArea", "stackedBar"}, false)),
204+
},
205+
},
206+
},
207+
},
186208
},
187209
},
188210
},
@@ -252,7 +274,6 @@ func resourceSysdigDashboardRead(ctx context.Context, data *schema.ResourceData,
252274
}
253275

254276
dashboard, err := client.GetDashboardByID(ctx, id)
255-
256277
if err != nil {
257278
data.SetId("")
258279
return nil
@@ -519,10 +540,24 @@ func textPanelFromResourceData(panelInfo map[string]interface{}) (*model.Panels,
519540
}
520541

521542
func queriesFromResourceData(panelInfo map[string]interface{}, panel *model.Panels) (newQueries []*model.AdvancedQueries, err error) {
543+
var display model.DisplayInfo
544+
522545
for _, queryItr := range panelInfo["query"].(*schema.Set).List() {
523546
queryInfo := queryItr.(map[string]interface{})
524547

525-
promqlQuery := model.NewPromqlQuery(queryInfo["promql"].(string), panel)
548+
displayInfo := queryInfo["display_info"].(*schema.Set).List()
549+
if len(displayInfo) > 0 {
550+
dip := displayInfo[0].(map[string]interface{})
551+
display.DisplayName = dip["display_name"].(string)
552+
display.TimeSeriesDisplayNameTemplate = dip["time_series_display_name_template"].(string)
553+
display.Type = dip["type"].(string)
554+
} else {
555+
display.DisplayName = ""
556+
display.TimeSeriesDisplayNameTemplate = ""
557+
display.Type = "lines"
558+
}
559+
560+
promqlQuery := model.NewPromqlQuery(queryInfo["promql"].(string), panel, display)
526561

527562
switch queryInfo["unit"].(string) {
528563
case "percent":
@@ -746,10 +781,22 @@ func queriesToResourceData(advancedQueries []*model.AdvancedQueries) ([]map[stri
746781
return nil, fmt.Errorf("unsupported query format unit: %s", query.Format.Unit)
747782
}
748783

749-
queries = append(queries, map[string]interface{}{
750-
"unit": unit,
751-
"promql": query.Query,
752-
})
784+
if query.DisplayInfo.DisplayName == "" && query.DisplayInfo.TimeSeriesDisplayNameTemplate == "" {
785+
queries = append(queries, map[string]interface{}{
786+
"unit": unit,
787+
"promql": query.Query,
788+
})
789+
} else {
790+
queries = append(queries, map[string]interface{}{
791+
"unit": unit,
792+
"promql": query.Query,
793+
"display_info": []map[string]interface{}{{
794+
"display_name": query.DisplayInfo.DisplayName,
795+
"time_series_display_name_template": query.DisplayInfo.TimeSeriesDisplayNameTemplate,
796+
"type": query.DisplayInfo.Type,
797+
}},
798+
})
799+
}
753800
}
754801
return queries, nil
755802
}

sysdig/resource_sysdig_monitor_dashboard_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func TestAccDashboard(t *testing.T) {
5555
}),
5656
),
5757
},
58+
{
59+
Config: multiplePanelsDashboardWithDisplayInfo(rText()),
60+
},
5861
},
5962
})
6063
}
@@ -275,3 +278,86 @@ resource "sysdig_monitor_dashboard" "dashboard" {
275278
}
276279
`, name, name, name)
277280
}
281+
282+
func multiplePanelsDashboardWithDisplayInfo(name string) string {
283+
return fmt.Sprintf(`
284+
resource "sysdig_monitor_dashboard" "dashboard" {
285+
name = "TERRAFORM TEST - METRIC %s"
286+
description = "TERRAFORM TEST - METRIC %s"
287+
288+
scope {
289+
metric = "agent.id"
290+
comparator = "in"
291+
value = ["foo", "bar"]
292+
variable = "agent_id"
293+
}
294+
295+
scope {
296+
metric = "agent.name"
297+
comparator = "equals"
298+
value = ["name"]
299+
}
300+
301+
scope {
302+
metric = "kubernetes.namespace.name"
303+
variable = "k8_ns"
304+
}
305+
306+
panel {
307+
pos_x = 0
308+
pos_y = 0
309+
width = 12 # Maximum size: 24
310+
height = 6
311+
type = "timechart"
312+
name = "example panel"
313+
description = "description"
314+
315+
query {
316+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
317+
unit = "percent"
318+
display_info {
319+
display_name = "hostname"
320+
time_series_display_name_template = "{{host_hostname}}"
321+
type = "lines"
322+
}
323+
}
324+
query {
325+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent{ns_name=$k8s_ns}[$__interval]))"
326+
unit = "number"
327+
display_info {
328+
time_series_display_name_template = "{{host_hostname}}"
329+
type = "stackedArea"
330+
}
331+
}
332+
}
333+
334+
panel {
335+
pos_x = 12
336+
pos_y = 0
337+
width = 12
338+
height = 6
339+
type = "number"
340+
name = "example panel - 2"
341+
description = "description of panel 2"
342+
343+
query {
344+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
345+
unit = "time"
346+
}
347+
}
348+
349+
panel {
350+
pos_x = 12
351+
pos_y = 12
352+
width = 12
353+
height = 6
354+
type = "text"
355+
name = "example panel - 2"
356+
content = "description of panel 2"
357+
visible_title = true
358+
autosize_text = true
359+
transparent_background = true
360+
}
361+
}
362+
`, name, name)
363+
}

website/docs/r/monitor_dashboard.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ resource "sysdig_monitor_dashboard" "dashboard" {
4747
query {
4848
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
4949
unit = "number"
50+
display_info {
51+
display_name = "ct_name"
52+
time_series_display_name_template = "{{container_name}}"
53+
type = "lines"
54+
}
5055
}
5156
}
5257
@@ -166,10 +171,20 @@ The following arguments are supported:
166171

167172
* `promql` - (Required) The PromQL query. Must be a valid PromQL query with existing
168173
metrics in Sysdig Monitor.
169-
174+
170175
* `unit` - (Required) The type of metric for this query. Can be one of: `percent`, `data`, `data rate`,
171176
`number`, `number rate`, `time`.
172177

178+
* `display_info` - (Optional) Configure the time series display visualization for the selected query.
179+
180+
Nested scheme for `display_info`:
181+
182+
* `display_name` - (Optional) Configure the query display name summary, the text will appears as a title for the legend.
183+
184+
* `time_series_display_name_template` - (Required) Configure the display name of the time series for the query using text and any label values returned with the metric. For example: `CPU usage % for {{host}}`.
185+
186+
* `type` - (Required) Configure the visualization type in the timechart, can be `lines`, `stackedArea`, `stackedBar`
187+
173188

174189
### share
175190

0 commit comments

Comments
 (0)