I am trying to extract the value at the end of this log:
Chat request completed in 3.818780105999849
This is a log saved in Google Cloud Logging and the message is written in the textPayload field. I am trying to create a log-based metric using the gcloud logging metrics create command (docs found here). Since I am trying to create a distribution metric, I am required to create a config file for the metric.
This is what I have for the config json:
{
"name": "end-to-end-response-time-shell",
"description": "Time duration of a chat request (in seconds).",
"filter": "resource.type=\"cloud_run_revision\" AND logName=\"log_name\" AND severity=\"INFO\" AND textPayload=~\"Chat request completed in\"",
"metricDescriptor": {
"name": "cloud_run_revision",
"type": "logging.googleapis.com/user/end-to-end-response-time-shell",
"metricKind": "CUMULATIVE",
"valueType": "DISTRIBUTION",
"unit": "s",
"description": "Time duration of a chat request (in seconds).",
"displayName": "end-to-end-response-time-shell (sec)"
},
"valueExtractor": "REGEXP_EXTRACT(textPayload, \"\\s(\\d+\\.*\\d*)\\s\")"
}
The command is erroring at the valueExtractor field with this error message:
ERROR: (gcloud.logging.metrics.create) INVALID_ARGUMENT: Failed to parse extractor expression: syntax error at line 1, column 29, token '"\s(\d+\.*\d*)\s"'
How should I write the regex expression for this task?
I think you'll need to double-escape (yeah!) the RegEx.
In the documentation for REGEXP_EXTRACT, the RegEx value is escaped in the string.
You'll want to re-escape it for your escaped string in JSON ..... e.g. \\\\s
and \\\\d
etc.