elastic / terraform-provider-elasticstack

Terraform provider for Elastic Stack
https://registry.terraform.io/providers/elastic/elasticstack/latest/docs
Apache License 2.0
155 stars 78 forks source link

[Bug] SLO Resource doesn't support 'range' starting from 0 #655

Open b-deam opened 1 month ago

b-deam commented 1 month ago

Describe the bug

The Provider treats the literal value of 0 as nil, and therefore doesn't pass it through to the Kibana API, resulting in a status code 400.

To Reproduce Apply this Terraform with TF_LOG="DEBUG":

resource "elasticstack_kibana_slo" "xp_upjet_ext_api_duration" {
  name        = "[Crossplane] Managed Resource External API Request Duration"
  description = "Measures in seconds how long it takes a Cloud SDK call to complete. This measures the time it takes for Crossplane Provider pods to complete external API requests, for example, the provider-aws-ec2 talks to AWS' EC2 API."
  slo_id      = "xp_upjet_ext_api_duration"

  histogram_custom_indicator {
    index = "metrics-*:metrics-*"
    good {
      field       = "prometheus.upjet_resource_ext_api_duration.histogram"
      aggregation = "range"
      from        = 0
      # 10s
      to = 10
    }
    total {
      field       = "prometheus.upjet_resource_ext_api_duration.histogram"
      aggregation = "range"
      from        = 0
      to          = 999999
    }
    filter          = "prometheus.upjet_resource_ext_api_duration.histogram: *"
    timestamp_field = "@timestamp"
  }

  time_window {
    duration = "30d"
    type     = "rolling"
  }

  budgeting_method = "occurrences"

  objective {
    target = 0.99
  }

  group_by = "orchestrator.cluster.name"

  tags = ["crossplane", "infra-mki"]
}

Note the 400 status code:

"statusCode": 400,
 "error": "Bad Request",
 "message": "Invalid value undefined supplied to :
<TRUNCATED>
{ field: string, aggregation: \"range\", from: number, to: number }/from: number"

I'm 99% sure the problem rests with this function: https://github.com/elastic/terraform-provider-elasticstack/blob/559478b04ae32152f4ed628d1868bb9964915c7e/internal/kibana/slo.go#L452-L458

Which is used when reading the schema here: https://github.com/elastic/terraform-provider-elasticstack/blob/559478b04ae32152f4ed628d1868bb9964915c7e/internal/kibana/slo.go#L529-L539

Expected behavior We should be able to specify a range starting from 0.

Debug output

Screenshots

Versions (please complete the following information):

Additional context Add any other context about the problem here.

b-deam commented 1 month ago

It may not work for all use-cases, but a possible workaround is to use a very low value for from, e.g.:

      from        = 0.000000000000000001