【问题标题】:Azure Function Poison Queue AlertingAzure Function Poison 队列警报
【发布时间】:2021-07-02 07:41:33
【问题描述】:

是否可以从位置队列中创建警报?我有一个 blob 触发功能,但是当它失败 x 次数时,它会将此消息抛出到毒队列中。我要做的是在该毒物队列的计数为x 时创建警报。

  1. 我可以使用 sidecart 方法来做到这一点,我有一个独立的服务来监控毒物队列
  2. 看到这个方法https://stackoverflow.com/a/46334184/1134076
  3. 希望使用blobproperties 覆盖来存储一些metadata 以便我可以跟踪失败,但似乎没有办法做到这一点?我正在考虑最后一次尝试跟踪事件以声明moving message to poison queue

有更好的方法吗?

        [FunctionName("MyFunction")]
        public async Task Run(
            [BlobTrigger("input-queue", Connection = "ConnectionString")] Stream blobContent,
            string name,
            System.Uri uri,
            IDictionary<string, string> metaData,
            BlobProperties properties,
            ILogger log)

编辑 注意到这个函数重载:

        [FunctionName("MyFunction")]
        public async Task Run(
            [BlobTrigger("input-queue", Connection = "ConnectionString")] ICloudBlob blobContent,
            string name,
            System.Uri uri,
            IDictionary<string, string> metaData,
            BlobProperties properties,
            ILogger log)
        {
            if (!blobContent.Metadata.ContainsKey("mycount"))
            {
                blobContent.Metadata["mycount"] = "1";
                await blobContent.SetMetadataAsync();
            }
            else
            {
                var value = int.Parse(blobContent.Metadata["mycount"]);
                value++;
                blobContent.Metadata["mycount"] = value.ToString();
                await blobContent.SetMetadataAsync();
            }

            throw new Exception(("Testing Function"));
        }

看起来有点矫枉过正,但我​​可以在这里跟踪失败计数并编写逻辑来跟踪自定义事件。

【问题讨论】:

    标签: azure-functions poison-queue


    【解决方案1】:

    我更喜欢的方法是使用 Azure 函数定期计算中毒队列中的项目数并将其作为 Application Insights 指标发布。然后您可以从那里设置查询/仪表板/警报。

    这种方法的基本概述是here。请注意,现代 Azure Functions 允许您使用 DI 和其他细节。

    每 5 分钟计时器触发函数的示例,该函数获取毒队列计数并将其作为指标发布:

    [FunctionName(nameof(TrackQueueMetrics))]
    public async Task TrackQueueMetrics([TimerTrigger("0 */5 * * * *")] TimerInfo message)
    {
      var queues = await _queueService.GetAllQueuesAsync();
      var poisonQueues = queues.Where(x => x.EndsWith("-poison", StringComparison.InvariantCultureIgnoreCase)).ToList();
      var poisonQueueCounts = await Task.WhenAll(poisonQueues.Select(_queueService.GetApproximateMessagesCountAsync));
      var fatalErrors = poisonQueueCounts.Sum();
      _metrics.TrackFatalErrorsCount(fatalErrors);
    }
    

    GetAllQueuesAsync() 本质上是:

    public async Task<IReadOnlyList<string>> GetAllQueuesAsync()
    {
      var result = new List<string>();
      await foreach (var item in _queueServiceClient.GetQueuesAsync())
          result.Add(item.Name);
      return result;
    }
    

    GetApproximateMessagesCount() 本质上是:

    public async Task<int> GetApproximateMessagesCountAsync(string queueName)
    {
      var properties = await _queueServiceClient.GetQueueClient(queueName).GetPropertiesAsync();
      return properties.Value.ApproximateMessagesCount;
    }
    

    TrackFatalErrorsCount 本质上是:

    public sealed class SingletonMetricsClient
    {
      private readonly Metric _fatalErrorsCountMetric;
    
      public SingletonMetricsClient(TelemetryConfiguration telemetryConfiguration)
      {
        var client = new TelemetryClient(telemetryConfiguration);
        _fatalErrorsCountMetric = client.GetMetric("FatalErrors");
      }
    
      public void TrackFatalErrorsCount(int count) => _fatalErrorsCountMetric.TrackValue(count);
    }
    

    一旦您将其作为指标,您就可以对其进行查询、为您的 Azure 仪表板构建图表和/或设置 Application Insights 警报。

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2021-12-23
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多