0

I have an Azure Function HTTP trigger listening to Twilio webhooks. The AF is sitting on a premium service plan. It is returning intermittent 502 errors to Twilio at a fairly high rate of 6% (out of hundreds or thousands of successfuls). I really need to get to the bottom of why this is happening as we are missing important data.

All this trigger does is send the incoming webhook to a service bus and return an OK...I have another nearly identical http trigger in a different AF that hears millions of hits from SendGrid without failure. So I'm having trouble figuring out what the issue is here. Appreciate any advice, let me know if there are questions.

Here's my trigger code:

namespace TwilioWebhookListener
{
    public class ReplyListener
    {
        private readonly AppSettings _settings;
        IQueueClient queueClient;

        public ReplyListener(AppSettings appSettings)
        {
            _settings = appSettings;
        }

        [FunctionName("Twilio-Reply-Listener")]
        public async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "reply")] HttpRequest httpRequest, 
            ILogger log)
        {

            var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("", Encoding.Default, @"application/xml") };
            try
            {
                log.LogInformation($"Twilio webhook - replyListener started at: { DateTime.UtcNow}");

                string requestBody = await new StreamReader(httpRequest.Body).ReadToEndAsync();

                queueClient = new QueueClient(_settings.ServiceBusConnection, "twilio-webhook-reply-messages");
                await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes(requestBody)));

                return response;
            }
            catch (Exception e)
            {
                log.LogInformation($"{DateTime.Now} :: Exception: {e.Message}");

                return response;
            }
        }
    }
} 
J Benjamin
  • 4,722
  • 6
  • 29
  • 39
  • Does the AF throw an error within the code when it returns the 502 status? And can you share that error? – philnash Dec 08 '21 at 22:42
  • I don't think so. Twilio is showing 502's on their side as the response from my webhook but I don't think the actual listener endpoint is ever getting hit....so, no errors in the diagnostic logs. But I definitely see them trying but not getting to me. – J Benjamin Dec 09 '21 at 03:54
  • So you're saying that Twilio is making the webhook request but your function is not getting called? So there's a problem between Twilio and Azure Functions? – philnash Dec 09 '21 at 03:55
  • That does seem like what is happening. I'm getting lots of 502s in Twilio's logs but not seeing related errors in the azure service side. Kinda hitting a wall there as the function http triggers in my other AF's are proving to be able to handle extremely high volume. Just not sure what's wrong with this one – J Benjamin Dec 09 '21 at 17:25
  • I don't know much about Azure Functions I'm afraid. Are there logs you can inspect in Azure API Management? Is there any more detail beyond the 502 status in the error message in the Twilio logs? – philnash Dec 09 '21 at 22:54

0 Answers0