'Extract scalar value from text log message as metric in Google Cloud Logging

According to the docs I can define log-based metrics, but I can't seem to find a way to do this. My application logs a message like this:

2022-03-29T10:20:30 [INFO] Some action took 0.23 seconds

What I'm trying to do is extract the 0.23 as a metric that I can put on dashboard and monitor. How can I go about doing this?

Edit

I mostly solved this at the application level (my hope was that no changes to the code would be needed) by using a logger from the client library (Python in my case), and adding my metric to the log statement via structured logging. From there, I created a user defined, log-based metric, which I can now monitor and set alarm on, etc. Only detail is that, as mentioned in the comments, this particular metric is a gauge metric type. I'm extracting it as a distribution, which mostly works out of the box, though, I lose a negligible amount of precision.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source