Skip to content

Commit

Permalink
Merge pull request #202 from tosuke/query-dashboard
Browse files Browse the repository at this point in the history
Labeled metrics for dashboards
  • Loading branch information
Arthur1 committed Jun 14, 2024
2 parents 8deb0c8 + 7d9fe6d commit 577340c
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 50 deletions.
6 changes: 6 additions & 0 deletions docs/data-sources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ data "mackerel_dashboard" "this" {
* `name` - The name of graph.
* `expression` - The expression graph.
* `expression` - The expression for graphs.
* `query` - The query graph.
* `query` - The PromQL-style query.
* `legend` - The query legend.
* `range` - The display period for graphs. If unspecified, it will be variable and the display period can be changed from the controller displayed at the top of the dashboard.
* `relative` - (The period from (current time + `offset` - `period`) to (current time + `offset`) is displayed. Negative values for `offset` can be used to display graphs for a specified period in the past.
* `period` - Duration (seconds).
Expand All @@ -65,6 +68,9 @@ data "mackerel_dashboard" "this" {
* `name` - The name of metric.
* `expression` - The expression metric.
* `expression` - The expression for metric.
* `query` - The query metric.
* `query` - The PromQL-style query.
* `legend` - The query legend.
* `fraction_size` - Number of decimal places to display (0-16).
* `suffix` - Units to be displayed after the numerical value.
* `layout` - The coordinates are specified with the upper left corner of the widget display area as the origin (x = 0, y = 0), with the x axis in the right direction and they axis in the down direction as the positive direction.
Expand Down
6 changes: 6 additions & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ resource "mackerel_dashboard" "alert_status" {
* `name` - (Required) The name of graph.
* `expression` - The expression graph.
* `expression` - (Required) The expression for graphs.
* `query` - The query graph.
* `query` - (Required) The PromQL-style query.
* `legend` - The query legend.
* `range` - The display period for graphs. If unspecified, it will be variable and thedisplay period can be changed from the controller displayed at the top of the dashboard.
* `relative` - (The period from (current time + `offset` - `period`) to (current time + `offset`) is displayed. Negative values for `offset` can be used to display graphs for a specified period in the past.
* `period` - (Required) Duration (seconds).
Expand All @@ -180,6 +183,9 @@ resource "mackerel_dashboard" "alert_status" {
* `name` - (Required) The name of metric.
* `expression` - The expression metric.
* `expression` - (Required) The expression for metric.
* `query` - The query metric.
* `query` - (Required) The PromQL-style query.
* `legend` - The query legend.
* `fraction_size` - Number of decimal places to display (0-16).
* `suffix` - Units to be displayed after the numerical value.
* `layout` - (Required) The coordinates are specified with the upper left corner of the widget display area as the origin (x = 0, y = 0), with the x axis in the right direction and they axis in the down direction as the positive direction.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-mux v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/mackerelio/mackerel-client-go v0.31.0
github.com/mackerelio/mackerel-client-go v0.33.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ github.com/leonklingele/grouper v1.1.0/go.mod h1:uk3I3uDfi9B6PeUjsCKi6ndcf63Uy7s
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lufeee/execinquery v1.2.1 h1:hf0Ems4SHcUGBxpGN7Jz78z1ppVkP/837ZlETPCEtOM=
github.com/lufeee/execinquery v1.2.1/go.mod h1:EC7DrEKView09ocscGHC+apXMIaorh4xqSxS/dy8SbM=
github.com/mackerelio/mackerel-client-go v0.31.0 h1:oUBwuJzZkvhSFf7C7zpOBej8jM0kITUz7eVMAINIfO4=
github.com/mackerelio/mackerel-client-go v0.31.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo=
github.com/mackerelio/mackerel-client-go v0.33.0 h1:dMv0pyR3Exvtxxq3l7xzGF6D/5ky2Jgftzmp4aAoFYg=
github.com/mackerelio/mackerel-client-go v0.33.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI=
Expand Down
32 changes: 32 additions & 0 deletions mackerel/data_source_mackerel_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ var dashboardMetricDataResource = &schema.Resource{
},
},
},
"query": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Computed: true,
},
"legend": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}

Expand Down Expand Up @@ -216,6 +232,22 @@ func dataSourceMackerelDashboard() *schema.Resource {
},
},
},
"query": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Computed: true,
},
"legend": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"range": {
Type: schema.TypeList,
Computed: true,
Expand Down
91 changes: 90 additions & 1 deletion mackerel/data_source_mackerel_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccDataSourceMackerelDashboardGraph(t *testing.T) {
resource.TestCheckResourceAttr(dsName, "title", title),
resource.TestCheckResourceAttr(dsName, "memo", "This dashboard is managed by Terraform."),
resource.TestCheckResourceAttr(dsName, "url_path", rand),
resource.TestCheckResourceAttr(dsName, "graph.#", "2"),
resource.TestCheckResourceAttr(dsName, "graph.#", "3"),
resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dsName, "graph.0.title", "test graph role"),
resource.TestCheckResourceAttr(dsName, "graph.0.role.0.role_fullname", fmt.Sprintf("tf-service-%s-include:tf-role-%s-include", rand, rand)),
Expand All @@ -43,6 +43,15 @@ func TestAccDataSourceMackerelDashboardGraph(t *testing.T) {
resource.TestCheckResourceAttr(dsName, "graph.1.layout.0.y", "32"),
resource.TestCheckResourceAttr(dsName, "graph.1.layout.0.width", "10"),
resource.TestCheckResourceAttr(dsName, "graph.1.layout.0.height", "8"),
resource.TestCheckResourceAttr(dsName, "graph.2.title", "test graph query"),
resource.TestCheckResourceAttr(dsName, "graph.2.query.0.query", "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"),
resource.TestCheckResourceAttr(dsName, "graph.2.query.0.legend", "{{k8s.node.name}}"),
resource.TestCheckResourceAttr(dsName, "graph.2.range.0.relative.0.period", "3600"),
resource.TestCheckResourceAttr(dsName, "graph.2.range.0.relative.0.offset", "1800"),
resource.TestCheckResourceAttr(dsName, "graph.2.layout.0.x", "0"),
resource.TestCheckResourceAttr(dsName, "graph.2.layout.0.y", "52"),
resource.TestCheckResourceAttr(dsName, "graph.2.layout.0.width", "10"),
resource.TestCheckResourceAttr(dsName, "graph.2.layout.0.height", "8"),
),
),
},
Expand Down Expand Up @@ -79,6 +88,27 @@ func TestAccDataSourceMackerelDashboardValue(t *testing.T) {
),
),
},
{
Config: testAccDataSourceMackerelDashboardConfigValue_Query(rand, title),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dsName, "id"),
resource.TestCheckResourceAttr(dsName, "title", title),
resource.TestCheckResourceAttr(dsName, "memo", "This dashboard is managed by Terraform."),
resource.TestCheckResourceAttr(dsName, "url_path", rand),
resource.TestCheckResourceAttr(dsName, "value.#", "1"),
resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dsName, "value.0.title", "test value query"),
resource.TestCheckResourceAttr(dsName, "value.0.metric.0.query.0.query", "avg(avg_over_time(container.cpu.utilization{k8s.deployment.name=\"httpbin\"}[1h]))"),
resource.TestCheckResourceAttr(dsName, "value.0.metric.0.query.0.legend", "average utilization"),
resource.TestCheckResourceAttr(dsName, "value.0.fraction_size", "10"),
resource.TestCheckResourceAttr(dsName, "value.0.suffix", "test query suffix"),
resource.TestCheckResourceAttr(dsName, "value.0.layout.0.x", "3"),
resource.TestCheckResourceAttr(dsName, "value.0.layout.0.y", "15"),
resource.TestCheckResourceAttr(dsName, "value.0.layout.0.width", "3"),
resource.TestCheckResourceAttr(dsName, "value.0.layout.0.height", "4"),
),
),
},
},
})
}
Expand Down Expand Up @@ -204,6 +234,25 @@ resource "mackerel_dashboard" "foo" {
height = 8
}
}
graph {
title = "test graph query"
query {
query = "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"
legend = "{{k8s.node.name}}"
}
range {
relative {
period = 3600
offset = 1800
}
}
layout {
x = 0
y = 52
width = 10
height = 8
}
}
}
Expand Down Expand Up @@ -252,6 +301,46 @@ data "mackerel_dashboard" "foo" {
`, rand, rand, title, rand)
}

func testAccDataSourceMackerelDashboardConfigValue_Query(rand, title string) string {
return fmt.Sprintf(`
resource "mackerel_service" "include" {
name = "tf-service-%s-include"
}
resource "mackerel_role" "include" {
service = mackerel_service.include.name
name = "tf-role-%s-include"
}
resource "mackerel_dashboard" "foo" {
title = "%s"
memo = "This dashboard is managed by Terraform."
url_path = "%s"
value {
title = "test value query"
metric {
query {
query = "avg(avg_over_time(container.cpu.utilization{k8s.deployment.name=\"httpbin\"}[1h]))"
legend = "average utilization"
}
}
fraction_size = 10
suffix = "test query suffix"
layout {
x = 3
y = 15
width = 3
height = 4
}
}
}
data "mackerel_dashboard" "foo" {
id = mackerel_dashboard.foo.id
}
`, rand, rand, title, rand)
}

func testAccDataSourceMackerelDashboardConfigMarkdown(rand, title string) string {
return fmt.Sprintf(`
resource "mackerel_dashboard" "foo" {
Expand Down
Loading

0 comments on commit 577340c

Please sign in to comment.