Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsstackdrivergoogle-cloud-stackdriver

python stackdriver google functions webhook listener


I'm trying to create a stackdriver webhook listener in google cloud functions, using the following script:

import sys
import logging
import json

from flask import Flask
from flask import Response, request

def webhook(request):
    logging.info("Stackdriver ga360_merge_ready starting up on %s" % (str.replace(sys.version, '\n', ' ')))

    app = Flask(__name__)


    @app.route('/', methods=['POST'])
    def simple_handler():
        """ Handle a webhook post with no authentication method """
        json_data = json.loads(request.data)
        logging.info(json.dumps(json_data, indent=4))
        return Response("OK")

For the above, I have the following URL:

https://xxxxx.cloudfunctions.net/webhook

"webhook" is the cloud functions name. when I put this URL in with an ending slash, as per the code, it doesn't seem to send across the message in from stackdriver, essentially, I want the message to also come through, presently, all I get is the below three log entries:

enter image description here

Not sure what I'm missing, I'm new to the python/webhooks world


Solution

  • Your simple_handler is never being called because the request is never being routed to the app you've created.

    Is there a reason your function is set up like that? I would expect it to be something like this instead:

    import sys
    import logging
    import json
    
    logging.info("Stackdriver ga360_merge_ready starting up on %s" % (str.replace(sys.version, '\n', ' ')))
    
    def webhook(request):
        """ Handle a webhook post with no authentication method """
        logging.info(json.dumps(request.get_json(), indent=4))
        return Response("OK")