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

[ASP.NET] Update enrich callbacks #940

Conversation

vishweshbankwar
Copy link
Member

@vishweshbankwar vishweshbankwar commented Jan 31, 2023

Fixes #766

Changes

Enrich Before

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddAspNetInstrumentation((options) => options.Enrich
        = (activity, eventName, rawObject) =>
    {
        if (eventName.Equals("OnStartActivity"))
        {
            if (rawObject is HttpRequest httpRequest)
            {
                activity.SetTag("physicalPath", httpRequest.PhysicalPath);
            }
        }
        else if (eventName.Equals("OnStopActivity"))
        {
            if (rawObject is HttpResponse httpResponse)
            {
                activity.SetTag("responseType", httpResponse.ContentType);
            }
        }
    })

Enrich with this change

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddAspNetInstrumentation(o =>
        {
            o.EnrichWithHttpRequest = (activity, httpRequest) =>
            {
                activity.SetTag("physicalPath", httpRequest.PhysicalPath);
            };
            o.EnrichWithHttpResponse = (activity, httpResponse) =>
            {
                activity.SetTag("responseType", httpResponse.ContentType);
            };
            o.EnrichWithException = (activity, exception) =>
            {
                activity.SetTag("exceptionType", exception.GetType().ToString());
            };
        })
    .Build();

For significant contributions please make sure you have completed the following items:

  • Appropriate CHANGELOG.md updated for non-trivial changes
  • Design discussion issue #

@vishweshbankwar vishweshbankwar marked this pull request as ready for review January 31, 2023 05:34
@vishweshbankwar vishweshbankwar requested a review from a team January 31, 2023 05:34
@Kielek Kielek added the comp:instrumentation.aspnet Things related to OpenTelemetry.Instrumentation.AspNet label Jan 31, 2023
Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com>
@codecov
Copy link

codecov bot commented Jan 31, 2023

Codecov Report

Merging #940 (b5db90d) into main (3ca2b34) will increase coverage by 0.02%.
The diff coverage is 87.50%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #940      +/-   ##
==========================================
+ Coverage   69.40%   69.42%   +0.02%     
==========================================
  Files         202      202              
  Lines        7668     7667       -1     
==========================================
+ Hits         5322     5323       +1     
+ Misses       2346     2344       -2     
Impacted Files Coverage Δ
...metry.Exporter.Instana/Implementation/Transport.cs 0.00% <0.00%> (ø)
.../OpenTelemetry.Exporter.Instana/InstanaExporter.cs 78.37% <100.00%> (-0.29%) ⬇️
...rumentation.AspNet/AspNetInstrumentationOptions.cs 100.00% <100.00%> (ø)
...umentation.AspNet/Implementation/HttpInListener.cs 88.31% <100.00%> (ø)

/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
public Action<Activity, HttpRequest> EnrichWithHttpRequest { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random idea. Since we are making this change...

Should we consider switching to HttpContext or adding it to the signature?

Action<Activity, HttpContext>
or
Action<Activity, HttpContext, HttpRequest>

I seem to remember users asking for that way back. Off the top of my head the nice thing HttpContext gives you is the items bag so you can easily push stuff into your telemetry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we pass in the Context, we could get rid of request. And if we do this, do we also change ASP.NET Core to match this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we pass in the Context, we could get rid of request.

Ya if you want to keep a single delegate we could probably get away with just Action<Activity, string, HttpContext, Exception?>. We could also introduce a contract that is expandable like...

public class AspNetInstrumentationOptions
{
   public AspNetEnrichAction Enrich {get; set;}
}

public delegate void AspNetInstrumentationEnrichAction(in AspNetEnrichArguments arguments);

public readonly struct AspNetInstrumentationEnrichArguments 
{
    public string EventName {get;}
    public Activity Activity {get;}
    public HttpContext HttpContext {get;}
    public Exception? Exception {get;}
}

And if we do this, do we also change ASP.NET Core to match this?

We could. Less of an issue there though because the ASP.NET Core request & response objects have a property that link back to the HttpContext. I kind of like that design with a dedicated delegate + arguments struct it feels cleaner? 🤷

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Feb 16, 2023
@github-actions github-actions bot removed the Stale label Feb 18, 2023
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Feb 25, 2023
@github-actions github-actions bot removed the Stale label Feb 28, 2023
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Mar 10, 2023
@github-actions
Copy link
Contributor

Closed as inactive. Feel free to reopen if this PR is still being worked on.

@github-actions github-actions bot closed this Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:instrumentation.aspnet Things related to OpenTelemetry.Instrumentation.AspNet Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Enrich callbacks to use specific type for rawObject
4 participants