title: LogQL - label_format conditionally format a label
body: <p>I have a log stream from where I am extracting a set of fields to be set as either labels or metric values. The stream is not in a standard format so I am extracting the fields with regexp pipeline command, as below.</p>
  <pre><code>(...)
   | regexp &quot;(?P&lt;api&gt;\\w+)\\sAPI&quot;
   | regexp &quot;\\[performed\\.(?P&lt;action&gt;\\w+)&quot;
   | regexp &quot;duration\\s\\[(?P&lt;duration_ms&gt;\\d+)&quot;
   | regexp &quot;response \\[(?P&lt;response&gt;.*?)\\]&quot;
  </code></pre>
  <p>The problem is that the <code>api</code> captured field, on some interaction, is not being populated, and I wanted to update those cases so that a default value was set - For presentation purposes.</p>
  <p>I've tried using the native LogLQ's <a href="https://grafana.com/docs/loki/latest/logql/template_functions/#contains" rel="nofollow noreferrer">contains</a> and <a href="https://grafana.com/docs/loki/latest/logql/template_functions/#hasprefix-and-hassuffix" rel="nofollow noreferrer">hasPrefix</a> template commands as the documentation suggests they can be used with <code>if else</code> blocks. The documentation is not clear on how to build those blocks inside the <code>label_format</code> or the <code>line_format</code> pipeline commands. But depending on the approach it either returns a format error or does not do anything.</p>
  <p>An working example would be appreciated.
  Thank you.</p>
  <p>Note: Tried to tag this as a LogQL topic but not enough reputation to do so.</p>