Activity.current.addtag()问题..... Singleton相关吗?

发布于 2025-02-10 09:51:09 字数 2628 浏览 1 评论 0原文

我希望我能够很好地解释这个问题,以提示解决我的问题。

我有一个使用C#编写的CosMoSDBTRIGGER的Azure函数(V4)。我们在其他功能类型中也看到了同样的问题。

为了在应用程序见解中添加自定义属性请求telemetry,我们使用了一个活动标记器。它是在函数启动中作为单身人士构建的。

在该功能中,我在我阅读的文档中添加了一个带有salestransactionnumber的标签,然后在代码I日志中进一步添加了交易编号已写入服务总线的代码。

问题在于,看来该活动标记器首次调用,它具有正确的交易号。之后,它可能是正确的,或者可能具有与上一个文档相同的交易号。记录的信息消息始终具有正确的交易号。

此问题是在下面显示的函数中使用的代码行。它将错误的交易号添加到定制限制中:

_activityTagger.AddTag("SaleTransactionNumber", sale.Header.TransactionNumber);

这是Activity Tagger的代码:

void AddTag(string key, string value)
{
    Activity.Current?.AddTag(key, value);
}

这是该函数的摘要(ChangeFeed):

    public class ServiceBusNotifier
    {
        private readonly IActivityTagger _activityTagger;

        public ServiceBusNotifier(
            IActivityTagger activityTagger
            )
        {
            _activityTagger = activityTagger;
        }

        [FunctionName("ServiceBusNotifier")]
        public async Task RunAsync([CosmosDBTrigger(
            databaseName: "%CosmosDb:DatabaseId%",
            collectionName: "%masterCollectionName%",
            ConnectionStringSetting = "cosmosdb-connection",
            LeaseCollectionName = "%leasesCollectionName%",
            LeaseCollectionPrefix = "ServiceBusNotifier")] IReadOnlyList<Document> documents, ILogger logger)
        {
            try
            {
                if (documents != null && documents.Any())
                {
                    var callbackBasePath = _configuration.CallbackBaseUrl;

                    await Task.WhenAll(documents.Select(async document =>
                    {
                        var sale = JsonConvert.DeserializeObject<Sale>(document.ToString());

                        _activityTagger.AddTag("SaleTransactionNumber", sale.Header.TransactionNumber);
                        logger.LogInformation($"Sending Service Bus Message for Sales change for TransactionNumber {sale.Header.TransactionNumber}");

                        Message build logic...
                        
                        await _serviceBusClient.SendJsonMessageAsync(message, userProperties, messageId);
                        logger.LogInformation($"Sent Service Bus Message: {messageId}  TransactionNumber: {sale.Header.TransactionNumber}");
                    }));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
        }
    }
}

我不确定它是否与ActivityTagger作为单身人士有关,或者也许必须这样做循环处理所有文档?任何帮助将不胜感激!

I hope I can explain this issue well enough to prompt for an answer to my issue.

I have an Azure function(V4) that uses CosmosDBTrigger written in C#. We're seeing the same issue in other function types as well.

In order to add custom properties in Application Insights RequestTelemetry we're using an activity tagger. It is built as a singleton in the function start up.

In the function, I am adding a tag with the salesTransactionNumber from the document I've read, then further down in the code I log that the transaction number has been written to the service bus.

The issue is that it appears that the first time the activity tagger is called, it has the correct transaction number. After that, it could be correct, or it could have the same transaction number as the previous document. The logged informational messages always have the correct transaction number.

The issue is with this line of code in the function shown below. It is adding the wrong transaction number to customDimensions:

_activityTagger.AddTag("SaleTransactionNumber", sale.Header.TransactionNumber);

Here's the activity tagger's code:

void AddTag(string key, string value)
{
    Activity.Current?.AddTag(key, value);
}

And here's a snippet of the function (changefeed):

    public class ServiceBusNotifier
    {
        private readonly IActivityTagger _activityTagger;

        public ServiceBusNotifier(
            IActivityTagger activityTagger
            )
        {
            _activityTagger = activityTagger;
        }

        [FunctionName("ServiceBusNotifier")]
        public async Task RunAsync([CosmosDBTrigger(
            databaseName: "%CosmosDb:DatabaseId%",
            collectionName: "%masterCollectionName%",
            ConnectionStringSetting = "cosmosdb-connection",
            LeaseCollectionName = "%leasesCollectionName%",
            LeaseCollectionPrefix = "ServiceBusNotifier")] IReadOnlyList<Document> documents, ILogger logger)
        {
            try
            {
                if (documents != null && documents.Any())
                {
                    var callbackBasePath = _configuration.CallbackBaseUrl;

                    await Task.WhenAll(documents.Select(async document =>
                    {
                        var sale = JsonConvert.DeserializeObject<Sale>(document.ToString());

                        _activityTagger.AddTag("SaleTransactionNumber", sale.Header.TransactionNumber);
                        logger.LogInformation(
quot;Sending Service Bus Message for Sales change for TransactionNumber {sale.Header.TransactionNumber}");

                        Message build logic...
                        
                        await _serviceBusClient.SendJsonMessageAsync(message, userProperties, messageId);
                        logger.LogInformation(
quot;Sent Service Bus Message: {messageId}  TransactionNumber: {sale.Header.TransactionNumber}");
                    }));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
        }
    }
}

I'm not sure if it has to do with the activityTagger as a singleton, or perhaps it has to do with the loop processing all documents? Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文