@@ -7,6 +7,7 @@ package ingest
7
7
import (
8
8
"bytes"
9
9
"encoding/json"
10
+ "errors"
10
11
"fmt"
11
12
"io"
12
13
"net/http"
@@ -22,18 +23,22 @@ type simulatePipelineRequest struct {
22
23
}
23
24
24
25
type simulatePipelineResponse struct {
25
- Docs []pipelineIngestedDocument `json:"docs"`
26
+ Docs []struct {
27
+ ProcessorResults []verboseProcessorResult `json:"processor_results"`
28
+ }
29
+ }
30
+
31
+ type verboseProcessorResult struct {
32
+ Processor string `json:"processor_type"`
33
+ Status string `json:"status"`
34
+ Doc pipelineDocument `json:"doc"`
26
35
}
27
36
28
37
type pipelineDocument struct {
29
38
Index string `json:"_index"`
30
39
Source json.RawMessage `json:"_source"`
31
40
}
32
41
33
- type pipelineIngestedDocument struct {
34
- Doc pipelineDocument `json:"doc"`
35
- }
36
-
37
42
// Pipeline represents a pipeline resource loaded from a file
38
43
type Pipeline struct {
39
44
Path string // Path of the file with the pipeline definition.
@@ -86,9 +91,10 @@ func SimulatePipeline(api *elasticsearch.API, pipelineName string, events []json
86
91
return nil , fmt .Errorf ("marshalling simulate request failed: %w" , err )
87
92
}
88
93
89
- r , err := api .Ingest .Simulate (bytes .NewReader (requestBody ), func (request * elasticsearch.IngestSimulateRequest ) {
90
- request .PipelineID = pipelineName
91
- })
94
+ r , err := api .Ingest .Simulate (bytes .NewReader (requestBody ),
95
+ api .Ingest .Simulate .WithPipelineID (pipelineName ),
96
+ api .Ingest .Simulate .WithVerbose (true ),
97
+ )
92
98
if err != nil {
93
99
return nil , fmt .Errorf ("simulate API call failed (pipelineName: %s): %w" , pipelineName , err )
94
100
}
@@ -110,10 +116,29 @@ func SimulatePipeline(api *elasticsearch.API, pipelineName string, events []json
110
116
}
111
117
112
118
processedEvents := make ([]json.RawMessage , len (response .Docs ))
119
+ var errs []error
113
120
for i , doc := range response .Docs {
114
- processedEvents [i ] = doc .Doc .Source
121
+ var source json.RawMessage
122
+ failed := false
123
+ for _ , result := range doc .ProcessorResults {
124
+ switch result .Status {
125
+ case "success" :
126
+ // Keep last successful document.
127
+ source = result .Doc .Source
128
+ case "skipped" :
129
+ continue
130
+ case "failed" :
131
+ failed = true
132
+ errs = append (errs , fmt .Errorf ("%q processor failed (status: %s)" , result .Processor , result .Status ))
133
+ }
134
+ }
135
+
136
+ if ! failed {
137
+ processedEvents [i ] = source
138
+ }
115
139
}
116
- return processedEvents , nil
140
+
141
+ return processedEvents , errors .Join (errs ... )
117
142
}
118
143
119
144
func UninstallPipelines (api * elasticsearch.API , pipelines []Pipeline ) error {
0 commit comments