Skip to content

Unsafe string split when parsing Prometheus metric labels in ontology monitoring #873

Description

@cybermaggedon

Description

In trustgraph-flow/trustgraph/query/ontology/monitoring.py (line 478):

cache_type = metric_name.split('cache_type=')[1].split(',')[0].split('}')[0]

Problem

If a metric name in the counters dict doesn't contain the substring `cache_type=`, the `split()` returns a single-element list and `[1]` raises `IndexError`.

The guard on line 477 (`if 'cache_type=' in metric_name`) currently prevents this, but the parsing itself is fragile — any refactoring that moves or removes the guard exposes the crash. The chained-split approach is also hard to read.

Suggested fix

Please PR against the latest release/vX.Y branch.

Use a small helper or regex to extract label values safely:

def _extract_label(metric_name, label):
    """Extract a Prometheus-style label value from a metric name, or None."""
    prefix = f'{label}="'
    if prefix not in metric_name:
        return None
    return metric_name.split(prefix)[1].split('"')[0]

Or keep inline but add a guard:

parts = metric_name.split('cache_type=')
if len(parts) < 2:
    continue
cache_type = parts[1].split(',')[0].split('}')[0]

What you'll learn

How TrustGraph's ontology query engine collects and reports internal performance metrics, and safe patterns for parsing structured strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions