grafana

How to escape a special char when using grafana line_format


I am currently trying to format a grafana query that extract a specific field from the json. I currently have this

... | json | line_format "{{.request_User-Agent}}"

The parsing fails because of the hyphen in request_User-Agent... but that is how the field is name in the log line. Is there any way to get around that?

EDIT

After the json stage, the key look like this:

"request_User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",

So the hyphen is still there. Which makes sense, it is a valid json key, just not a valid go-template variable name..


Solution

  • Thanks to @markalex comments, I found the solution. Very simple, I only need to do this.

    ... | json | line_format "{{.request_User_Agent}}"
    

    The json stage replaces any - in json key with _, so request_User-Agent become request_User_Agent

    It will still show the original json key in the UI, which is why I thought they weren't getting replaced, but identifier in the query will use _