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

Incorrect Parent ID #2142

Closed
ghost opened this issue Jan 2, 2021 · 1 comment
Closed

Incorrect Parent ID #2142

ghost opened this issue Jan 2, 2021 · 1 comment
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Jan 2, 2021

Describe the bug

The parent Id seems incorrect using DependencyTrackingTelemetryModule.IncludeDiagnosticSourceActivities

To Reproduce

public class SomeDependencyDiagnosticSource
{
    public const string DiagnosticListenerName = "Company.SomeDependency";
    public const string ActivityName = DiagnosticListenerName + ".RequestOut";
    public const string ActivityStartName = ActivityName + ".Start";
    public const string ActivityStopName = ActivityName + ".Stop";

    private static readonly DiagnosticListener _diagnostic = new DiagnosticListener(DiagnosticListenerName);

    public static SomeDependencyDiagnosticSource Instance => new SomeDependencyDiagnosticSource();

    public Activity? StartActivity()
    {
        if (_diagnostic == null)
        {
            return null;
        }

        Activity? activity = null;
        if (_diagnostic.IsEnabled() && _diagnostic.IsEnabled(DiagnosticListenerName))
        {
            activity = new Activity(ActivityName);

            // add some tags/baggages to activity.
            activity.AddTag("http.url", "https://somedependency.company.net");

            if (_diagnostic.IsEnabled(ActivityStartName))
            {
                _diagnostic.StartActivity(activity, null);
            }
            else
            {
                activity.Start();
            }
        }

        return activity;
    }

    public void StopActivity(Activity? activity)
    {
        if (activity == null)
        {
            return;
        }

        // set some tags like dependency result.

        if (_diagnostic != null && _diagnostic.IsEnabled(ActivityStopName))
        {
            _diagnostic.StopActivity(activity, null);
        }
        else
        {
            activity.Stop();
        }
    }
}

Usage :

public Task<DependencyResult> CallDependency()
{
    var activity = SomeDependencyDiagnosticSource.Instance.StartActivity();
    try
    {
        // Call the dependency
    }
    finally
    {
        SomeDependencyDiagnosticSource.Instance.StopActivity(activity);
    }
}

Application Insights configuration in ASP.NET Core startup :

services.AddApplicationInsightsTelemetry(options =>
{
    options.InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"];
});
services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((m, o) =>
{
    m.IncludeDiagnosticSourceActivities.Add(SomeDependencyDiagnosticSource.DiagnosticListenerName);
});

Current result in Application Insights:

Request:

  • Operation Id: 4384d4025a4b0d4fa1ef565161c22fb0
  • Parent Id: 4384d4025a4b0d4fa1ef565161c22fb0
  • Request Id: 4894327993ba8d4b

Dependency:

  • Operation Id: 4384d4025a4b0d4fa1ef565161c22fb0
  • Parent Id: 00-4384d4025a4b0d4fa1ef565161c22fb0-4894327993ba8d4b-00
  • Dependency Id: 00-4384d4025a4b0d4fa1ef565161c22fb0-de5c292e9b06574c-00

image

The request and the dependency are at the same level. Dependency should be nested in the request in this case.

Environment:

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64

Host (useful for support):
Version: 5.0.1
Commit: b02e13abab

.NET SDKs installed:
5.0.101 [C:\Program Files\dotnet\sdk]

Application Insights SDK version : 2.16.0
Target framework in csproj: net5.0

@ghost ghost added the bug label Jan 2, 2021
@cedricfronteau
Copy link

Operation Id, parent id & telemetry id need to be set differently when activity is using W3C id format:
Current:

// properly fill dependency telemetry operation context
telemetry.Context.Operation.Id = currentActivity.RootId;
telemetry.Context.Operation.ParentId = currentActivity.ParentId;

Suggestion:

if (currentActivity.IdFormat == ActivityIdFormat.W3C)
{
    telemetry.Context.Operation.Id = currentActivity.TraceId.ToHexString();
    if (currentActivity.ParentSpanId != default)
    {
        telemetry.Context.Operation.ParentId = currentActivity.ParentSpanId.ToHexString();
    }

    telemetry.Id = currentActivity.SpanId.ToHexString();
}
else
{
    telemetry.Context.Operation.Id = currentActivity.RootId;
    telemetry.Context.Operation.ParentId = currentActivity.ParentId;
    telemetry.Id = currentActivity.Id;
}

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

No branches or pull requests

2 participants