locked
ASP.Net Core 3.1 WebAPI, using TelemetryClient Azure Application insights hides operations metrics RRS feed

  • Question

  • User-50787436 posted

    Hi all, I have an ASP.Net Core 3.1 Web API installed as App Service in Azure.

    This App Service has the environment variable APPINSIGHTS_INSTRUMENTATIONKEY properly configured and until now it was writting operations like a charm.

    But my API also uses CosmosDB (Mongo) and these dependencies were not being tracked.

    After googling, I found out I had to use TelemetryClient.

    Registering:

    string appInsightsKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
    
                if (!string.IsNullOrEmpty(appInsightsKey))
                {
                    var telemetryConfig = TelemetryConfiguration.CreateDefault();
                    telemetryConfig.InstrumentationKey = appInsightsKey;
                    services.AddSingleton(new TelemetryClient(telemetryConfig));
    
                    var depModule = new DependencyTrackingTelemetryModule();
                    depModule.Initialize(telemetryConfig);
                }

    Then adding the client on my repository:

    MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(repositoryConfiguration.ConnectionString)); ;
                    settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
                    settings.SocketTimeout = TimeSpan.FromMilliseconds(repositoryConfiguration.TimeoutInMiliseconds);
                    settings.ConnectTimeout = TimeSpan.FromMilliseconds(repositoryConfiguration.TimeoutInMiliseconds);
                    settings.ServerSelectionTimeout = TimeSpan.FromMilliseconds(repositoryConfiguration.TimeoutInMiliseconds * 2);
    
                    if (_telemetryClient != null) // Initialize Application Insights
                    {
                        settings.ClusterConfigurator = clusterConfigurator =>
                        {
                            clusterConfigurator.Subscribe<CommandSucceededEvent>(e =>
                            {
                                _telemetryClient.TrackDependency("MongoDb", e.CommandName, e.Reply.ToString(), DateTime.Now.Subtract(e.Duration), e.Duration, true);
                            });
                            clusterConfigurator.Subscribe<CommandFailedEvent>(e =>
                            {
                                _telemetryClient.TrackDependency("MongoDb", $"{e.CommandName} - {e.ToString()}", e.Failure.ToString(), DateTime.Now.Subtract(e.Duration), e.Duration, false);
                            });
                        };
                    }
    
                    var mongoClient = new MongoClient(settings);

    Now when I go to my application insights, I only see dependencies and don't see any operations at all.

    Do I need to explicitly add also application insights on startup in order to use insights with telemetry client? Am I missing something here?

    Thanks a lot!

    Wednesday, February 17, 2021 3:31 PM

Answers

  • User-50787436 posted

    Tried out installing package Microsoft.ApplicationInsights.AspNetCore and added the following line, works like a charm.

    services.AddApplicationInsightsTelemetry();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 17, 2021 7:33 PM