# .NET Profiler does not trace/show MySQL transactions

**URL:** <https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591>\
**Category:** APM\
**Tags:** docker, dotnet\
**Created:** [February 18, 2022, 10:23am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591 "2022-02-18T10:23:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [February 18, 2022, 10:23am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/1 "2022-02-18T10:23:30Z")

</div>

Hi,

I'm trying to get insights in our MySQL queries in our APM transactions.

I've been following this guide: [Profiler Auto instrumentation | APM .NET Agent Reference [master] | Elastic](https://www.elastic.co/guide/en/apm/agent/dotnet/master/setup-auto-instrumentation.html)  
Our application is containerized (normally hosted in kubernetes, used local docker for this test).

The docker file I've created

```auto

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

ARG AGENT_VERSION=1.14.0

WORKDIR /src
COPY . .
WORKDIR /src/API

RUN apt-get -y update && apt-get -yq install unzip
RUN curl -L -o elastic_apm_profiler_${AGENT_VERSION}.zip https://github.com/elastic/apm-agent-dotnet/releases/download/${AGENT_VERSION}/ElasticApmAgent_${AGENT_VERSION}.zip  
RUN unzip elastic_apm_profiler_${AGENT_VERSION}.zip -d /elastic_apm_profiler_${AGENT_VERSION}

RUN dotnet publish API.csproj -c Release -o /app

FROM base AS final
WORKDIR /app

ARG AGENT_VERSION=1.14.0

COPY --from=build /app .
COPY --from=build /elastic_apm_profiler_${AGENT_VERSION} /elastic_apm_profiler
RUN mkdir /test

ENV CORECLR_ENABLE_PROFILING=1
ENV CORECLR_PROFILER={FA65FE15-F085-4681-9B20-95E04F6C03CC}
ENV CORECLR_PROFILER_PATH=/elastic_apm_profiler/libelastic_apm_profiler.so
ENV ELASTIC_APM_PROFILER_HOME=/elastic_apm_profiler
ENV ELASTIC_APM_PROFILER_INTEGRATIONS=/elastic_apm_profiler/integrations.yml

ENTRYPOINT ["dotnet", "API.dll"]

```

In the Startup.cs of the API project:

```auto
        Environment.SetEnvironmentVariable("ELASTIC_APM_ENVIRONMENT", "env.");
        Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", "permission-api");
        Environment.SetEnvironmentVariable("ELASTIC_APM_SECRET_TOKEN", "##");
        Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URL", "##");
        app.UseAllElasticApm();

```

And an example of a DB query in 1 of our repositories:

```auto
      using var connection = await _ConnectionFactory.CreateConnectionAsync();
      const string statement = "SELECT * FROM partner_permission WHERE ActorId = @actorId;";

      return await connection.QueryAsync<PartnerPermission>(statement, new { actorId });

```

Even tried adding a transaction:

```auto
      using var connection = await _ConnectionFactory.CreateConnectionAsync();
      using var transaction = connection.BeginTransaction();
      const string statement = "SELECT * FROM partner_permission WHERE ActorId = @actorId;";

      return await connection.QueryAsync<PartnerPermission>(statement, new { actorId }, transaction);

```

In the APM traces I still see a gap where the MySQL queries are being executed.

(relevant) Nuget packages used:  
Elastic.Apm.NetCoreAll 1.14.0  
MySql.Data 8.0.28  
Dapper 2.0.123

What am I missing?

Thanks,  
Danny

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 18, 2022, 1:45pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/2 "2022-02-18T13:45:31Z")

</div>

MySql commands are captured as APM spans, and spans are captured only if there is an active [**APM transaction**](https://www.elastic.co/guide/en/apm/get-started/current/transactions.html).

In conjunction with other integrations, such as those for [ASP.NET](http://ASP.NET) and [ASP.NET](http://ASP.NET) Core for example, an APM transaction is started for the incoming request, so MySql spans would be captured inside of controller actions. If you're using the agent in a context where there isn't an active APM transaction, you can [start one manually](https://www.elastic.co/guide/en/apm/agent/dotnet/current/public-api.html#api-tracer-api), then MySql commands will be captured as spans whilst the transaction is active.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 18, 2022, 1:55pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/3 "2022-02-18T13:55:39Z")

</div>

It looks like you're using the [ASP.NET](http://ASP.NET) Core integration too, so there should be transactions for incoming requests. Is it just the MySql spans that are missing?

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [February 21, 2022, 6:48am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/4 "2022-02-21T06:48:17Z")

</div>

Yes, only the MySql spans are missing. Any HTTP call made to other services are showing as expected.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 21, 2022, 11:53am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/5 "2022-02-21T11:53:55Z")

</div>

Are you able to set the profiler log level to trace and IL logging, and share the profiler log? The following environment variables should be set

```auto
ELASTIC_APM_PROFILER_LOG_DIR="<log dir>"
ELASTIC_APM_PROFILER_LOG="trace"
ELASTIC_APM_PROFILER_LOG_IL="1"

```

Set `<log dir>` to a directory that the profiler will have access to write logs to.

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [February 24, 2022, 8:37am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/6 "2022-02-24T08:37:00Z")

</div>

Sorry for the late reply, somehow I dont receive email updates.

I've added the following to my dockerfile

```auto
RUN mkdir /apm_logs
RUN chmod 777 /apm_logs
ENV ELASTIC_APM_PROFILER_LOG_DIR="/apm_logs"
ENV ELASTIC_APM_PROFILER_LOG="trace"
ENV ELASTIC_APM_PROFILER_LOG_IL="1"

```

The logs directory remains empty though.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 25, 2022, 11:09am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/7 "2022-02-25T11:09:25Z")

