@@ -30,6 +30,7 @@ import (
3030 inputcursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor"
3131 "github.com/elastic/beats/v7/libbeat/beat"
3232 "github.com/elastic/beats/v7/libbeat/feature"
33+ "github.com/elastic/beats/v7/libbeat/management/status"
3334 "github.com/elastic/beats/v7/libbeat/monitoring/inputmon"
3435 "github.com/elastic/beats/v7/libbeat/statestore"
3536 "github.com/elastic/beats/v7/libbeat/version"
@@ -175,8 +176,14 @@ func runWithMetrics(ctx v2.Context, cfg config, pub inputcursor.Publisher, crsr
175176}
176177
177178func run (ctx v2.Context , cfg config , pub inputcursor.Publisher , crsr * inputcursor.Cursor , reg * monitoring.Registry ) error {
178- log := ctx .Logger .With ("input_url" , cfg .Request .URL )
179+ stat := ctx .StatusReporter
180+ if stat == nil {
181+ stat = noopReporter {}
182+ }
183+ stat .UpdateStatus (status .Starting , "" )
184+ stat .UpdateStatus (status .Configuring , "" )
179185
186+ log := ctx .Logger .With ("input_url" , cfg .Request .URL )
180187 stdCtx := ctxtool .FromCanceller (ctx .Cancelation )
181188
182189 if cfg .Request .Tracer != nil {
@@ -196,30 +203,33 @@ func run(ctx v2.Context, cfg config, pub inputcursor.Publisher, crsr *inputcurso
196203
197204 metrics := newInputMetrics (reg )
198205
199- client , err := newHTTPClient (stdCtx , cfg , log , reg )
206+ client , err := newHTTPClient (stdCtx , cfg , stat , log , reg )
200207 if err != nil {
208+ stat .UpdateStatus (status .Failed , "failed to create HTTP client: " + err .Error ())
201209 return err
202210 }
203211
204- requestFactory , err := newRequestFactory (stdCtx , cfg , log , metrics , reg )
212+ requestFactory , err := newRequestFactory (stdCtx , cfg , stat , log , metrics , reg )
205213 if err != nil {
206214 log .Errorf ("Error while creating requestFactory: %v" , err )
215+ stat .UpdateStatus (status .Failed , "failed to create request factory: " + err .Error ())
207216 return err
208217 }
209218 var xmlDetails map [string ]xml.Detail
210219 if cfg .Response .XSD != "" {
211220 xmlDetails , err = xml .Details ([]byte (cfg .Response .XSD ))
212221 if err != nil {
213222 log .Errorf ("error while collecting xml decoder type hints: %v" , err )
223+ stat .UpdateStatus (status .Failed , "error while collecting xml decoder type hints: " + err .Error ())
214224 return err
215225 }
216226 }
217- pagination := newPagination (cfg , client , log )
218- responseProcessor := newResponseProcessor (cfg , pagination , xmlDetails , metrics , log )
219- requester := newRequester (client , requestFactory , responseProcessor , metrics , log )
227+ pagination := newPagination (cfg , client , stat , log )
228+ responseProcessor := newResponseProcessor (cfg , pagination , xmlDetails , metrics , stat , log )
229+ requester := newRequester (client , requestFactory , responseProcessor , metrics , stat , log )
220230
221231 trCtx := emptyTransformContext ()
222- trCtx .cursor = newCursor (cfg .Cursor , log )
232+ trCtx .cursor = newCursor (cfg .Cursor , stat , log )
223233 trCtx .cursor .load (crsr )
224234
225235 doFunc := func () error {
@@ -241,6 +251,7 @@ func run(ctx v2.Context, cfg config, pub inputcursor.Publisher, crsr *inputcurso
241251 metrics .updateIntervalMetrics (err , startTime )
242252
243253 if err := stdCtx .Err (); err != nil {
254+ stat .UpdateStatus (status .Stopping , "" )
244255 return err
245256 }
246257
@@ -254,10 +265,14 @@ func run(ctx v2.Context, cfg config, pub inputcursor.Publisher, crsr *inputcurso
254265 }
255266
256267 log .Infof ("Input stopped because context was cancelled with: %v" , err )
257-
268+ stat . UpdateStatus ( status . Stopped , "" )
258269 return nil
259270}
260271
272+ type noopReporter struct {}
273+
274+ func (noopReporter ) UpdateStatus (status.Status , string ) {}
275+
261276// sanitizeFileName returns name with ":" and "/" replaced with "_", removing repeated instances.
262277// The request.tracer.filename may have ":" when a httpjson input has cursor config and
263278// the macOS Finder will treat this as path-separator and causes to show up strange filepaths.
@@ -267,7 +282,7 @@ func sanitizeFileName(name string) string {
267282 return strings .ReplaceAll (name , string (filepath .Separator ), "_" )
268283}
269284
270- func newHTTPClient (ctx context.Context , config config , log * logp.Logger , reg * monitoring.Registry ) (* httpClient , error ) {
285+ func newHTTPClient (ctx context.Context , config config , stat status. StatusReporter , log * logp.Logger , reg * monitoring.Registry ) (* httpClient , error ) {
271286 client , err := newNetHTTPClient (ctx , config .Request , log , reg )
272287 if err != nil {
273288 return nil , err
@@ -286,7 +301,7 @@ func newHTTPClient(ctx context.Context, config config, log *logp.Logger, reg *mo
286301 }).StandardClient ()
287302 }
288303
289- limiter := newRateLimiterFromConfig (config .Request .RateLimit , log )
304+ limiter := newRateLimiterFromConfig (config .Request .RateLimit , stat , log )
290305
291306 if config .Auth .OAuth2 .isEnabled () {
292307 authClient , err := config .Auth .OAuth2 .client (ctx , client )
@@ -356,7 +371,7 @@ func newNetHTTPClient(ctx context.Context, cfg *requestConfig, log *logp.Logger,
356371 return netHTTPClient , nil
357372}
358373
359- func newChainHTTPClient (ctx context.Context , authCfg * authConfig , requestCfg * requestConfig , log * logp.Logger , reg * monitoring.Registry , p ... * Policy ) (* httpClient , error ) {
374+ func newChainHTTPClient (ctx context.Context , authCfg * authConfig , requestCfg * requestConfig , stat status. StatusReporter , log * logp.Logger , reg * monitoring.Registry , p ... * Policy ) (* httpClient , error ) {
360375 client , err := newNetHTTPClient (ctx , requestCfg , log , reg )
361376 if err != nil {
362377 return nil , err
@@ -382,7 +397,7 @@ func newChainHTTPClient(ctx context.Context, authCfg *authConfig, requestCfg *re
382397 }).StandardClient ()
383398 }
384399
385- limiter := newRateLimiterFromConfig (requestCfg .RateLimit , log )
400+ limiter := newRateLimiterFromConfig (requestCfg .RateLimit , stat , log )
386401
387402 if authCfg != nil && authCfg .OAuth2 .isEnabled () {
388403 authClient , err := authCfg .OAuth2 .client (ctx , client )
0 commit comments