-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x-pack/filebeat/input/entityanalytics/provider/{azuread,okta}: add request tracing support #39821
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2714dab
x-pack/filebeat/input/entityanalytics/provider/okta: add request trac…
efd6 78f2fe1
x-pack/filebeat/input/entityanalytics/provider/azuread: add request t…
efd6 c1090de
add changelog entry
efd6 92139c1
add documentation
efd6 7a424cf
address pr comments
efd6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
1 change: 1 addition & 0 deletions
1
x-pack/filebeat/input/entityanalytics/provider/azuread/fetcher/graph/.gitignore
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 @@ | ||
*.ndjson |
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 |
---|---|---|
|
@@ -15,13 +15,19 @@ import ( | |
"io" | ||
"net/http" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"go.elastic.co/ecszap" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
|
||
"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/internal/collections" | ||
"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/provider/azuread/authenticator" | ||
"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/provider/azuread/fetcher" | ||
"github.com/elastic/beats/v7/x-pack/filebeat/input/internal/httplog" | ||
"github.com/elastic/elastic-agent-libs/config" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"github.com/elastic/elastic-agent-libs/mapstr" | ||
|
@@ -104,6 +110,9 @@ type graphConf struct { | |
Select selection `config:"select"` | ||
|
||
Transport httpcommon.HTTPTransportSettings `config:",inline"` | ||
|
||
// Tracer allows configuration of request trace logging. | ||
Tracer *lumberjack.Logger `config:"tracer"` | ||
} | ||
|
||
type selection struct { | ||
|
@@ -329,16 +338,22 @@ func (f *graph) doRequest(ctx context.Context, method, url string, body io.Reade | |
} | ||
|
||
// New creates a new instance of the graph fetcher. | ||
func New(cfg *config.C, logger *logp.Logger, auth authenticator.Authenticator) (fetcher.Fetcher, error) { | ||
func New(ctx context.Context, id string, cfg *config.C, logger *logp.Logger, auth authenticator.Authenticator) (fetcher.Fetcher, error) { | ||
var c graphConf | ||
if err := cfg.Unpack(&c); err != nil { | ||
return nil, fmt.Errorf("unable to unpack Graph API Fetcher config: %w", err) | ||
} | ||
|
||
if c.Tracer != nil { | ||
id = sanitizeFileName(id) | ||
c.Tracer.Filename = strings.ReplaceAll(c.Tracer.Filename, "*", id) | ||
} | ||
|
||
client, err := c.Transport.Client() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create HTTP client: %w", err) | ||
} | ||
client = requestTrace(ctx, client, c, logger) | ||
|
||
f := graph{ | ||
conf: c, | ||
|
@@ -383,6 +398,41 @@ func New(cfg *config.C, logger *logp.Logger, auth authenticator.Authenticator) ( | |
return &f, nil | ||
} | ||
|
||
// requestTrace decorates cli with an httplog.LoggingRoundTripper if cfg.Trace | ||
// is non-nil. | ||
func requestTrace(ctx context.Context, cli *http.Client, cfg graphConf, log *logp.Logger) *http.Client { | ||
if cfg.Tracer == nil { | ||
return cli | ||
} | ||
w := zapcore.AddSync(cfg.Tracer) | ||
go func() { | ||
// Close the logger when we are done. | ||
<-ctx.Done() | ||
cfg.Tracer.Close() | ||
}() | ||
core := ecszap.NewCore( | ||
ecszap.NewDefaultEncoderConfig(), | ||
w, | ||
zap.DebugLevel, | ||
) | ||
traceLogger := zap.New(core) | ||
|
||
const margin = 1e3 // 1OkB ought to be enough room for all the remainder of the trace details. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't 1e3 == 1kB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed it is 🤦. |
||
maxSize := cfg.Tracer.MaxSize * 1e6 | ||
cli.Transport = httplog.NewLoggingRoundTripper(cli.Transport, traceLogger, max(0, maxSize-margin), log) | ||
return cli | ||
} | ||
|
||
// sanitizeFileName returns name with ":" and "/" replaced with "_", removing | ||
// repeated instances. The request.tracer.filename may have ":" when an input | ||
// has cursor config and the macOS Finder will treat this as path-separator and | ||
// causes to show up strange filepaths. | ||
func sanitizeFileName(name string) string { | ||
name = strings.ReplaceAll(name, ":", string(filepath.Separator)) | ||
name = filepath.Clean(name) | ||
return strings.ReplaceAll(name, string(filepath.Separator), "_") | ||
} | ||
|
||
func formatQuery(name string, query []string, dflt string) string { | ||
q := dflt | ||
if len(query) != 0 { | ||
|
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 @@ | ||
*.ndjson |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.