</div>

The fact we're not seeing a log file indicates to me that the profiler is not running. Looking at the Dockerfile, the command to download the profiler isn't for the right release artifact- it's downloading the startup hooks instead of the profiler.

I've put together [an example app that demonstrates ASP.NET Core instrumented with the profiler](https://github.com/russcam/ProfilerExample).

1. clone the repository at [GitHub - russcam/ProfilerExample: ASP.NET Core application instrumented with Elastic APM profiler](https://github.com/russcam/ProfilerExample)

2. clone [GitHub - elastic/apm-integration-testing: APM Integration Testing](https://github.com/elastic/apm-integration-testing), which is an easy way to spin up Kibana, Elasticsearch and APM server with docker.

3. Start docker 7.16.0 Elastic stack from the apm-integration-testing local directory

4. Start mysql docker container on the same docker network as the stack

5. Build the example application from the repository local directory

6. Run application on same docker network as the stack

7. Open app on [http://localhost:5000](http://localhost:5000). Observe that there are MySql version details on the landing page.

8. Open Kibana at [http://localhost:5601](http://localhost:5601) and log in with `admin` and password `changeme`

9. Navigate to the APM UI and observe that the transaction contains MySql spans

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/7/87b02a4659f63018624d2cb95873eb1545a20628.png)

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [February 25, 2022, 2:53pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/8 "2022-02-25T14:53:25Z")

</div>

Thanks a lot for this example!

I'll try it out, but might take a week or two. I'll get back to you to let you know if it worked out.

Thanks  
Danny

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [March 7, 2022, 1:39pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/9 "2022-03-07T13:39:46Z")

</div>

Hi Russ,

So I managed to get it working (kind of). Also in the program/startup of the API there were a few minor differences.

The only issue I'm having now, is that the first API call is fully traced (including the mysql queries), but in all subsequent calls they are partially or fully missing again.

Both images below are the same call to the example you provided, where the first call has a lot more details.

 ![call1](https://us1.discourse-cdn.com/elastic/original/3X/5/0/50070397e69eb79f79ab4eef13b5a4f6f043271d.png)

 ![call2](https://us1.discourse-cdn.com/elastic/original/3X/0/5/058f545be43a6f09fc8ea16ca4ac6eebd3624111.png)

In our application, after the first call the MySQL traces are missing in total.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 7, 2022, 2:10pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/10 "2022-03-07T14:10:55Z")

</div>

Can you please show exactly how the agent configuration is performed in startup. The differences mentioned may explain the behaviour you're seeing

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [March 7, 2022, 3:37pm UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/11 "2022-03-07T15:37:53Z")

</div>

The shots were from the ProfilerExample repository. The only thing changes in there was the `ELASTIC_APM_SERVER_URL` variable to point to our production environment instead of the local docker one.

In our application, the startup is not really similiar to your example (we're still using the older .NET's Program/Startup one), but in `Startup.cs` in the `Configure` method I changed

```auto
        app.UseAllElasticApm();

```

to

```auto
        Agent.Subscribe(new AspNetCoreDiagnosticSubscriber());
        Agent.Subscribe(new AspNetCoreErrorDiagnosticsSubscriber());
        Agent.Subscribe(new HttpDiagnosticsSubscriber());

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 8, 2022, 5:10am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/12 "2022-03-08T05:10:10Z")

</div>

I've looked at the screenshots again, and what you're seeing between the first trace and subsequent traces is the correct behaviour of `MySql.Data`; the driver collects information on the first connection

> <https://github.com/mysql/mysql-connector-net/blob/4306c8484ec74b3ee1c349847f66acabbce6d63c/MySQL.Data/src/Driver.cs#L197-L274>

So, this the expected behaviour of the `MySql.Data` driver and profiler auto instrumentation.

---

<div class="post-metadata">

**Author:** ![Danny\_Stommen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danny_stommen/32/77845_2.png) [@Danny\_Stommen](https://discuss.elastic.co/u/Danny_Stommen)\
**Post date:** [March 8, 2022, 7:31am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/13 "2022-03-08T07:31:50Z")

</div>

Good to know that part is expected behavior.

Then I'll I just need to find out why traces are nog being logged at all for subsequent calls in our application.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 8, 2022, 9:29am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/14 "2022-03-08T09:29:47Z")

</div>

Is it possible to debug step through the subscribers? Specifically, the `AspNetCoreDiagnosticSubscriber`, which creates transactions for incoming requests.

Is there anything in the agent log or profiler logs that might provide more details? The agent log level can be in increased to `trace` to log a verbose amount of agent operation.

---

<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:** [March 29, 2022, 5:29am UTC](https://discuss.elastic.co/t/net-profiler-does-not-trace-show-mysql-transactions/297591/15 "2022-03-29T05:29:50Z")

</div>

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