[英].NET OpenTelemetry Not Adding Listener For ActivitySource
Why is OpenTelemetry .NET not adding a listener for my ActivitySource
in a hosted service on generic host?为什么 OpenTelemetry .NET 没有在通用主机上的托管服务中为我的
ActivitySource
添加侦听器?
I have some configurations to pass into OpenTelemetry set-up, so I depend on a class MyClass
.我有一些配置要传递到 OpenTelemetry 设置中,所以我依赖于 class
MyClass
。 This means registering that class, and then configuring a callback action for TracerProviderBuilder
which uses that class.这意味着注册 class,然后为使用该 class 的
TracerProviderBuilder
配置回调操作。 However, when I later create an ActivitySource
and start an Activity
with ActivitySource.StartActivity(...)
then it returns null because no listeners were attached to the source.但是,当我稍后创建一个
ActivitySource
并使用ActivitySource.StartActivity(...)
启动一个Activity
时,它会返回 null 因为没有监听器连接到源。 By debugging and inspecting the ActivitySource
, I could see the list of s_activeSources
which included the OpenTelemetry.HttpRequest
ActivitySource
created by .AddHttpClientInstrumentation()
so it's odd that no listener was added for that either.通过调试和检查
ActivitySource
,我可以看到s_activeSources
列表,其中包括由.AddHttpClientInstrumentation()
创建的OpenTelemetry.HttpRequest
ActivitySource
,所以奇怪的是也没有为此添加侦听器。
Note: This is being run in a unit test and failing, whereas it is working in a long-running worker service program.注意:这是在单元测试中运行并失败,而它在长时间运行的工作服务程序中运行。 Not sure if unit tests are too short-lived or are influenced by test environment eg Visual Studio
不确定单元测试是否过于短暂或受测试环境(例如 Visual Studio)的影响
Below is a minimal version of my scenario:以下是我的场景的最小版本:
Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services
.AddSingleton<MyClass>()
.AddOpenTelemetryTracing(hostingBuilder =>
hostingBuilder
.Configure(
(sp, builder) =>
{
var myClass = sp.GetRequiredService<MyClass>();
// Do something with myClass on builder
builder
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddSource("TestService*");
}))
.AddHostedService<TestService>();
})
.Build();
TestService.cs
internal class TestService : BackgroundService
{
private static ActivitySource testActivitySource = new ActivitySource("TestService");
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Run(() =>
{
using (var activity = testActivitySource.StartActivity("TestService", ActivityKind.Server))
{
Console.WriteLine("I'm inside the activity using clause!");
activity?.SetTag("foo", 1);
Console.WriteLine($"activity.Tags: {activity.Tags}"); // This throws NullReferenceException since activity is null due to no listeners
}
Console.WriteLine("I'm outside the activity using clause!");
});
}
}
Package versions: Package 版本:
<package id="OpenTelemetry" version="1.3.0" />
<package id="OpenTelemetry.Api" version="1.3.0" />
<package id="OpenTelemetry.Exporter.Console" version="1.3.0" />
<package id="OpenTelemetry.Extensions.Hosting" version="1.0.0-rc9.5" />
<package id="OpenTelemetry.Instrumentation.AspNet" version="1.0.0-rc9.5" />
<package id="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule" version="1.0.0-rc9.5" />
<package id="OpenTelemetry.Instrumentation.Http" version="1.0.0-rc9.5" />
I've also tried moving the .AddConsoleExporter()
line out of the .Configure
method and simply called straight away by hostingBuilder
but still no listener attached to any ActivitySource
.我还尝试将
.AddConsoleExporter()
行移出.Configure
方法,并由hostingBuilder
直接调用,但仍然没有连接到任何ActivitySource
的侦听器。
In your unit test project, u have created the builder, but you haven't started the builder.在您的单元测试项目中,您已经创建了构建器,但您还没有启动构建器。 Unless you don't start the Host it will not add the listener.
除非您不启动主机,否则它不会添加侦听器。 Try using below code snippet.
尝试使用下面的代码片段。
var builder = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services
.AddSingleton<MyClass>()
.AddOpenTelemetryTracing(hostingBuilder =>
hostingBuilder
.Configure(
(sp, builder) =>
{
var myClass = sp.GetRequiredService<MyClass>();
// Do something with myClass on builder
builder
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddSource("TestService*");
}))
.AddHostedService<TestService>();
}).Build();
var host = builder.Build();
await host.StartAsync();
await host.StopAsync();
host.Dispose();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.