unit-testing - 单元测试 TelemetryProcessor 和 TelemetryInitializer(Application Insights)?

标签 unit-testing azure-application-insights open-telemetry

我一直在网上寻找一种方法来对我的处理器和初始化器进行单元测试。到目前为止,我已经从另一篇文章中设置了以下代码,但我对如何继续操作有点迷茫。

public class TelemetryClientTests
{
    private TelemetryClient telemetryClient;
    private List<ITelemetry> sendItems;

    [TestInitialize]
    public void TestInit()
    {
        var httpContext = Substitute.For<IHttpContextAccessor>();
        var configuration = new TelemetryConfiguration();
        //this.sendItems = new List<ITelemetry>();
        configuration.TelemetryChannel = new RequeteTelemtry();
        configuration.InstrumentationKey = Guid.NewGuid().ToString();
        configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
        this.telemetryClient = new TelemetryClient(configuration);
    }

    /// <summary>
    /// Ensure that context being propagated via async/await.
    /// </summary>
    [TestMethod]
    public void ContextPropogatesThruAsyncAwait()
    {
        var task = this.TestAsync();
        task.Wait();
    }
}

我不确定应该在哪里添加我的 telemtryprocessor,以便 Application Inisight 知道我正在对它进行单元测试。如果有人知道如何去做,将不胜感激。

最佳答案

这是我们对遥测初始化程序进行单元测试的服务之一的示例。

首先,我们有 NullTelemetryProcessor。它有两个作用:防止数据实际发送到 Application Insights 服务;而是将所有项目存储在内存中(因此可以将它们用于断言)。

请注意,遥测处理器在遥测初始化器之后运行。

public class NullTelemetryProcessor : ITelemetryProcessor
{
    public List<ITelemetry> Items = new List<ITelemetry>();

    public void Process(ITelemetry item)
    {
        this.Items.Add(item);
    }
}

然后我们注册它:

[TestInitialize]
public void Initialize()
{
    // Initialization of a telemetry configuration
    this.telemetryConfiguration = new TelemetryConfiguration();

    // Adding a telemetry processor which prevents actually sending anything
    this.nullTelemetryProcessor = new NullTelemetryProcessor();
    TelemetryProcessorChainBuilder builder = this.telemetryConfiguration.TelemetryProcessorChainBuilder;
    builder.Use(next => this.nullTelemetryProcessor);
    builder.Build();
}

然后测试可能看起来像这样:

[TestMethod]
public void AppInsightsTelemetryClient_TrackException()
{
    // Act
    this.telemetryClient.TrackException(new Exception("myexception"));
    this.telemetryClient.Flush();

    // Assert
    Assert.AreEqual(1, this.nullTelemetryProcessor.Items.Count);
    ExceptionTelemetry telemetryItem = this.nullTelemetryProcessor.Items[0] as ExceptionTelemetry;
    Assert.IsNotNull(telemetryItem);
    Assert.IsNull(telemetryItem.Context?.Operation?.ParentId);
    Assert.IsNull(telemetryItem.Context?.Operation?.Id);
}

关于unit-testing - 单元测试 TelemetryProcessor 和 TelemetryInitializer(Application Insights)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68292097/

相关文章:

azure-application-insights - Azure Application Insights中的每小时平均使用量

azure - 为什么我在 Azure 应用服务中每 5 分钟收到一个错误请求

c# - 如何在没有后端收集器的情况下将 opentelemetry 数据导出到文件中进行测试

go - 如何使用 NATS 正确传递远程父跨度?

c++ - 这是防止 gtest (C++) 捕获异常和段错误的方法吗?

unit-testing - 我可以仅使用浅层渲染而不安装完整的组件树来完成所有单元测试吗?

c# - 将 C# 字节属性硬编码到模型中

C#:在密封类中模拟和测试 protected (或私有(private))方法——方法

mysql - Azure Application Insights Kusto 语言按时间生成值进行汇总

asp.net-core - 如何在 ASP.NET Core 中使用 opentelemetry 跟踪数据库操作?