Skip to content

Commit 3e9a10d

Browse files
Fix failed to compare legacy queries error for malformed legacy queries in state (#182)
* repro * fix badness * comment * minimal test case * version
1 parent 83dee2d commit 3e9a10d

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

.go-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.83.0
1+
1.83.1

lightstep/legacy_query_equivalence.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lightstep
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"regexp"
78
"strings"
89

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

154157
priorUQL := make(map[string]string)

lightstep/resource_metric_condition_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,3 +1377,89 @@ func Test_buildLatencyPercentiles(t *testing.T) {
13771377
})
13781378
}
13791379
}
1380+
1381+
func TestAccSpanLatencyConditionInvalidQuery(t *testing.T) {
1382+
var condition client.UnifiedCondition
1383+
1384+
invalidQuery := "customer IN (\\\"test\\\")"
1385+
validQuery := "\\\"customer\\\" IN (\\\"test\\\")"
1386+
1387+
conditionConfigTemplate := `
1388+
resource "lightstep_slack_destination" "slack" {
1389+
project_name = "` + testProject + `"
1390+
channel = "#emergency-room"
1391+
}
1392+
1393+
resource "lightstep_metric_condition" "test" {
1394+
project_name = "` + testProject + `"
1395+
name = "Span latency alert"
1396+
1397+
expression {
1398+
is_multi = false
1399+
is_no_data = true
1400+
operand = "above"
1401+
thresholds {
1402+
critical = 10
1403+
warning = 5
1404+
}
1405+
}
1406+
1407+
metric_query {
1408+
hidden = false
1409+
query_name = "a"
1410+
display = "line"
1411+
spans {
1412+
query = "%s"
1413+
operator = "latency"
1414+
operator_input_window_ms = 3600000
1415+
latency_percentiles = [50]
1416+
}
1417+
1418+
final_window_operation {
1419+
operator = "min"
1420+
input_window_ms = 30000
1421+
}
1422+
}
1423+
1424+
alerting_rule {
1425+
id = lightstep_slack_destination.slack.id
1426+
update_interval = "1h"
1427+
}
1428+
}
1429+
`
1430+
1431+
resourceName := "lightstep_metric_condition.test"
1432+
resource.Test(t, resource.TestCase{
1433+
PreCheck: func() { testAccPreCheck(t) },
1434+
Providers: testAccProviders,
1435+
CheckDestroy: testAccMetricConditionDestroy,
1436+
Steps: []resource.TestStep{
1437+
// make a valid legacy span alert
1438+
{
1439+
Config: fmt.Sprintf(conditionConfigTemplate, validQuery),
1440+
Check: resource.ComposeTestCheckFunc(
1441+
testAccCheckMetricConditionExists(resourceName, &condition),
1442+
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", "\"customer\" IN (\"test\")"),
1443+
),
1444+
},
1445+
// attempt to update with an invalid query, this fails but stores the invalid query in state
1446+
{
1447+
Config: fmt.Sprintf(conditionConfigTemplate, invalidQuery),
1448+
Check: resource.ComposeTestCheckFunc(
1449+
testAccCheckMetricConditionExists(resourceName, &condition),
1450+
// the resource in state has an invalid query
1451+
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", invalidQuery),
1452+
),
1453+
ExpectError: regexp.MustCompile(".*Invalid query predicate.*"),
1454+
},
1455+
// fix the query, apply should succeed despite the invalid query in state
1456+
{
1457+
Config: fmt.Sprintf(conditionConfigTemplate, validQuery),
1458+
Check: resource.ComposeTestCheckFunc(
1459+
testAccCheckMetricConditionExists(resourceName, &condition),
1460+
resource.TestCheckResourceAttr(resourceName, "metric_query.0.spans.0.query", "\"customer\" IN (\"test\")"),
1461+
),
1462+
},
1463+
},
1464+
})
1465+
}

0 commit comments

Comments
 (0)