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

No RequestTelemetry created in aspnet core TestServer #2491

Open
saikatguha opened this issue May 18, 2020 · 11 comments
Open

No RequestTelemetry created in aspnet core TestServer #2491

saikatguha opened this issue May 18, 2020 · 11 comments

Comments

@saikatguha
Copy link

Describe your environment.

  • SDK version: dotnetc:2.12.0-21496
  • .NET runtime version (.NET or .NET Core, TargetFramework in the .csproj file): netstandard2.0
  • Hosting Info (IIS/Azure WebApps/etc): In-process TestServer in Azure Functions
  • Platform and OS version: dotnet, Azure Functions 2.x, Windows 10

Steps to reproduce.

  1. Create a Startup class with services.AddApplicationInsightsTelemetry() in ConfigureServices.
  2. Create a test server using var server = new TestServer(new WebHostBuilder().UseStartup<Startup>())
  3. Execute a request on the test server using await server.CreateClient().SendAsync(new HttpRequestMessage...)

What is the expected behavior?
ApplicationInsights creates RequestTelemetry for the request just as it would for a normal Kestrel host. Additionally, the created RequestTelemetry is dependency injected into HttpContext.Features in the request processing pipeline. (This is what happens for normal Kestrel server.)

What is the actual behavior?
No RequestTelemetry is created. No RequestTelemetry is dependency injected into HttpContext.Features in the request processing pipeline.

Additional context.
Auto-collection of SQL/HTTP dependencies appears to not work in TestServer request handling pipeline (perhaps due to there not being a RequestTelemetry context).

Code that works in console app

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services) {
            services.AddApplicationInsightsTelemetry();
        }

        public void Configure(IApplicationBuilder app) {
            app.Use(async (context, next) => {
                await context.Response.WriteAsync(context.Features.Get<RequestTelemetry>()?.GetType().Name ?? "RequestTelemetry not found.");
            });
        }
    }

    public class Program
    {
        public static async Task Main(string[] args) {
            var testBuilder = new WebHostBuilder().UseDefaultServiceProvider(options => { }).UseStartup<Startup>();
            var testServer = new TestServer(testBuilder);
            var testClient = testServer.CreateClient();
            var testRequest = new HttpRequestMessage(HttpMethod.Get, "https://localhost:5001/");
            var testResponse = await testClient.SendAsync(testRequest);
            Console.WriteLine(await testResponse.Content.ReadAsStringAsync());
            // Prints: RequestTelemetry
        }
    }

Code that does not work in functions app

    public static class Function1
    {
        public class Startup
        {
            public void ConfigureServices(IServiceCollection services) {
                services.AddApplicationInsightsTelemetry();
            }

            public void Configure(IApplicationBuilder app) {
                app.Use(async (context, next) => {
                    await context.Response.WriteAsync(context.Features.Get<RequestTelemetry>()?.GetType().Name ?? "RequestTelemetry not found.");
                });
            }
        }

        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) {
            var testBuilder = new WebHostBuilder().UseDefaultServiceProvider(options => { }).UseStartup<Startup>();
            var testServer = new TestServer(testBuilder);
            var testClient = testServer.CreateClient();
            var testRequest = new HttpRequestMessage(HttpMethod.Get, "https://localhost:5001/");
            var testResponse = await testClient.SendAsync(testRequest);
            return testResponse;
            // Returns: RequestTelemetry not found.
        }
    }
@v-anvari
Copy link

Hi @saikatguha , Apologies for the delayed response. Is this scenario completely based on functions or webjobs SDK. Because the Http Trigger is only supported in functions, But, the scope in which the above scenario trigger runs is the webjobs builder scope. Dispatching comes through the job host and not through the ASP .net Http Pipeline. Setting up in the ASP .Net Http Pipeline will not work here as expected. So it is important that it will be dispatched through the job host and not through the Http server.

@ghost
Copy link

ghost commented Apr 23, 2021

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@ghost ghost added the no-recent-activity label Apr 23, 2021
@ghost ghost closed this as completed Apr 26, 2021
@saikatguha
Copy link
Author

This is functions only. Not web jobs.

@v-anvari
Copy link

@saikatguha , Thank you for your feedback! As this is functions based scenario, as suggested in the earlier comment, the scope in which the above scenario/code trigger runs is the webjobs builder scope. Dispatching comes through the job host and not through the ASP .net Http Pipeline. Setting up in the ASP .Net Http Pipeline will not work here as expected. So it is important that it will be dispatched through the job host and not through the Http server.

@ghost
Copy link

ghost commented May 24, 2021

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@saikatguha
Copy link
Author

Dispatch is happening inside the function handler.
See the code snippet here:

            var testServer = new TestServer(testBuilder);
            var testClient = testServer.CreateClient();
            var testRequest = new HttpRequestMessage(HttpMethod.Get, "https://localhost:5001/");
            var testResponse = await testClient.SendAsync(testRequest);

@v-anvari
Copy link

@saikatguha , Thank you for your feedback! We will investigate and try to reproduce this scenario and update the findings

@v-anvari
Copy link

Tagging @fabiocav , for more insights

@v-anvari
Copy link

Hi @saikatguha , Apologies for the delayed response, We would like to know if you are facing this issue in version 3.0. There is an activity set-up during the invocation and it is likely impacting the flow of information and details as they are going down to the request that's being issued against the Test client.

@saikatguha
Copy link
Author

Yes in 3 as well. It likely has to do with some global or singleton objects or thread local object since it shows up even when the TestServer/TestClient are completely disconnected from the original dispatch and request objects.

@v-anvari
Copy link

Hi @saikatguha , Thank you for the information, triaging this issue for further investigation

@v-anvari v-anvari added this to the Triaged milestone Jun 17, 2021
@v-anvari v-anvari removed their assignment Jun 17, 2021
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

3 participants