Search code examples
google-cloud-platformgoogle-cloud-pubsubdataflow

Using Pub/Sub to trigger Cloud Function/Cloud Run but with an IF Condution


I am interested to know what the best solution would be to the following problem: I have telemetry data published to pubsub at 10 second intervals. The data changes in cycles (ie. One particular parameter goes from 400 to 1700 and resets back to 400 ...)

I would like to trigger a Cloud Function or Cloud Run API when the parameter is at 800 and 1200 and 1500.

So I was wondering if there is such a thing whereby we can trigger a cloud function/cloud run if the pubsub message says that the parameter is at those values?

One way to do it of course is to write a Cloud Function to always activate when a new message is published and use an "if" condition to determine whether to proceed. However, I'm thinking this is a very expensive method because the cloud function would be triggered every 10 seconds when these cycles are particularly long and don't need to be executed frequently. So I'm wondering if there is a more cost effective method.


Solution

  • The naive solution is fine even if not "elegant". We yet have to define what "very expensive" is. A single function up is not an important point of spending. Especially if you consider the free tier usage.

    That being said, since June PubSub beta released a filtering feature that allows you to put delivery conditions based on your message attributes. It may helps you. I say it may bc as far as i can tell, there is no comparaison operator on the payload content. Only attributes. So you would have to forge messages with attributes that allow you to deliver or not according your business rule.