I have a console application written in c# and .net 6 and it's using Application Insights.
Some of the dependencies are discovered automatically and is shown in the Application Map. The application is also using the MQTTnet library to send messages to a MQTT broker. Since these calls are not showing in the Application Map automaticall, I added a call to TrackDependency to add it manually like this:
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
bool success = false;
try
{
await _mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
success = true;
}
finally
{
timer.Stop();
ApplicationTelemetry.TelemetryClient?.TrackDependency("EventBusTopic", "PublishAsync", null, startTime, timer.Elapsed, success);
}
I can see that the data is ingested into Application Insights by searching for "Dependency" in the Transaction search:
However the dependency does not show up in the Application Map.
This is the code initializing Application Insights:
IServiceCollection services = new ServiceCollection();
ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions();
options.ConnectionString = configuration["ApplicationInsights:ConnectionString"];
services.AddApplicationInsightsTelemetryWorkerService(options);
IServiceProvider serviceProvider = services.BuildServiceProvider();
ApplicationTelemetry.TelemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
What could be the reason?
Probably because it has no operation id. You should start an operation like this:
using (var op = ApplicationTelemetry.TelemetryClient.StartOperation<DependencyTelemetry>("PublishAsync"))
{
op.Telemetry.Type = "EventBusTopic";
op.Telemetry.Success = false;
await _mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
op.Telemetry.Success = true;
}
it will automatically track the start time and duration this way and it gets an operation id. If there is already an operation pending it will be attached as a child operation to that parent.