Skip to content

Add description for custom metrics tags #20663

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

R-HNF
Copy link

@R-HNF R-HNF commented Jul 5, 2025

What does this PR do?

  • Add description for custom metrics tags. This tags is the field that can be configured under custom_queries.
  • Also, made some minor fixes to the README.md.

Motivation

There is no description for custom metrics tags in README.md. I think it's necessary to clarify that, unlike the tags configured under instances, the tags configured under custom_queries apply only to custom metrics.
Please refer the following:

def _process_custom_metric(
self,
value,
data_path,
value_path,
dynamic_tags,
xtype,
metric_name,
tags=None,
):
"""
value: JSON payload to traverse
data_path: path to data right before metric value or right before list of metric values
value_path: path to data after data_path to metric value
dynamic_tags: list of dynamic tags and their value_paths
xtype: datadog metric type, default to gauge
metric_name: datadog metric name
tags: list of tags that should be included with each metric submitted
"""
tags_to_submit = deepcopy(tags)
path = '{}.{}'.format(data_path, value_path)
# Collect the value of tags first, and then append to tags_to_submit
for dynamic_tag_path, dynamic_tag_name in dynamic_tags:
# Traverse down the tree to find the tag value
dynamic_tag_value = get_value_from_path(value, dynamic_tag_path)
# If tag is there, then add it to list of tags to submit
if dynamic_tag_value is not None:
dynamic_tag = '{}:{}'.format(dynamic_tag_name, dynamic_tag_value)
tags_to_submit.append(dynamic_tag)
else:
self.log.debug("Dynamic tag is null: %s -> %s", path, dynamic_tag_name)
# Now do the same for the actual metric
branch_value = get_value_from_path(value, value_path)
if branch_value is not None:
if xtype == "gauge":
self.gauge(metric_name, branch_value, tags=tags_to_submit)
elif xtype == "monotonic_count":
self.monotonic_count(metric_name, branch_value, tags=tags_to_submit)
elif xtype == "rate":
self.rate(metric_name, branch_value, tags=tags_to_submit)
else:
self.log.warning(
"Metric type of %s is not gauge, monotonic_count, or rate; skipping this metric", metric_name
)
else:
self.log.debug("Metric not found: %s -> %s", path, metric_name)
def _run_custom_queries(self, admin_forwarder, base_tags):
self.log.debug("Running custom queries")
custom_queries = self._config.custom_queries
for query_endpoint in custom_queries:
try:
columns = query_endpoint.get('columns', [])
data_path = query_endpoint.get('data_path')
raw_endpoint = query_endpoint.get('endpoint')
static_tags = query_endpoint.get('tags', [])
payload = query_endpoint.get('payload', {})
endpoint = self._join_url(raw_endpoint, admin_forwarder)
data = self._get_data(endpoint, data=payload)
# If there are tags, add the tag path to list of paths to evaluate while processing metric
dynamic_tags = get_dynamic_tags(columns)
tags = base_tags + static_tags
# Traverse the nested dictionaries to the data_path and get the remainder JSON response
value = get_value_from_path(data, data_path)
for column in columns:
metric_type = column.get('type', 'gauge')
# Skip tags since already processed
if metric_type == 'tag':
continue
name = column.get('name')
value_path = column.get('value_path')
if name and value_path:
# At this point, there may be multiple branches of value_paths.
# If value is a list, go through each entry
if isinstance(value, list):
value = value
else:
value = [value]
for branch in value:
self._process_custom_metric(
value=branch,
data_path=data_path,
value_path=value_path,
dynamic_tags=dynamic_tags,
xtype=metric_type,
metric_name=name,
tags=tags,
)
except Exception as e:
self.log.error("Custom query %s failed: %s", query_endpoint, e)
continue

Minor fixes:

  • Remove a trailing space.
  • Adjust whitespace in curl commands.
  • Use an absolute URL for the link.
    • To prevent redirection to the following URL when accessed from GitHub:
      https://github.com/DataDog/integrations-core/blob/master/account/settings/agent/latest

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add the qa/skip-qa label if the PR doesn't need to be tested during QA.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

@@ -458,7 +459,7 @@ See [service_checks.json][26] for a list of service checks provided by this inte


[1]: https://raw.githubusercontent.com/DataDog/integrations-core/master/elastic/images/elasticsearch-dash.png
[2]: /account/settings/agent/latest
[2]: https://app.datadoghq.com/account/settings/agent/latest
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference:

[2]: https://app.datadoghq.com/account/settings/agent/latest

@R-HNF R-HNF marked this pull request as ready for review July 5, 2025 08:20
@R-HNF R-HNF requested review from a team as code owners July 5, 2025 08:20
Copy link
Contributor

@aliciascott aliciascott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants