question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App Insights Telemetry

See original GitHub issue

When CoreWCF requests come into application insights, I just see POST /MyService.svc. There isn’t any telemetry that tells me the actual service method that was called. I would like to see CoreWCF either support this natively or by including another CoreWCF package.

If someone can point me in the right direction on how to do this, I’d be willing to spend some time working this out.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
roend83commented, Aug 1, 2022

I was able to get this working in my application by including the Microsoft.ApplicationInsights.AspNetCore package, registering app insights on startup, and adding this service behavior:

public class AppInsightsTelemetryServiceBehavior : IServiceBehavior
{
    private IHttpContextAccessor HttpContextAccessor { get; }

    public AppInsightsTelemetryServiceBehavior(IHttpContextAccessor httpContextAccessor)
    {
        HttpContextAccessor = httpContextAccessor;
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        var handler = new RequestHandler(HttpContextAccessor);

        foreach (var dispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            dispatcher.ErrorHandlers.Insert(0, handler);

            foreach (var endpoint in dispatcher.Endpoints)
            {
                endpoint.DispatchRuntime.MessageInspectors.Insert(0, handler);
            }
        }
    }

    private class RequestHandler : IDispatchMessageInspector, IErrorHandler
    {
        private IHttpContextAccessor HttpContextAccessor { get; }

        public RequestHandler(IHttpContextAccessor httpContextAccessor)
        {
            HttpContextAccessor = httpContextAccessor;
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            SetRequestTelemetry();
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            SetRequestTelemetry();
        }

        public bool HandleError(Exception error)
        {
            return false;
        }

        private void SetRequestTelemetry()
        {
            var requestTelemetry = HttpContextAccessor.HttpContext.Features.Get<RequestTelemetry>();
            if (requestTelemetry is null) return;

            var context = OperationContext.Current;
            if (context is null) return;

            var dispatchRuntime = context.EndpointDispatcher.DispatchRuntime;
            var operation = context.EndpointDispatcher.DispatchRuntime.Operations
                .SingleOrDefault(operation => operation.Action == context.IncomingMessageHeaders.Action);
            if (operation is null) return;

            requestTelemetry.Name = $"{dispatchRuntime.Type.Name}/{operation.Name}";
        }
    }
}

Most of this logic is borrowed and slightly modified from the ApplicationInsights-SDK-Labs project. If this is useful to others I can package it up on my own or add it as a new project in CoreWCF. Let me know what you think.

1reaction
mconnewcommented, Apr 23, 2022

I would be open to having this be built in. My only criteria with the implementation is that it’s pay to play. That is, if you aren’t using App Insights, there’s no runtime cost of checking if it’s enabled all the time. We have a new middleware style pipeline so it might be worth adding it to our dispatch pipeline as an optional layer so that it gets put into the call path if used, but if not needed, it’s out of the call path altogether.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Application Insights overview - Azure Monitor
Application Insights is an extension of Azure Monitor and provides application performance monitoring (APM) features. APM tools are useful to ...
Read more >
Monitoring Applications using Application Insights
Visual Studio Application Insights is an extensible analytics service that monitors your live web application. With it you can detect and diagnose performance ......
Read more >
User Telemetry and Perf Monitoring with App Insights
Using the out-of-box telemetry for Application Insights, the teams will be able to find out how people use the application and gain insights...
Read more >
Application Insights Telemetry - Implementing Continuous ...
Adding telemetry is very easy. From within Visual Studio select Add Application Insights Telemetry from the project menu and follow the wizard. When...
Read more >
Tracking Application Insights Custom Events
Application Insights telemetry model defines a way to correlate telemetry to the operation of which it's a part. For example, a request can ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found