asp.net OpenTelemetry .NET StartActiveSpan在内置span工作时不执行任何操作

pinkon5k  于 11个月前  发布在  .NET
关注(0)|答案(1)|浏览(108)

我正在按照here的说明将OpenTelemetry跟踪添加到我的Web应用程序中。我正在调用AddAspNetCoreInstrumentationAddEntityFrameworkCoreInstrumentation,这两个程序似乎都在工作;我可以在我的Trace中看到HTTP处理程序和数据库级别的span(使用Google Cloud Trace View)。但是,当我尝试使用tracer.StartActiveSpan("my-span");手动创建跟踪span时,span在任何地方都不可见。
下面是我的配置代码:

var serviceName = Environment.GetEnvironmentVariable("K_SERVICE") ?? "web-ui";

builder.Services.AddOpenTelemetry()
    .ConfigureResource(resource => resource.AddService(serviceName))
    .WithTracing(
        tracing =>
        {
            tracing
                .AddAspNetCoreInstrumentation()
                .AddEntityFrameworkCoreInstrumentation(
                    e =>
                    {
                        e.EnrichWithIDbCommand = (activity, command) =>
                        {
                            var tag = string.Empty;
                            if (command.CommandText.StartsWith("--"))
                            {
                                using var reader = new StringReader(command.CommandText);
                                tag = reader.ReadLine()?[3..];
                            }

                            if (!string.IsNullOrEmpty(tag))
                            {
                                activity.SetTag("db.query.tag", tag);
                            }
                            else
                            {
                                // Store the whole query for non-tagged queries.
                                activity.SetTag("db.query.command", command.CommandText);
                            }
                        };
                    });
            if (configuration.GetSection("Otlp").Exists())
            {
                tracing.AddOtlpExporter(e => configuration.GetSection("Otlp").Bind(e));
            }
        })
    .WithMetrics(
        metrics =>
        {
            metrics
                .AddAspNetCoreInstrumentation();
            if (configuration.GetSection("Otlp").Exists())
            {
                metrics.AddOtlpExporter(e => configuration.GetSection("Otlp").Bind(e));
            }
        });
builder.Services.AddSingleton(TracerProvider.Default.GetTracer(serviceName));

字符串
下面是我创建一个span的代码(tracer被注入到类中):

using var span = tracer.StartActiveSpan(nameof(CreateMarkdownComment));

gojuced7

gojuced71#

我缺少一些配置行:

tracing
 .AddSource(serviceName)
 .SetResourceBuilder(
     ResourceBuilder.CreateDefault()
         .AddService(serviceName: serviceName))

字符串
添加这些似乎解决了这个问题。

相关问题