我有一个示例 ASP.NET Core 6 Web 服务,它配置为使用 OpenTelemetry 和 Azure Monitor,如下所示:
// Program.cs.
using Azure.Monitor.OpenTelemetry.AspNetCore;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var resourceAttributes = new Dictionary<string, object> {
{ "service.name", "my-service" },
{ "service.namespace", "my-namespace" },
{ "service.instance.id", "my-instance" }};
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
options.ConnectionString = "<my-connection-string>";
});
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => {
builder.ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(resourceAttributes));
});
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// The rest of the logic.
然后这是我的控制器的方法:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogInformation($"my-weather-{(Random.Shared.Next(1, 99)):00}");
using (var activity = WeatherActivities.MainSource.StartActivity("Weather.Random"))
{
_logger.LogInformation("my-weather-inside-activity!");
activity?.SetTag("my-tag", Random.Shared.Next(1, 99));
activity?.AddEvent(new ActivityEvent("my-activity-event"));
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
当我点击该服务时,调用
_logger.LogInformation()
发出的信息将登陆 Azure Monitor 并可以查询。我找不到 activity?.SetTag()
和 activity.AddEvent()
调用中记录的信息。我也不确定如何查找活动源名称或资源属性。感觉好像因为某种原因没有发送。
我尝试了以下查询但没有成功:
union dependencies, traces | where customDimensions["my-tag"] != ""
customEvents | where name == "my-activity-event"
我错过了什么?预先感谢!
不幸的是,
Custom Event
目前不支持OpenTelemetry
,仅支持custom metric
、dependency
、Exception
和Resquest
。
如需参考,请查看此Microsoft 文档。
我在应用程序 Insights 中遵循了此 Microsoft document for
Opentelemetry
,它对我有用。
我在 VS 2022 中创建了一个示例
ASP.NET Core Web App
。
Program.cs
:
using Azure.Monitor.OpenTelemetry.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry().UseAzureMonitor();
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
OUTPUT
: