question-mark

Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MetricsList not working with no-name metrics.

See original GitHub issue

Issue Description

The following code sample is breaking for me when attempting to instantiate a metricslist object

pc = PrometheusConnect(
    url="my_prometheus_instance",
    headers={"Authorization":"bearer my_private_token" },
    disable_ssl=True,
    )

end_time = datetime.datetime.now()
start_time = end_time - datetime.timedelta(days=365)

chunk_size = parse_timedelta("now", "1d")

query_result = pc.custom_query(query='avg_over_time(min(up{job=~"foo"})[7d:])*100')

bar=MetricsList(query_result)

This is the output of the code in question

bar=MetricsList(foo)

for item in bar:

    print(item.metric_name, item.label_config)

    print(item.metric_values)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-6b7c63e24b2a> in <module>
----> 1 bar=MetricsList(foo)
      2 for item in bar:
      3     print(item.metric_name, item.label_config)
      4     print(item.metric_values)

/opt/app-root/lib/python3.6/site-packages/prometheus_api_client/metrics_list.py in __init__(self, metric_data_list)
     40                         metric_object_list.append(metric_object)
     41             else:
---> 42                 metric_object = Metric(i)
     43                 if metric_object in metric_object_list:
     44                     metric_object_list[metric_object_list.index(metric_object)] += metric_object

/opt/app-root/lib/python3.6/site-packages/prometheus_api_client/metric.py in __init__(self, metric, oldest_data_datetime)
     68             self.oldest_data_datetime = oldest_data_datetime
     69         else:
---> 70             self.metric_name = metric["metric"]["__name__"]
     71             self.label_config = deepcopy(metric["metric"])
     72             self.oldest_data_datetime = oldest_data_datetime

KeyError: '__name__'

The value of query_result is [{'metric': {}, 'value': [1566485171.497, '14735']}]

My guess is that any prometheus query that returns a time_series without a name will break. Setting a default value of some sort is probably the simplest solution here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
anushkrishnavcommented, Nov 28, 2020

Can I work on this ?

1reaction
stefancraincommented, Mar 17, 2022

For the next person who comes across this, I think I’ve found some success. In my query I was able to use label_replace to insert a label where there wasn’t one.

Updating the example above :

query_result = pc.custom_query(query='label_replace(avg_over_time(min(up{job=~"foo"})[7d:])*100), '__name__', 'baz', 'job', '.*')')

… which doesn’t run for me so here’s mine:

end_time = datetime.now()
start_time = end_time - timedelta(days=30)

query_result = pc.custom_query_range(
    query="label_replace(max(max_over_time(container_memory_max_usage_bytes[1d])) by (pod_name), '__name__', 'baz', 'job', '.*')"
    start_time=start_time,
    end_time=end_time,
    step = 60*60*24
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I retrieve data points for CloudWatch metrics in the ...
To troubleshoot missing data points, follow these steps: Confirm that the metric namespace, metric name, and key-value pairs for the ...
Read more >
Rules for creating metrics: requirements and tips
Here are some limits, requirements, and recommendations when you create metrics from events, logs, or spans. Metric aggregation. Your NRQL query must use...
Read more >
MongoDB Prometheus exporter not scrapping all metrics
I have a Mongo deployment with a metrics exporter ...
Read more >
Nsight Compute CLI - NVIDIA Documentation Center
If not specified and --section or --metrics are used, no sets are collected. Use --list-sets to see which set is the default. list-sections ......
Read more >
Code metrics values - Visual Studio - Microsoft Learn
Software measurements. The following list shows the code metrics results that Visual Studio calculates: Maintainability Index - Calculates an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found