繁体   English   中英

如何将 CosmosDB 依赖项跟踪数据从 Azure Function 事件中心触发器发送到 Application Insights

[英]How to send CosmosDB dependency tracking data to Application Insights from Azure Function Event Hub Trigger

我有一个 Azure Function 由 eventhub 触发并批量发送数据。 在 function 内部,有多次调用将数据插入 CosmosDB。 我添加了以下代码作为App Insight Monitoring 的一部分。

 builder.Services.AddApplicationInsightsTelemetry();

            builder.Services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) =>
            {
                module.EnableW3CHeadersInjection = true;
            });

         
            builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>(
                    (module, o) =>
                    {
                        module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "gen-0-size"));
                    }
                );

我可以在 App Insight 中看到总响应时间,但无法弄清楚如何跟踪和发送CosmosDB中每个插入查询所花费的时间

这是C#内的代码 Azure Function

var watch = System.Diagnostics.Stopwatch.StartNew();
                    
                    var DemoContainerData = new
                       {
                           id = Guid.NewGuid().ToString(),
                           UserId = userId,
                           // other properties 
                       };

                       _demoContainer.CreateItemAsync<object>(DemoContainerData);
                       
                       var DemoContainerData2 = new
                       {
                           id = Guid.NewGuid().ToString(),
                           ProductId = productId,
                           // other properties 
                       };

                       _productContainer.CreateItemAsync<object>(DemoContainerData2);
              /* var dependency = new DependencyTelemetry
               {
                   Name = "",
                   Target = "",
                   Data = ",
                   Timestamp = start,
                   Duration = DateTime.UtcNow - start,
                   Success = true
               };
               this._telemetryClient.TrackDependency(dependency);
               */                    
            watch.Stop();
            var elapsed = watch.Elapsed.TotalMilliseconds;
            log.LogInformation("Total Items {0} - Total Time {1}", Items.Length, elapsed);     

您的代码没有等待异步操作,您应该:

ItemResponse<object> response = await _demoContainer.CreateItemAsync<object>(DemoContainerData);

从响应中,您可以测量客户端延迟:

var elapsedTimeForOperation = response.Diagnostics.GetClientElapsedTime();

如果您想调查高延迟,我们建议您在请求超过某个阈值时记录诊断,例如:

if (response.Diagnostics.GetClientElapsedTime() > ConfigurableSlowRequestTimeSpan)
    {
        // Log the diagnostics and add any additional info necessary to correlate to other logs 
        log.LogWarning("Slow request {0}", response.Diagnostics);
    }

为了获得最佳延迟,请确保您遵循https://learn.microsoft.com/azure/cosmos-db/sql/troubleshoot-dot.net-sdk-slow-request?tabs=cpu-new#application-design (主要是确保您使用的是 Singleton 客户端,使用ApplicationRegionApplicationPreferredRegions定义要连接的首选区域,希望该区域与 Function 部署到的区域相同)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM