You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note that custom labels are supported for OCI resource service metrics only. Custom metrics generated by scripts or by UMA do not support custom labels.
249
+
Please note that custom labels are supported for OCI resource service metrics only. Custom metrics generated by scripts or by UMA do not support custom labels.
250
+
251
+
## Label customization using regex Transformation
252
+
In some use case, the Metric Label Customization is not applicable or does not work as expected, due to limitations of dimension key which the plugin is able to capture. The "Rename by Regex" transformation in Grafana allows you to dynamically change the names of fields or series returned by your queries using regular expressions. This is useful for standardizing naming conventions, shortening long names, or extracting parts of a name to reformat it.
253
+
254
+
**How it Works:**
255
+
256
+
1.**Match:** You provide a regular expression (regex) that Grafana attempts to match against existing field or series names.
257
+
2.**Replace:** You define a replacement pattern. This pattern can include:
258
+
* Literal text.
259
+
* Backreferences to "capture groups" from your "Match" regex. Capture groups are parts of your regex enclosed in parentheses `(...)`. You refer to them using `$1` for the first group, `$2` for the second, and so on.
260
+
3.**Apply to:** You specify whether the renaming logic should apply to "Field name" or "Series name".
261
+
262
+
**When to Use It (Use Case):**
263
+
264
+
Use "Rename by Regex" when you need to systematically alter names that follow a pattern, without manually overriding each one.
265
+
266
+
**Example Use Case: Shortening and Reformatting Server Metric Names**
267
+
268
+
***Scenario:** You have metric names like `production_webserver_03_cpu_utilization` or `staging_database_01_memory_usage`. You want to shorten them to `prod_ws_03_cpu` or `stag_db_01_mem` in your panel's legend or table.
269
+
270
+
***Configuration:**
271
+
1.**Add Transformation:** In your panel editor, go to the "Transform" tab and add the "Rename by regex" transformation.
272
+
2.**Match Regex:**`(\w+)_(\w+)server_(\d+)_(\w+)_utilization` (this is just one example, tailor it to your exact naming scheme)
273
+
*`(\w+)`: Captures the environment (e.g., "production") - becomes `$1`
274
+
*`(\w+)`: Captures the type (e.g., "web") - becomes `$2`
275
+
*`(\d+)`: Captures the server number (e.g., "03") - becomes `$3`
276
+
*`(\w+)`: Captures the metric type (e.g., "cpu") - becomes `$4`
277
+
3.**Replace Pattern:**`$1_$2_$3_$4` (This example mainly reorders and removes parts. A more aggressive shortening might be `env:$1_srv:$3_metric:$4`)
(You might need multiple "Rename by Regex" transformations or a more complex single regex if the patterns vary widely).
285
+
286
+
A more general approach for capturing initial letters and numbers:
287
+
* Match: `(\w{3,4})\w*_(\w{2})\w*_(\d+)_(\w{3})\w*` (This regex is an example and would need careful crafting and testing for your specific names)
288
+
* Replace: `$1_$2_$3_$4`
289
+
290
+
4.**Apply to:** "Field name" or "Series name" (often "Series name" for legends).
291
+
292
+
***Result:** Names like `production_webserver_03_cpu_utilization` would be transformed based on your replace pattern, for instance, to `prod_ws_03_cpu`.
293
+
294
+
### Regex applied to Metrics Plugin - example
295
+
This is an example dashboard created without using regex, and since it uses custom metrics, the metric label function is not working properly:
296
+

297
+
298
+
We can then use this Transformation Rename by Regex:
299
+
300
+
```
301
+
^.*cluster="([^"]+)".*$
302
+
```
303
+
304
+
and that will extract the cluster name, as in the following screenshot:
305
+

306
+
307
+
308
+
**Key Things to Remember:**
309
+
310
+
***Regex Engine:** Grafana typically uses the RE2 regex engine.
311
+
***Testing:** Always test your regex patterns. Tools like regex101.com (set to Go/RE2 if possible) are helpful before applying in Grafana. Then verify in the Grafana UI.
312
+
***Specificity:** Make your "Match" regex as specific as possible to avoid unintended renames.
313
+
***Order Matters:** If you have multiple transformations, their order of execution can affect the final output.
0 commit comments