Skip to content

Commit

Permalink
Fix failed to compare legacy queries error for malformed legacy queri…
Browse files Browse the repository at this point in the history
…es in state (#182)

* repro

* fix badness

* comment

* minimal test case

* version
  • Loading branch information
MisterSquishy authored Sep 13, 2023
1 parent 83dee2d commit 3e9a10d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.83.0
1.83.1
5 changes: 4 additions & 1 deletion lightstep/legacy_query_equivalence.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lightstep
import (
"context"
"fmt"
"log"
"regexp"
"strings"

Expand Down Expand Up @@ -148,7 +149,9 @@ func compareUpdatedLegacyQueries(
}
err := c.CallAPI(context.Background(), "POST", fmt.Sprintf("projects/%v/query_translation", projectName), req, &resp)
if err != nil {
return false, err
// don't short circuit; terraform saves invalid input in state, so we need to just assume the queries did
// change in this case (as long as the new ones are valid)
log.Printf("warning! legacy query translation failed for existsing queries: %s", err.Error())
}

priorUQL := make(map[string]string)
Expand Down
86 changes: 86 additions & 0 deletions lightstep/resource_metric_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,3 +1377,89 @@ func Test_buildLatencyPercentiles(t *testing.T) {
})
}
}

func TestAccSpanLatencyConditionInvalidQuery(t *testing.T) {
var condition client.UnifiedCondition

invalidQuery := "customer IN (\\\"test\\\")"
validQuery := "\\\"customer\\\" IN (\\\"test\\\")"

conditionConfigTemplate := `
resource "lightstep_slack_destination" "slack" {
project_name = "` + testProject + `"
channel = "#emergency-room"
}
resource "lightstep_metric_condition" "test" {
project_name = "` + testProject + `"
name = "Span latency alert"
expression {
is_multi = false
is_no_data = true
operand = "above"
thresholds {
critical = 10
warning = 5
}
}
metric_query {
hidden = false
query_name = "a"
display = "line"
spans {
query = "%s"
operator = "latency"
operator_input_window_ms = 3600000
latency_percentiles = [50]
}
final_window_operation {
operator = "min"
input_window_ms = 30000
}
}
alerting_rule {
id = lightstep_slack_destination.slack.id
update_interval = "1h"
}
}
`

resourceName := "lightstep_metric_condition.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccMetricConditionDestroy,
Steps: []resource.TestStep{
// make a valid legacy span alert
{
Config: fmt.Sprintf(conditionConfigTemplate, validQuery),
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricConditionExists(resourceName, &condition),
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", "\"customer\" IN (\"test\")"),
),
},
// attempt to update with an invalid query, this fails but stores the invalid query in state
{
Config: fmt.Sprintf(conditionConfigTemplate, invalidQuery),
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricConditionExists(resourceName, &condition),
// the resource in state has an invalid query
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", invalidQuery),
),
ExpectError: regexp.MustCompile(".*Invalid query predicate.*"),
},
// fix the query, apply should succeed despite the invalid query in state
{
Config: fmt.Sprintf(conditionConfigTemplate, validQuery),
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricConditionExists(resourceName, &condition),
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", "\"customer\" IN (\"test\")"),
),
},
},
})
}

0 comments on commit 3e9a10d

Please sign in to comment.