Skip to content

Loki metric queries return null labels for aggregate groupings #368

@IS-PatrickSmith

Description

@IS-PatrickSmith

Problem

When executing Loki metric queries with aggregate functions like sum by() using the query_loki_logs tool, the results include metric values but all label groupings return as null.

Expected Behavior

Metric queries with grouping should return the labels that were grouped by. For example:

sum by(app, namespace) (count_over_time({namespace="dev2"} | json | Level="Error" [3h])) > 0

Should return results with app and namespace labels populated.

Actual Behavior

The query executes successfully but all results have "labels": null:

[
  {"timestamp":"1761755580","line":"5","labels":null},
  {"timestamp":"1761755623","line":"5","labels":null},
  ...
]

Steps to Reproduce

  1. Configure Grafana MCP with a Loki datasource
  2. Execute a metric query with grouping:
query_loki_logs(
  datasourceUid="<loki-uid>",
  logql='sum by(app, namespace) (count_over_time({namespace="dev2"} [3h])) > 0',
  ...
)
  1. Observe that labels field is null in all results

Workaround

Instead of using metric queries, fetch raw log samples and extract labels from individual log entries.

Environment

  • Grafana MCP version: latest (Docker image mcp/grafana)
  • Transport: stdio via Docker

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions