azure - 如何获取 Azure EventHub 深度

标签 azure azure-eventhub

我的 EventHub 每天都会摄取数百万条消息。我正在处理来自 Azure Function 的这些消息,并在日志中打印偏移量和序列数值。

public static async Task Run([EventHubTrigger("%EventHub%", Connection = "EventHubConnection", ConsumerGroup = "%EventHubConsumerGroup%")]EventData eventMessage,
        [Inject]ITsfService tsfService, [Inject]ILog log)
    {
log.Info($"PartitionKey {eventMessage.PartitionKey}, Offset {eventMessage.Offset} and SequenceNumber {eventMessage.SequenceNumber}");
}

日志输出

PartitionKey、偏移量 78048157161248 和 SequenceNumber 442995283

问题

  1. PartitionKey 值空白?我在该 EventHub 中有 2 个分区

  2. 有什么方法可以检查积压的订单吗?有时我想知道我的函数需要处理多少条消息。

最佳答案

是的,您可以将 PartitionContext 对象作为签名的一部分包含在内,这将为您提供一些附加信息,

public static async Task Run([EventHubTrigger("HubName", 
    Connection = "EventHubConnectionStringSettingName", 
    ConsumerGroup = "Consumer-Group-If-Applicable")] EventData[] messageBatch, PartitionContext partitionContext, ILogger log)

编辑您的host.json并将enableReceiverRuntimeMetric设置为true,例如

"version":  "2.0",
"extensions": {
    "eventHubs": {
        "batchCheckpointFrequency": 100,
        "eventProcessorOptions": {
            "maxBatchSize": 256,
            "prefetchCount": 512,
            "enableReceiverRuntimeMetric": true
        }            
    }
}

您现在可以访问 PartitionContext 上的 RuntimeInformation,其中包含有关 LastSequenceNumber 的一些信息,并且您当前的消息有自己的序列号,因此您可以使用它们之间的差异来计算指标,例如,

public class EventStreamBacklogTracing
{
    private static readonly Metric PartitionSequenceMetric = 
        InsightsClient.Instance.GetMetric("PartitionSequenceDifference", "PartitionId", "ConsumerGroupName", "EventHubPath");

    public static void LogSequenceDifference(EventData message, PartitionContext context)
    {
        var messageSequence = message.SystemProperties.SequenceNumber;
        var lastEnqueuedSequence = context.RuntimeInformation.LastSequenceNumber;

        var sequenceDifference = lastEnqueuedSequence - messageSequence;

        PartitionSequenceMetric.TrackValue(sequenceDifference, context.PartitionId, context.ConsumerGroupName,
            context.EventHubPath);
    }
}

我在 Medium 上写了一篇文章,其中更详细地介绍了如何使用 grafana 中的数据,

https://medium.com/@dylanm_asos/azure-functions-event-hub-processing-8a3f39d2cd0f

关于azure - 如何获取 Azure EventHub 深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292184/

相关文章:

Azure 事件中心 - 如何通过 https 从传感器接收数据?

java - 从 EventHostProcessor 中的最后一个检查点重新开始消费

c# - Azure.Messaging.EventHubs 与 WindowsAzure.ServiceBus 生成的消息有区别吗?

azure - 应如何使 Azure 资源管理器 JSON 与 Azure 门户保持一致?

node.js - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client Registering user in node JS

azure - 在 2 个 Azure SQL 数据库之间移动/合并表的最佳方法

azure - 时间扫描在 Azure 流分析翻转窗口和跳跃窗口中如何工作?

azure - 在共置缓存中,如何确定键的位置?

Azure APIM 限制所有 IP 地址

c# - 发送异步 Azure 事件中心数据方法