Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

StartOperation dependency is tracked outside of dependency activity #864

Closed
zakimaksyutov opened this issue Mar 20, 2018 · 1 comment
Closed
Assignees
Labels
Milestone

Comments

@zakimaksyutov
Copy link
Member

If you are reporting bug/issue, please provide detailed Repro instructions.

Repro Steps

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;

namespace MvcFiveApp.Controllers
{
    public class TestController : ApiController
    {
        private readonly TelemetryClient telemetryClient = new TelemetryClient();

        [System.Web.Http.AcceptVerbs("GET")]
        public async Task<string> DoWork([FromUri] int WorkID)
        {
            try
            {
                Activity.Current.AddTag("WorkID", WorkID.ToString()); // Exists for Request, Dependency, Trace and Exception telemetry

                using (var dependency = this.telemetryClient.StartOperation<DependencyTelemetry>("StoreWork"))
                {
                    Activity.Current.AddTag("DependencyID", Guid.NewGuid().ToString("N")); // Exists only for Trace telemetry. Expectation: exists for Dependency as well

                    this.telemetryClient.TrackTrace("MyTrace");
                    await Task.Delay(TimeSpan.FromMilliseconds(267)).ConfigureAwait(false);
                    this.telemetryClient.TrackTrace("MyTrace2");

                    if (WorkID > 10)
                    {
                        throw new Exception("Too big ID!");
                    }
                }

                return WorkID.ToString();
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackException(ex);
                throw;
            }
        }
    }
}

Actual Behavior

TelemetryInitializer is called twice (once upon StartOperation and one as a part of Track), both with Activity.Current pointing to RequestTelemetry and not DependencyTelemetry

Expected Behavior

TelemetryInitializer is called once. Activity.Current should point to Dependency activity

Version Info

SDK Version : 2.6-beta2
.NET Version :
How Application was onboarded with SDK(VisualStudio/StatusMonitor/Azure Extension) :
OS :
Hosting Info (IIS/Azure WebApps/ etc) :

@zakimaksyutov
Copy link
Member Author

I used this TelemetryInitializer to add tags (thank you @lmolkova for helping with this!):

using System.Diagnostics;
using System.Web;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace MvcFiveApp.Controllers
{
    public class ActvityTagsTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            Activity current = Activity.Current;

            if (current == null)
            {
                current = (Activity)HttpContext.Current.Items["__AspnetActivity__"];
            }

            while (current != null)
            {
                foreach (var tag in current.Tags)
                {
                    if (!telemetry.Context.Properties.ContainsKey(tag.Key))
                    {
                        telemetry.Context.Properties.Add(tag.Key, tag.Value);
                    }
                }

                current = current.Parent;
            }
        }
    }
}

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

No branches or pull requests

3 participants