grafana / terraform-provider-schemas

Mozilla Public License 2.0
32 stars 1 forks source link

feedback/bug: time_series panel #41

Open eraac opened 1 year ago

eraac commented 1 year ago

I'm not sure is the good place to send a feedback, but I can rewrite this somewhere if needed

First of all, I was very excited by this provider when the announce come, and after few hours to try to reproduce some mimic of our custom "dashboard-builder" I'm still excited by the capabilities of this.

I have try to reproduce a small part of our custom code to do the same thing, a very simple dashboard with 3 panels (1 text, 2 timeseries), and after few hacks/workaround I was able to reproduce the same dashboard and I'm quitte impressed by this.

Here some of difficulty encounters

terraform code **disclaimer**: my workaround are dirty ```hcl data "schemas_core_dashboard" "my_dashboard" { title = "My Dashboard" time = { from = "now-1h" } # the replace with the regex remove keys from the JSON with value '-1' or '"-1"', to unset key and lets Grafana use default value panels = [ data.schemas_panel_text.title.rendered_json, replace( jsonencode( merge( # fieldConfig.override can't be set via the `data.schemas_panel_time_series`, so we inject the object manually, and deal with merge function who erase others key of an overridden map jsondecode(data.schemas_panel_time_series.publish_rate.rendered_json), { fieldConfig = { defaults = jsondecode(data.schemas_panel_time_series.publish_rate.rendered_json)["fieldConfig"]["defaults"], overrides = [{ matcher = { id = "byName", options = "message rate" }, properties = [{ id = "custom.axisPlacement", value = "right" }, { id = "unit", value = "ops" }] }] } } ) ), "/\"[a-zA-Z_]+\":(| )\"?-1\"?,?/", "" ), replace(data.schemas_panel_time_series.average_message_size.rendered_json, "/\"[a-zA-Z_]+\":(| )\"?-1\"?,?/", ""), ] } data "schemas_panel_text" "title" { transparent = true options = { content = "

\n\tmy.topic\n

" } grid_pos = { h = 2 w = 24 } } data "schemas_panel_time_series" "publish_rate" { title = "Publish rate" description = "left: publish request per second
\nright: message publish per second" datasource = { type = "stackdriver" uid = local.datasource_gcp } field_config = { defaults = { custom = { show_points = "never", draw_style = "line", line_interpolation = "linear", line_width = 1, gradient_mode = "none", span_nulls = false, point_size = 5, axis_placement = "auto", axis_color_mode = "text", axis_grid_show = true, axis_width = -1, axis_soft_min = -1 axis_soft_max = -1, } unit = "reqps", color = { mode = "palette-classic" } decimals = -1, min = 0, max = -1, no_value = "-1", } # doesn't work overrides = [ { matcher = { id = "byName", options = "message rate" } properties = [ { id = "custom.axisPlacement", value = "right" }, { id = "unit", value = "ops" } ] } ] } options = { tooltip = { mode = "multi", sort = "none", } legend = { show_legend = true, display_mode = "table", placement = "bottom", calcs = ["mean"], } } targets = [ "{\"aliasBy\":\"publish rate\",\"datasource\":{\"type\":\"stackdriver\",\"uid\":\"xxxx\"},\"queryType\":\"timeSeriesList\",\"refId\":\"A\",\"timeSeriesList\":{\"alignmentPeriod\":\"cloud-monitoring-auto\",\"crossSeriesReducer\":\"REDUCE_NONE\",\"filters\":[\"resource.label.topic_id\",\"=\",\"my.topic\",\"AND\",\"metric.type\",\"=\",\"pubsub.googleapis.com/topic/send_request_count\"],\"groupBys\":[],\"perSeriesAligner\":\"ALIGN_MEAN\",\"preprocessor\":\"rate\",\"projectName\":\"my-project\"}}", "{\"aliasBy\":\"message rate\",\"datasource\":{\"type\":\"stackdriver\",\"uid\":\"xxxxx\"},\"hide\":false,\"queryType\":\"timeSeriesList\",\"refId\":\"B\",\"timeSeriesList\":{\"alignmentPeriod\":\"cloud-monitoring-auto\",\"crossSeriesReducer\":\"REDUCE_NONE\",\"filters\":[\"resource.label.topic_id\",\"=\",\"my.topic\",\"AND\",\"metric.type\",\"=\",\"pubsub.googleapis.com/topic/send_message_operation_count\"],\"groupBys\":[],\"perSeriesAligner\":\"ALIGN_NONE\",\"preprocessor\":\"rate\",\"projectName\":\"my-project\"}}" ] grid_pos = { h = 9 w = 12 x = 0 } } data "schemas_panel_time_series" "average_message_size" { title = "Average message size" description = "Average message size in this topic grouped by minute" datasource = { type = "stackdriver" uid = local.datasource_gcp } field_config = { defaults = { custom = { show_points = "never", draw_style = "line", line_interpolation = "linear", line_width = 1, gradient_mode = "none", span_nulls = false, point_size = 5, axis_placement = "auto", axis_color_mode = "text", axis_grid_show = true, axis_width = -1, axis_soft_min = -1 axis_soft_max = -1, } min = 0, max = -1, no_value = "-1", } } options = { tooltip = { mode = "multi", sort = "none", } legend = { show_legend = false, display_mode = "table", placement = "bottom", } } targets = ["{\"aliasBy\":\"size\",\"datasource\":{\"type\":\"stackdriver\",\"uid\":\"xxxxxx\"},\"queryType\":\"timeSeriesList\",\"refId\":\"A\",\"timeSeriesList\":{\"alignmentPeriod\":\"cloud-monitoring-auto\",\"crossSeriesReducer\":\"REDUCE_MEAN\",\"filters\":[\"resource.label.topic_id\",\"=\",\"my.topic\",\"AND\",\"metric.type\",\"=\",\"pubsub.googleapis.com/topic/message_sizes\"],\"groupBys\":[],\"perSeriesAligner\":\"ALIGN_SUM\",\"preprocessor\":\"none\",\"projectName\":\"my-project\"}}"] grid_pos = { h = 9 w = 12 x = 12 } } resource "grafana_dashboard" "example" { folder = local.folder_uid config_json = data.schemas_core_dashboard.my_dashboard.rendered_json } ```
AgnesToulet commented 1 year ago

Hi! Thanks for this great feedback, this is the perfect place to post it.

it's seems there is a constant drift with the grafana_dashboard resource, because of the uid

  • I don't understand this point, could you please elaborate?

Thanks again for this detailed and clear feedback!

eraac commented 1 year ago

I don't understand this point, could you please elaborate?

Yes, indeed it was incomplete. When you combine schemas_core_dashboard and grafana_dashboard, if the attribute uid isn't set explicitly in the first one, terraform plan|apply will show constant diff between the current state and the terraform state

image

data "schemas_core_dashboard" "main" {
  # value not set
  #uid   = "my-uid"

  ...
}

resource "grafana_dashboard" "main" {
  ... 

  # here uid isn't set, but set in Grafana (auto generate), so the provider consider there is a difference
  config_json = data.schemas_core_dashboard.main.rendered_json
}

If you apply with the diff, nothing change (Grafana seems smart enough to re-use the current uid)