Skip to content

Commit 2d32cd9

Browse files
committed
chore: Store state for persistent storage types
1 parent 76a5ca7 commit 2d32cd9

File tree

7 files changed

+11
-6
lines changed

7 files changed

+11
-6
lines changed

config/endpoint/condition_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestCondition_Validate(t *testing.T) {
4444
{condition: "[STATUS] = = 201", expectedErr: errors.New("invalid condition: [STATUS] = = 201")},
4545
{condition: "[STATUS] ==", expectedErr: errors.New("invalid condition: [STATUS] ==")},
4646
{condition: "[STATUS]", expectedErr: errors.New("invalid condition: [STATUS]")},
47+
// TODO#227 Test linked state conditions
4748
// FIXME: Should return an error, but doesn't because jsonpath isn't evaluated due to body being empty in Condition.Validate()
4849
//{condition: "len([BODY].users == 100", expectedErr: nil},
4950
}

config/endpoint/endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (e *Endpoint) EvaluateHealthWithContext(context *gontext.Gontext) *Result {
343343
} else { // Go over condition results to see if any of them has a specific state to set
344344
for _, conditionResult := range result.ConditionResults {
345345
if !conditionResult.Success && len(conditionResult.LinkedState) > 0 {
346-
result.State = conditionResult.LinkedState // TODO#227 Only set if no other state with h
346+
result.State = conditionResult.LinkedState // TODO#227 Only set if no other state with higher priority has been set
347347
break
348348
}
349349
}

config/endpoint/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Result struct {
3636
Success bool `json:"success"`
3737

3838
// State of the endpoint after evaluating the result
39-
State string `json:"state,omitempty"` // TODO#227 Omitempty correct?
39+
State string `json:"state"`
4040

4141
// Timestamp when the request was sent
4242
Timestamp time.Time `json:"timestamp"`

storage/store/sql/specific_postgres.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (s *Store) createPostgresSchema() error {
5656
endpoint_result_id BIGSERIAL PRIMARY KEY,
5757
endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
5858
success BOOLEAN NOT NULL,
59+
state TEXT NOT NULL,
5960
errors TEXT NOT NULL,
6061
connected BOOLEAN NOT NULL,
6162
status BIGINT NOT NULL,

storage/store/sql/specific_sqlite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (s *Store) createSQLiteSchema() error {
5656
endpoint_result_id INTEGER PRIMARY KEY,
5757
endpoint_id INTEGER NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
5858
success INTEGER NOT NULL,
59+
state TEXT NOT NULL,
5960
errors TEXT NOT NULL,
6061
connected INTEGER NOT NULL,
6162
status INTEGER NOT NULL,

storage/store/sql/sql.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,13 @@ func (s *Store) insertEndpointResultWithSuiteID(tx *sql.Tx, endpointID int64, re
623623
var endpointResultID int64
624624
err := tx.QueryRow(
625625
`
626-
INSERT INTO endpoint_results (endpoint_id, success, errors, connected, status, dns_rcode, certificate_expiration, domain_expiration, hostname, ip, duration, timestamp, suite_result_id)
627-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
626+
INSERT INTO endpoint_results (endpoint_id, success, state, errors, connected, status, dns_rcode, certificate_expiration, domain_expiration, hostname, ip, duration, timestamp, suite_result_id)
627+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
628628
RETURNING endpoint_result_id
629629
`,
630630
endpointID,
631631
result.Success,
632+
result.State,
632633
strings.Join(result.Errors, arraySeparator),
633634
result.Connected,
634635
result.HTTPStatus,
@@ -791,7 +792,7 @@ func (s *Store) getEndpointEventsByEndpointID(tx *sql.Tx, endpointID int64, page
791792
func (s *Store) getEndpointResultsByEndpointID(tx *sql.Tx, endpointID int64, page, pageSize int) (results []*endpoint.Result, err error) {
792793
rows, err := tx.Query(
793794
`
794-
SELECT endpoint_result_id, success, errors, connected, status, dns_rcode, certificate_expiration, domain_expiration, hostname, ip, duration, timestamp
795+
SELECT endpoint_result_id, success, state, errors, connected, status, dns_rcode, certificate_expiration, domain_expiration, hostname, ip, duration, timestamp
795796
FROM endpoint_results
796797
WHERE endpoint_id = $1
797798
ORDER BY endpoint_result_id DESC -- Normally, we'd sort by timestamp, but sorting by endpoint_result_id is faster
@@ -809,7 +810,7 @@ func (s *Store) getEndpointResultsByEndpointID(tx *sql.Tx, endpointID int64, pag
809810
result := &endpoint.Result{}
810811
var id int64
811812
var joinedErrors string
812-
err = rows.Scan(&id, &result.Success, &joinedErrors, &result.Connected, &result.HTTPStatus, &result.DNSRCode, &result.CertificateExpiration, &result.DomainExpiration, &result.Hostname, &result.IP, &result.Duration, &result.Timestamp)
813+
err = rows.Scan(&id, &result.Success, &result.State, &joinedErrors, &result.Connected, &result.HTTPStatus, &result.DNSRCode, &result.CertificateExpiration, &result.DomainExpiration, &result.Hostname, &result.IP, &result.Duration, &result.Timestamp)
813814
if err != nil {
814815
logr.Errorf("[sql.getEndpointResultsByEndpointID] Silently failed to retrieve endpoint result for endpointID=%d: %s", endpointID, err.Error())
815816
err = nil

watchdog/endpoint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func executeEndpoint(ep *endpoint.Endpoint, cfg *config.Config, extraLabels []st
5252
if inMaintenanceWindow && !result.Success {
5353
result.State = state.DefaultMaintenanceStateName
5454
}
55+
// TODO#227 Evaluate result.Success based on set states' healthiness configuration once that config option is implemented
5556
if cfg.Metrics {
5657
metrics.PublishMetricsForEndpoint(ep, result, extraLabels)
5758
}

0 commit comments

Comments
 (0)