elastic / apm-agent-dotnet

https://www.elastic.co/guide/en/apm/agent/dotnet/current/index.html
Apache License 2.0
585 stars 208 forks source link

[BUG] profiler auto-instrumention not working in win10 IIS #2485

Closed ysj2018 closed 1 week ago

ysj2018 commented 1 week ago

I would like to use Elastic APM's profiler auto-instrumentation to monitor our .NET application. I first tested it on Windows 10, but the profiling was unsuccessful. I followed the steps described on the official website:

  1. I downloaded the Elastic APM Profiler version 1.30.0 and extracted it to a directory on Windows 10.
  2. I created a basic application using .NET 5.0.408 (which provides a page with a button that, when clicked, uses HttpClient to access Baidu) (the test was functional).
  3. I packaged it into a specific directory.
  4. I enabled IIS features on Windows 10:
    • .NET Extensibility 4.6
    • ASP
    • ASP.NET 4.6
  5. I created an application pool (selected "No Managed Code" for the .NET CLR version and "Integrated" for the managed pipeline mode).
  6. I created a website (assigned port 8081).
  7. I published the application to the specified path of the website, and the application was functional.
  8. I modified the web.config file to add environment variables with the following configuration:
xml<?xml version="1.0" encoding="utf-8"?><configuration>  <location path="." inheritInChildApplications="false">    <system.webServer>      <handlers>        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />      </handlers>      <aspNetCore processPath="dotnet" arguments=".\WebApplication3.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">        <environmentVariables>          <environmentVariable name="ELASTIC_APM_SERVICE_NAME" value="dotnettest" />          <environmentVariable name="COR_ENABLE_PROFILING" value="1" />          <environmentVariable name="COR_PROFILER_PATH" value="D:\agent\elastic_apm_profiler.dll" />          <environmentVariable name="ELASTIC_APM_PROFILER_HOME" value="D:\agent\" />          <environmentVariable name="ELASTIC_APM_PROFILER_INTEGRATIONS" value="D:\agent\integrations.yml" />          <environmentVariable name="ELASTIC_APM_SERVER_URL" value="http://localhost:8200" />          <environmentVariable name="ELASTIC_OTEL_LOG_LEVEL" value="trace" />          <environmentVariable name="ELASTIC_OTEL_LOG_DIRECTORY" value="D:\agent\logs" />          <environmentVariable name="ELASTIC_APM_APPLICATION_PACKAGES" value="D:\IIS\" />          <environmentVariable name="ELASTIC_APM_PROFILER_LOG_DIR" value="D:\agent\log" />          <environmentVariable name="COR_PROFILER" value="{FA65FE15-F085-4681-9B20-95E04F6C03CC}" />        </environmentVariables>      </aspNetCore>    </system.webServer>  </location></configuration>
  1. I restarted the application, and the application was accessible normally, but I did not see any trace data (I had APM Server 7.10 running and configured to output data to Kafka), nor did I see any profiler loading logs.
  2. I restored the web.config and directly configured the variables as system variables, then restarted the application, but still did not see the profiler being loaded.

What could be the possible reasons? Are there any issues with my operations?

stevejgordon commented 1 week ago

@ysj2018, Have you viewed the profiler documentation? We provide a script to configure the environment variables against the app pool. Using web.config is untested for ASP.NET Core.

You have used the incorrect environment variables for your app. For ASP.NET Core (.NET 5+) you should be using:

CORECLR_ENABLE_PROFILING = "1"
CORECLR_PROFILER = "{FA65FE15-F085-4681-9B20-95E04F6C03CC}"
CORECLR_PROFILER_PATH = "$profilerHomeDir\elastic_apm_profiler.dll"

COR_PROFILER etc. is for .NET Framework.

Please try using the documented scripting approach to enable profiling, and let us know if you have any issues after that.

ysj2018 commented 1 week ago

Thank you for your answer. On Windows 10, I can get results by using a script to add environment variables. However, I have tried adding system variables before, but they didn't take effect. So, for previous versions of Windows, is it currently not supported to add a variable for each application?

stevejgordon commented 1 week ago

The script should work on IIS10 on all versions of Windows. It sets the environment variables on the AppPool, not system environment variables.

stevejgordon commented 1 week ago

@ysj2018 I will close this issue and switch the conversation to your discuss post. There isn't a bug to address here, and questions are best kept on the forum.