Shahid Thaika
Shahid Thaika

Reputation: 2305

How to set GCP Alert threshold based on time?

I have a Google Dataflow Pipeline that logs JSON similar to the following.

jsonPayload: {
    metric: "viewings-per-second",
    time: "2023-03-28 17:16:14",
    value: 15
}

I created a Log-based-metric to extract the value and used it in a Cloud Monitoring Alert with a fixed threshold. My actual requirement though, is to have two thresholds - one during peak hours (6AM - 1AM) and one during off-peak (1AM - 6AM).

How can I create a dynamic threshold, if possible? Perhaps I can use Monitoring Query Language (MQL) or something.

One alternate method I could think of was to create two Log-based-metrics and put the hour condition via Log Query Language (LQL). While testing my query in Log Explorer, this works:

jsonPayload.time>="2023-03-28 06:00:00"

However, I am unable to figure out how to extract the time alone from the datetime value.

As a last resort, I guess I can make the Dataflow Pipeline log just the hour or time, but was wondering whether there is a better solution.

EDIT:

My current LQL

logName="projects/example-project/logs/dataflow-viewings-per-second"
jsonPayload.metric="viewings-per-second"
jsonPayload.time>="2023-03-28 06:00:00"

Upvotes: 0

Views: 409

Answers (0)

Related Questions