-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…20702) * Convert httpjson input to a v2 cursor input * Add CHANGELOG entry * Fix format errors * Convert to stateless input and refactor: - Paginator takes care of requesting next page info - Rate limiter takes care of rate limiting requests - Date cursor takes care of keeping track of cursor state * Remove python tests * Do not fail if there is no next page * Refactor go integration tests to work with v2 input * Do suggested changes to input and tests * Update time.Periodic call with error return * Change test duration values * Sepparate sync test case * Create custon url config type * Change input.Run comment * Change input.Run to only return on context cancellation * Remove all usages of pkg/errors (cherry picked from commit 929e838)
- Loading branch information
Showing
14 changed files
with
1,263 additions
and
1,254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package httpjson | ||
|
||
import ( | ||
"bytes" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/logp" | ||
) | ||
|
||
type dateCursor struct { | ||
log *logp.Logger | ||
enabled bool | ||
field string | ||
url url.URL | ||
urlField string | ||
initialInterval time.Duration | ||
dateFormat string | ||
|
||
value string | ||
valueTpl *Template | ||
} | ||
|
||
func newDateCursorFromConfig(config config, log *logp.Logger) *dateCursor { | ||
c := &dateCursor{ | ||
enabled: config.DateCursor.IsEnabled(), | ||
url: *config.URL.URL, | ||
} | ||
|
||
if !c.enabled { | ||
return c | ||
} | ||
|
||
c.log = log | ||
c.field = config.DateCursor.Field | ||
c.urlField = config.DateCursor.URLField | ||
c.initialInterval = config.DateCursor.InitialInterval | ||
c.dateFormat = config.DateCursor.GetDateFormat() | ||
c.valueTpl = config.DateCursor.ValueTemplate | ||
|
||
return c | ||
} | ||
|
||
func (c *dateCursor) getURL() string { | ||
if !c.enabled { | ||
return c.url.String() | ||
} | ||
|
||
var dateStr string | ||
if c.value == "" { | ||
t := timeNow().UTC().Add(-c.initialInterval) | ||
dateStr = t.Format(c.dateFormat) | ||
} else { | ||
dateStr = c.value | ||
} | ||
|
||
q := c.url.Query() | ||
|
||
var value string | ||
if c.valueTpl == nil { | ||
value = dateStr | ||
} else { | ||
buf := new(bytes.Buffer) | ||
if err := c.valueTpl.Template.Execute(buf, dateStr); err != nil { | ||
return c.url.String() | ||
} | ||
value = buf.String() | ||
} | ||
|
||
q.Set(c.urlField, value) | ||
|
||
c.url.RawQuery = q.Encode() | ||
|
||
return c.url.String() | ||
} | ||
|
||
func (c *dateCursor) advance(m common.MapStr) { | ||
if c.field == "" { | ||
c.value = time.Now().UTC().Format(c.dateFormat) | ||
return | ||
} | ||
|
||
v, err := m.GetValue(c.field) | ||
if err != nil { | ||
c.log.Warnf("date_cursor field: %q", err) | ||
return | ||
} | ||
switch t := v.(type) { | ||
case string: | ||
_, err := time.Parse(c.dateFormat, t) | ||
if err != nil { | ||
c.log.Warn("date_cursor field does not have the expected layout") | ||
return | ||
} | ||
c.value = t | ||
default: | ||
c.log.Warn("date_cursor field must be a string, cursor will not advance") | ||
return | ||
} | ||
} |
Oops, something went wrong.