Skip to content

Commit

Permalink
Fix index pattern when querying ES and condition when searching logs (#…
Browse files Browse the repository at this point in the history
…3765)

Two issues were causing flakiness on integration tests and are fixed by this PR:
1. The pattern used to query ES was not working on serverless, this commit updates it to a pattern that works on both stateful and serverless as well as make it more specific to the indexes/data streams we want to query
2. `findESDocs` did not wait for the data to be indexed, only to a successful query on ES. In some cases the documents the test wanted were not indexed yet, leading to 0 documents being returned and the test failing with no error in the logs/diagnostics. This is fixed by waiting for a document count > 0 and no error.

(cherry picked from commit a03aa9c)

# Conflicts:
#	pkg/testing/tools/estools/elasticsearch.go
#	testing/integration/logs_ingestion_test.go
  • Loading branch information
belimawr authored and mergify[bot] committed Nov 28, 2023
1 parent 67a8d1b commit 9a25cb5
Show file tree
Hide file tree
Showing 2 changed files with 495 additions and 1 deletion.
39 changes: 38 additions & 1 deletion pkg/testing/tools/estools/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func FindMatchingLogLinesWithContext(ctx context.Context, client elastictranspor
return Documents{}, fmt.Errorf("error creating ES query: %w", err)
}

<<<<<<< HEAD

Check failure on line 140 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }

Check failure on line 140 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '<<' (typecheck)
es := esapi.New(client)
res, err := es.Search(
es.Search.WithIndex("*.ds-logs*"),
Expand All @@ -149,6 +150,9 @@ func FindMatchingLogLinesWithContext(ctx context.Context, client elastictranspor
if err != nil {
return Documents{}, fmt.Errorf("error performing ES search: %w", err)
}
=======
return performQueryForRawQuery(ctx, queryRaw, "logs-elastic_agent*", client)
>>>>>>> a03aa9cf19 (Fix index pattern when querying ES and condition when searching logs (#3765))

Check failure on line 155 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'

return handleDocsResponse(res)
}
Expand Down Expand Up @@ -221,6 +225,7 @@ func CheckForErrorsInLogsWithContext(ctx context.Context, client elastictranspor
return Documents{}, fmt.Errorf("error creating ES query: %w", err)
}

<<<<<<< HEAD

Check failure on line 228 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }
es := esapi.New(client)
res, err := es.Search(
es.Search.WithIndex("*.ds-logs*"),
Expand All @@ -235,6 +240,9 @@ func CheckForErrorsInLogsWithContext(ctx context.Context, client elastictranspor
}

return handleDocsResponse(res)
=======
return performQueryForRawQuery(ctx, queryRaw, "logs-elastic_agent*", client)
>>>>>>> a03aa9cf19 (Fix index pattern when querying ES and condition when searching logs (#3765))

Check failure on line 245 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'
}

// GetLogsForDatastream returns any logs associated with the datastream
Expand All @@ -260,7 +268,7 @@ func GetLogsForAgentID(client elastictransport.Interface, id string) (Documents,

es := esapi.New(client)
res, err := es.Search(
es.Search.WithIndex("*.ds-logs*"),
es.Search.WithIndex("logs-elastic_agent*"),
es.Search.WithExpandWildcards("all"),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
Expand All @@ -287,6 +295,35 @@ func GetLogsForDatastreamWithContext(ctx context.Context, client elastictranspor
},
}

<<<<<<< HEAD

Check failure on line 298 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }
=======
return performQueryForRawQuery(ctx, indexQuery, "logs-elastic_agent*", client)
}

// GetPing performs a basic ping and returns ES config info
func GetPing(ctx context.Context, client elastictransport.Interface) (Ping, error) {
req := esapi.InfoRequest{}
resp, err := req.Do(ctx, client)
if err != nil {
return Ping{}, fmt.Errorf("error in ping request")
}
defer resp.Body.Close()

respData, err := handleResponseRaw(resp)
if err != nil {
return Ping{}, fmt.Errorf("error in HTTP response: %w", err)
}
pingData := Ping{}
err = json.Unmarshal(respData, &pingData)
if err != nil {
return pingData, fmt.Errorf("error unmarshalling JSON: %w", err)
}
return pingData, nil

}

func performQueryForRawQuery(ctx context.Context, queryRaw map[string]interface{}, index string, client elastictransport.Interface) (Documents, error) {
>>>>>>> a03aa9cf19 (Fix index pattern when querying ES and condition when searching logs (#3765))

Check failure on line 326 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected >>, expected }

Check failure on line 326 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(indexQuery)

Check failure on line 328 in pkg/testing/tools/estools/elasticsearch.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: non-declaration statement outside function body (typecheck)
if err != nil {
Expand Down
Loading

0 comments on commit 9a25cb5

Please sign in to comment.