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 Core Requests Instrumentation - change to use new Activity API #692

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/Exporters/Web/OpenTelemetry.Exporter.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="..\..\..\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\src\OpenTelemetry.Instrumentation.Dependencies\OpenTelemetry.Instrumentation.Dependencies.csproj" />
<ProjectReference Include="..\..\..\src\OpenTelemetry.Exporter.Zipkin\OpenTelemetry.Exporter.Zipkin.csproj" />
Expand Down
6 changes: 6 additions & 0 deletions samples/Exporters/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using OpenTelemetry.Exporter.Console;
using OpenTelemetry.Trace.Configuration;

namespace API
Expand Down Expand Up @@ -36,6 +37,11 @@ public void ConfigureServices(IServiceCollection services)
}
});


OpenTelemetrySdk.EnableOpenTelemetry(
(builder) => builder.AddActivitySource("Microsoft.AspNetCore")
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = opt.DisplayAsJson));
Copy link
Member Author

Choose a reason for hiding this comment

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

for easily trying this out locally, i changed to use consoleexporter.
The Instrumention(Adapter) hook to OpenTelemetryBuilder is not implemented in this draft PR.


services.AddOpenTelemetry((sp, builder) =>
{
builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ internal class HttpInListener : ListenerHandler
private readonly bool hostingSupportsW3C = false;
private readonly AspNetCoreInstrumentationOptions options;

// Create an ActivitySource here to re-create Activity.
private ActivitySource activitySource = new ActivitySource("Microsoft.AspNetCore");
Copy link
Contributor

Choose a reason for hiding this comment

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

ActivitySource [](start = 16, length = 14)

do we usually create only one instance of HttpInListener and use it the life time of the app/service? I am asking to know if we need to explicitly dispose this ActivitySource when it is not needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes only one instance for life of app.

Copy link
Contributor

Choose a reason for hiding this comment

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

good. we should be ok here then. thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the existing implementation, we dispose the DiagnosticListeners when TracerFactory is disposed. Don't think we need it here.


public HttpInListener(string name, Tracer tracer, AspNetCoreInstrumentationOptions options)
: base(name, tracer)
{
Expand All @@ -60,11 +63,21 @@ public override void OnStartActivity(Activity activity, object payload)
return;
}

// Create a brand new activity using ActivitySource
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this code into existing place itself, which does span population. Once we settle on approach, we'll be removing all the span populations and replace with activity.

// and populate its tags correctly.
// This is double allocation of Activity! and probably bad for performance.
var newActivity = this.activitySource.StartActivity("HttpRequestIn", ActivityKind.Server);

var request = context.Request;

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";

if (newActivity != null)
{
newActivity.DisplayName = path;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

you can write this block as

newActivity?.DisplayName = path;

Copy link
Member Author

Choose a reason for hiding this comment

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

image
Gets this error. Need a newer c# version for it to work?

Copy link
Contributor

@tarekgh tarekgh May 21, 2020

Choose a reason for hiding this comment

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

add

    <LangVersion>7.3</LangVersion>  

to the csproj

TelemetrySpan span;
if (this.hostingSupportsW3C && this.options.TextFormat is TraceContextFormat)
{
Expand All @@ -90,6 +103,28 @@ public override void OnStartActivity(Activity activity, object payload)
span.PutHttpUserAgentAttribute(userAgent);
span.PutHttpRawUrlAttribute(GetUri(request));
}

if (newActivity != null)
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can probably combine these two if blocks.

{
if (newActivity.IsAllDataRequested)
{
if (request.Host.Port == 80 || request.Host.Port == 443)
{
newActivity.AddTag("http.host", request.Host.Host);
Copy link
Member

Choose a reason for hiding this comment

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

Why do you not include the port for 80/443?

Copy link
Member Author

Choose a reason for hiding this comment

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

just retaining the current behavior. Current behavior is probably because 80,443 are common and can be deducted from other info like url etc.

}
else
{
newActivity.AddTag("http.host", request.Host.Host + ":" + request.Host.Port);
}

newActivity.AddTag("http.method", request.Method);
newActivity.AddTag("http.path", path);

var userAgent = request.Headers["User-Agent"].FirstOrDefault();
newActivity.AddTag("http.user_agent", userAgent);
Copy link
Member

Choose a reason for hiding this comment

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

It's possible for FirstOrDefault to return null so we should add null check too.

Suggested change
newActivity.AddTag("http.user_agent", userAgent);
if (!string.IsNullOrEmpty(userAgent))
{
newActivity.AddTag("http.user_agent", userAgent);
}

newActivity.AddTag("http.url", GetUri(request));
}
}
}

public override void OnStopActivity(Activity activity, object payload)
Expand All @@ -114,8 +149,15 @@ public override void OnStopActivity(Activity activity, object payload)
var response = context.Response;

span.PutHttpStatusCode(response.StatusCode, response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase);

// activity here will be the activity which we created using ActivitySource in the Start callback.
Copy link
Contributor

Choose a reason for hiding this comment

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

// activity here will be the activity which we created using ActivitySource in the Start callback. [](start = 15, length = 99)

how this magic is done? doesn't OnStopActivity is teh call back called from DiagnosticListener?

Copy link
Member Author

Choose a reason for hiding this comment

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

diagnosticlistener callback itself doesn't give us activity. Instead we access it using Activity.Current.
https://github.com/open-telemetry/opentelemetry-dotnet/blob/master/src/OpenTelemetry/Instrumentation/DiagnosticSourceListener.cs#L40

Since we started a new activity() in the OnBegin, Activity.Current will now be the one we created in OnBegin. This will have its .Parent set to the one created by "old" instrumentation code using DiagnosticSource.StartActivity

Copy link
Contributor

Choose a reason for hiding this comment

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

make sense. thanks for the clarification. I am not expecting DiagnsoticListener will notify on the stop event of the Activity created from ActivitySource, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct. (In fact diagnosticlistener will only notify for diagnosticsource.write events, never for activity.start or stop. On listening side, we get Activity.Current to get the activity)

if (activity.IsAllDataRequested)
{
activity.AddTag("http.status_code", response.StatusCode.ToString());
}
}

activity.Stop();
span.End();
}

Expand Down