# DB Span Stacktrace reporting wrong filename

**URL:** <https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [April 6, 2023, 6:50am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485 "2023-04-06T06:50:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [April 6, 2023, 6:50am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/1 "2023-04-06T06:50:34Z")

</div>

**Kibana version** : 8.1.2  
**Elasticsearch version** : 8.1.2  
**APM Server version** : 8.1.2  
**APM Agent language and version** : dotnet agent version: 1.18.0

I was doing profiler auto instrumentation of a netcoreapp3.1 application :  
[CourseLibraryAPI](https://github.com/KevinDockx/BuildingRESTfulAPIAspNetCore3/tree/master/Finished%20sample/CourseLibrary)

While going through the db span stacktrace I found that filepath was wrong, even though the classname and function names are correct.

```auto
{
                "exclude_from_grouping" : false,
                "filename" : """C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.32\System.Private.CoreLib.dll""",
                "classname" : "CourseLibrary.API.Services.CourseLibraryRepository",
                "line" : {
                  "number" : 0
                },
                "function" : "GetCourses",
                "module" : "CourseLibrary.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
}

```

I am new to dotnet developement, can anyone explain why the filename is a dll not the .cs source code containing the class and the function - [link to source code with the function](https://github.com/KevinDockx/BuildingRESTfulAPIAspNetCore3/blob/e2fe73add93c232ec5c7fe6747ae1fe5d056c983/Finished%20sample/CourseLibrary/CourseLibrary.API/Services/CourseLibraryRepository.cs#L56)

Environment variables used for profiler auto instrumentation:

```auto
set CORECLR_ENABLE_PROFILING=1
set CORECLR_PROFILER={FA65FE15-F085-4681-9B20-95E04F6C03CC}
set CORECLR_PROFILER_PATH=D:\DotNet\elastic_apm_profiler_1.18.0\elastic_apm_profiler.dll
set ELASTIC_APM_PROFILER_HOME=D:\DotNet\elastic_apm_profiler_1.18.0
set ELASTIC_APM_PROFILER_INTEGRATIONS=D:\DotNet\elastic_apm_profiler_1.18.0\integrations.yml
set ELASTIC_APM_SPAN_STACK_TRACE_MIN_DURATION=0ms
set ELASTIC_APM_STACK_TRACE_LIMIT=-1
set ELASTIC_APM_SERVICE_NAME=CourseLibraryApp

```

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [April 6, 2023, 9:07am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/2 "2023-04-06T09:07:37Z")

</div>

Hi @AbhijithCV,

this is because C# is a compiled language. Once you compile a `.cs` file, the output will be a `.dll` containing the byte-code - this is also known as the .NET assembly. So when your program runs, there is no `.cs` file with source code visible to the runtime since the runtime works with the compiled assemblies which are the `.dll` files.

The unfortunate consequence of this from an APM Agent's point of view is that the Elastic APM Agent is unable to determine from which `.cs` file a given method is coming from and only reports the `.dll` file.

Nevertheless, we still report the fully qualified class name in `classname`, so that should be enough to identify which class/method you see in a given stack frame.

So, overall this is expected behaviour.

---

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [April 6, 2023, 9:39am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/3 "2023-04-06T09:39:46Z")

</div>

@GregKalapos thank you for the reply.  
I have also done profiler auto instrumentation for another asp .net core application targeting **net6.0**.  
[NopCommerce](https://github.com/nopSolutions/nopCommerce/tree/release-4.50.4)  
For this application I was getting correct filename in the span stacktrace.

```auto
{
    "exclude_from_grouping": false,
    "abs_path": "D:\\DotNet\\nopCommerce_4.50.4_Source\\Libraries\\Nop.Services\\Localization\\LocalizationService.cs",
    "filename": "D:\\DotNet\\nopCommerce_4.50.4_Source\\Libraries\\Nop.Services\\Localization\\LocalizationService.cs",
    "classname": "Nop.Services.Localization.LocalizationService",
    "line": {
        "number": 116
    },
    "function": "ResourceValuesToDictionary",
    "module": "Nop.Services, Version=4.5.0.0, Culture=neutral, PublicKeyToken=null"
}

```

Can you explain this behaviour?  
How does the agent determine to which source file the function belongs to?

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [April 8, 2023, 9:35am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/4 "2023-04-08T09:35:46Z")

</div>

> I have also done profiler auto instrumentation for another asp .net core application targeting **net6.0**.  
> [NopCommerce](https://github.com/nopSolutions/nopCommerce/tree/release-4.50.4)  
> For this application I was getting correct filename in the span stacktrace.  
> Can you explain this behaviour?

So, in additional to what I wrote in the previous answer, the agent actually tries to find the `.cs` file if possible - so indeed, the agent has a "best effort" mechanism to figure out the original `.cs` file, if this fails, the agent just reports the assembly (which is the `.dll` file) that is executed during runtime.

For determining the `.cs` file the agent uses this method: [StackFrame.GetFileName Method (System.Diagnostics) | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/api/System.Diagnostics.StackFrame.GetFileName?view=net-7.0)

That can determine the `.cs` file when debugging symbols are present and currently set up. If that method returns `null`, the agent just reports `.dll` files as the file name.

You can see this logic here: [https://github.com/elastic/apm-agent-dotnet/blob/main/src/Elastic.Apm/Helpers/StacktraceHelper.cs#L87](https://github.com/elastic/apm-agent-dotnet/blob/main/src/Elastic.Apm/Helpers/StacktraceHelper.cs#L87)

> How does the agent determine to which source file the function belongs to?

You opened another entry as well, I just see: [Custom Stacktrace for Transaction](https://discuss.elastic.co/t/custom-stacktrace-for-transaction/329160/1)

Is this question related to that entry. That indeed seems to be a bug (not sure yet, I need to dig deeper).

---

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [April 10, 2023, 6:20am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/5 "2023-04-10T06:20:36Z")

</div>

> Is this question related to that entry. That indeed seems to be a bug (not sure yet, I need to dig deeper).

Yes its related.  
I am provding steps to repoduce

**Kibana version** : 8.1.2  
**Elasticsearch version** : 8.1.2  
**APM Server version** : 8.1.2  
**APM Agent language and version** : dotnet agent version: 1.18.0

1. Clone the repo - [BuildingRESTfulAPIAspNetCore3/Finished sample/CourseLibrary at master · KevinDockx/BuildingRESTfulAPIAspNetCore3 · GitHub](https://github.com/KevinDockx/BuildingRESTfulAPIAspNetCore3/tree/master/Finished%20sample/CourseLibrary)
2. Build the application - `dotnet build`
3. Configure elastic dotnet apm agent `v1.18.0` for `profiler auto instrumentatiion` with following environment variables

```auto
set CORECLR_ENABLE_PROFILING=1
set CORECLR_PROFILER={FA65FE15-F085-4681-9B20-95E04F6C03CC}
set CORECLR_PROFILER_PATH=<agent_path>\elastic_apm_profiler.dll
set ELASTIC_APM_PROFILER_HOME=<agent_path>
set ELASTIC_APM_PROFILER_INTEGRATIONS=<agent_path>\integrations.yml
set ELASTIC_APM_SPAN_STACK_TRACE_MIN_DURATION=0ms
set ELASTIC_APM_STACK_TRACE_LIMIT=-1
set ELASTIC_APM_SERVICE_NAME=CourseLibraryApp

```

1. Launch the dotnet application from directory `CourseLibrary\CourseLibrary.API\bin\Debug\netcoreapp3.1` by using the command `dotnet CourseLibrary.API.dll`
2. Hit one of the rest api : `http://localhost:5000/api/authors`
3. Check the db span stacktrace from Kibana.

```auto
{
                "exclude_from_grouping" : false,
                "filename" : """C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.32\System.Private.CoreLib.dll""",
                "classname" : "CourseLibrary.API.Controllers.AuthorsController",
                "line" : {
                  "number" : 0
                },
                "function" : "GetAuthors",
                "module" : "CourseLibrary.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
              }

```

`GetAuthors` function is part of the application, but its filename is wrong.  
[Course Library application - GetAuthors function](https://github.com/KevinDockx/BuildingRESTfulAPIAspNetCore3/blob/e2fe73add93c232ec5c7fe6747ae1fe5d056c983/Finished%20sample/CourseLibrary/CourseLibrary.API/Services/CourseLibraryRepository.cs#L126)

Question: Agent should have resolved the filename right? since the symbol file .pdb is available.

---

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [April 18, 2023, 9:38am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/6 "2023-04-18T09:38:16Z")

</div>

Hi @GregKalapos,  
Was this issue reproducable?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 9, 2023, 5:39am UTC](https://discuss.elastic.co/t/db-span-stacktrace-reporting-wrong-filename/329485/7 "2023-05-09T05:39:01Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
