Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correlation does not work within WCF service #1631

Closed
lmolkova opened this issue Jun 15, 2018 · 6 comments
Closed

Correlation does not work within WCF service #1631

lmolkova opened this issue Jun 15, 2018 · 6 comments

Comments

@lmolkova
Copy link
Member

When WCF service makes async call to http/sql/other dependency, writes traces, or reports exceptions, child telemetries do not correlate to the request telemetry.

It happens because WCF prevents ambient correlation context to flow with the request.
WCF is working to improve this story in future.

Workaround

Use WCF behavior extensions to restore the context. This article provides examples on how to create and configure the extension.

    public class ApplicationInsightsContextRestoreHelper : IDispatchMessageInspector, IServiceBehavior
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // WORKAROUND! TEST CAREFULLY!
           // see https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/941
            var formerActivity = (Activity)HttpContext.Current?.Items["__AspnetActivity__"];
            if (formerActivity != null)
            {
                var newActvity = new Activity(formerActivity.OperationName);
                newActvity.SetStartTime(formerActivity.StartTimeUtc);
                newActvity.SetParentId(formerActivity.ParentId ?? formerActivity.RootId);
                foreach (var baggage in formerActivity.Baggage)
                {
                    newActvity.AddBaggage(baggage.Key, baggage.Value);
                }

                foreach (var tag in formerActivity.Tags)
                {
                    newActvity.AddTag(tag.Key, tag.Value);
                }

                newActvity.Start();

                var requestTelemetry = (RequestTelemetry)HttpContext.Current?.Items["Microsoft.ApplicationInsights.RequestTelemetry"];
                if (requestTelemetry != null)
                {
                    requestTelemetry.Context.Operation.Id = newActvity.RootId;
                    requestTelemetry.Context.Operation.ParentId = newActvity.ParentId;
                    requestTelemetry.Id = newActvity.Id;
                }
            }
            return null;
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints,
            BindingParameterCollection bindingParameters)
        {
            foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endpoint in dispatcher.Endpoints)
                {
                    endpoint.DispatchRuntime.MessageInspectors.Add(new ApplicationInsightsContextRestoreHelper());
                }
            }
        }
...

The workaround has a downside that telemetry reported by HttpModules/Handlers/Filters that happen in request execution pipeline BEFORE workaround will be correlated to telemetry reported in controller only by operation Id:
E.g trace1 was tracked in some HttpModule and trace2 was tracked in the WCF service.
Normally, Request telemetry becomes a parent to both traces (i.e. operation_Id match for traces and request, and request.Id match each trace operation_ParentId).

With the above workaround, operation_Ids would still match for all of them, however, trace1 operation_ParentId would NOT match request.Id, while trace2 parentId would match request Id.

@TimothyMothra TimothyMothra transferred this issue from microsoft/ApplicationInsights-dotnet-server Jan 10, 2020
@TimothyMothra TimothyMothra added this to the Future milestone Jan 10, 2020
@ank3it ank3it assigned ank3it and mrbullwinkle and unassigned ank3it Apr 30, 2020
@ank3it
Copy link
Contributor

ank3it commented Apr 30, 2020

@mrbullwinkle : To create a seperate doc for this, to drive clarity

@peta
Copy link

peta commented Jun 11, 2020

Any news on this issue?

@cijothomas
Copy link
Contributor

@peta Are you asking if there are plans in Application Insights to make correlation work automatically for WCF services?
There are no investments planned in Application Insights for WCF area in next 6 months.

@joymon
Copy link

joymon commented Feb 26, 2021

Any news about AppInsights for WCF as 6 months is over?

@cijothomas
Copy link
Contributor

None. Its likely to come via OpenTelemetry route. (OpenTelemetry WCF instrumentation+AzureMonitor exporter, which dont have feature parity with current ApplicationInsights. Don't have an ETA on when or whether it'll have same feature parity as this SDK.)

@reyang reyang removed this from the Future milestone Mar 9, 2021
@github-actions
Copy link

github-actions bot commented Jan 4, 2022

This issue is stale because it has been open 300 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants