"Add metadata" feature fails to interpolate $__user variable (renders literal string)
Description: When the Add metadata toggle is enabled in the query editor, the plugin appends a comment to the SQL query containing context about the dashboard and user. However, the variable $__user is not being interpolated correctly by Grafana and appears as the literal string $__user in the ClickHouse query logs.
It appears that in modern Grafana versions, $__user is an object (containing login, email, id), and using it directly as a string often fails or requires specific dot notation (e.g., ${__user.login}).
Steps to Reproduce:
Create a panel in Grafana using the Altinity ClickHouse data source.
Toggle the Add metadata option to ON in the query editor.
Execute the query.
Check the ClickHouse query log (system.query_log) or inspect the generated SQL in Grafana's "Query Inspector".
Expected Behavior: The generated SQL comment should contain the actual username of the logged-in user. Example: /* grafana dashboard=MyDashboard, user=admin */
Actual Behavior: The generated SQL comment contains the literal variable string. Example: /* grafana dashboard=MyDashboard, user=$__user */
Relevant Code: This likely stems from how the metadata string is constructed in the query builder. If $__user is passed raw without braces or property accessors (like ${__user.login} ), newer Grafana versions may not replace it.
Workaround: Manually disabling "Add metadata" and adding a comment to the query works if specific properties are used: /* user=${__user.login} */ SELECT ...
Environment:
Grafana version: [12.3.0]
Plugin version: [3.4.8]
ClickHouse version: [25.8]
Possible Solution: Update the metadata generation logic to use ${__user.login} or ${__user.id} instead of the raw $__user variable, which provides better compatibility with recent Grafana versions.
can't reproduce your behavior, could you suggest
how exactly you authorized in your grafana? do you use OAuth authorization or just use local database stored users with login and password?
hello when i connect to admin have same as you
but all user are using OAuth authentication so it does not work
@Slach @ashraf133
This is consistent with known Grafana issues:
- Issue #19437 — template variables are not expanded when using Explore, especially when opening in a new window
- Issue #74015 — dashboard variables not interpolated when navigating to Explore
The root cause seems to be that Grafana doesn't fully populate the template variable context for plugins in certain scenarios (Explore mode). The __user and __dashboard global variables either return wrong values or don't interpolate at all.
The reason why it may be not reproducing in specific scenarios is some sort of context leakage when you make transition from dashboard to explore mode and template variables are present in memory
@Slach If you want we can try to use some workaround like extracting user info directly from boot config of grafana, it will be relatively simple to implement and we'll at least get interpolation of $__user