-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Implement Python API for setting check metadata #4686
Conversation
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
Codecov Report
|
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.
Looks good, some minor questions.
datadog_checks_base/datadog_checks/base/utils/metadata/utils.py
Outdated
Show resolved
Hide resolved
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.
This is great, just a couple minor points:
- I agree with Thomas, having a default blacklist to prevent passwords and other secrets to be submitted would help.
- The backend will only receive json formatted data for configs. Do we know how well that is supported? I think this is a good idea because we don't want to index every single field, but I'm not sure how the backend would handle JSON
@FlorianVeaux As far as I know this will be supported, but the backend parsing has yet to be written |
What does this PR do?
API
This adds a method
set_metadata
to theAgentCheck
base class that updates cached metadata values, which are then sent by the Agent at regular intervals.It requires 2 arguments:
name
- the name of the metadatavalue
- the value for the metadata. ifname
has no transformer defined then the rawvalue
will be submitted and therefore it must be astr
The method also accepts arbitrary keyword arguments that are forwarded to any defined transformers.
Transformers
Transformers are defined via a class level attribute
METADATA_TRANSFORMERS
.This is a mapping of metadata names to functions. When you call
self.set_metadata(name, value, **options)
, ifname
is in this mapping then the corresponding function will be called with thevalue
, and the return value(s) will be sent instead.Transformer functions must satisfy the following signature:
If the return type is a string, then it will be sent as the value for
name
. If the return type is a mapping type, then each key will be considered aname
and will be sent with its (str
) value.Initially, there are 2 default transformers:
version
andconfig
version
This transforms a version like
1.2.3-rc.4+5
to its constituent parts. In all cases, the metadata namesversion.raw
andversion.scheme
will be sent.If a
scheme
is defined then it will be looked up from our known schemes. If no scheme is defined then it will default tosemver
.The scheme may be set to
regex
in which case apattern
must also be defined. Any matching named subgroups will then be sent asversion.<GROUP_NAME>
. In this case, the check name will be used as the value ofversion.scheme
unlessfinal_scheme
is also set, which will take precedence.config
This transforms a
dict
of arbitrary user configuration. Asection
must be defined indicating what the configuration represents e.g.init_config
.The metadata name submitted will become
config.<section>
.The value will be a JSON
str
with the root being an array. There will be one map element for every allowed field. Every map may have 2 entries:is_set
- a boolean indicating whether or not the field existsvalue
- the value of the field. this is only set if the field exists and the value is a primitive type (None
|bool
|float
|int
|str
)The allowed fields are derived from the optional
whitelist
andblacklist
. By default, nothing will be sent.User configuration can override defaults allowing complete, granular control of metadata submissions. In any section, one may set
metadata_whitelist
and/ormetadata_blacklist
which will override their keyword argument counterparts. In following our standard, blacklists take precedence over whitelists.Blacklists are special in that each item is considered a regular expression.
Motivation
DataDog/datadog-agent#4234
DataDog/datadog-agent#4158
Additional Notes
In another PR I will implement a mechanism to automatically perform
config
metadata collection to avoid redundant calls in every check.