Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Adaptive Metrics #1027

Closed
Hronom opened this issue Sep 10, 2023 · 3 comments
Closed

Add support for Adaptive Metrics #1027

Hronom opened this issue Sep 10, 2023 · 3 comments

Comments

@Hronom
Copy link

Hronom commented Sep 10, 2023

Need support for Adaptive Metrics in terraform.

Here is the current workaround that I use:

locals {
  authorization_base64 = base64encode("${var.GRAFANA_ADAPTIVE_METRICS_TENANT}:${var.GRAFANA_ADAPTIVE_METRICS_TOKEN}")
}

# In order to "Upload new aggregation rules" we need to follow two step process described here
# https://grafana.com/docs/grafana-cloud/account-management/billing-and-usage/control-metrics-usage-via-adaptive-metrics/#upload-new-aggregation-rules
# There special header Etag that we need to get from previous upload and put value from it in header If-Match
data "http" "current_adaptive_metrics_aggregation_rules" {
  url = "${var.GRAFANA_ADAPTIVE_METRICS_URL}/aggregations/rules"

  method = "GET"

  # Optional request headers
  request_headers = {
    Accept        = "application/json"
    Authorization = "Basic ${local.authorization_base64}"
  }
}

data "http" "new_adaptive_metrics_aggregation_rules" {
  url = "${var.GRAFANA_ADAPTIVE_METRICS_URL}/aggregations/rules"

  method = "POST"

  # Optional request headers
  request_headers = {
    Accept        = "application/json"
    Authorization = "Basic ${local.authorization_base64}"
    If-Match      = data.http.current_adaptive_metrics_aggregation_rules.response_headers.Etag
  }

  request_body = var.grafana_adaptive_metrics_recommendations
}

# Inspired by https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http#usage-with-provisioner
resource "null_resource" "new_adaptive_metrics_aggregation_rules_check" {
  # On success, this will attempt to execute the true command in the
  # shell environment running terraform.
  # On failure, this will attempt to execute the false command in the
  # shell environment running terraform.
  provisioner "local-exec" {
    command = contains([200], data.http.new_adaptive_metrics_aggregation_rules.status_code)
  }
}

This solution is the hack that applies during plan phase instead of apply.

@jacobstr
Copy link

Thanks for this workaround @Hronom. I suspect we're going to run head first into this as well, we try to manage a subset of Grafana using IAC and this certainly looks to fall under that subset. local-exec provisioners do make me cringe a bit whenever I have to use them - would be lovely to have a well-trodden path built into the provider for this.

@grafuzer
Copy link

@Hronom we are working on it!

See the project here Terraform Provider for Adaptive Metrics: https://github.com/grafana/terraform-provider-grafana-adaptive-metrics

@julienduchesne
Copy link
Member

A first release of the provider went out today: https://registry.terraform.io/providers/grafana/grafana-adaptive-metrics/latest 🎉. You can direct your questions and file issues in its provider from now on: https://github.com/grafana/terraform-provider-grafana-adaptive-metrics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants