# How to use Distributed tracing in with elastic apm in non .net core application?

**URL:** <https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [August 29, 2019, 2:10pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356 "2019-08-29T14:10:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonammarda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonammarda/32/52241_2.png) [@sonammarda](https://discuss.elastic.co/u/sonammarda)\
**Post date:** [August 29, 2019, 2:10pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/1 "2019-08-29T14:10:55Z")

</div>

Kibana version: 7.3

Elasticsearch version: 7.3

APM Server version: 7.2

APM Agent language and version: Using for .NET project.

My project is in .Net Framework 4.6.1. I can capture transactions now in my project. Now I want to add distributed tracing and log all SQL queries executed in calls. I have used below code for distributed tracing:

var tracingData = Elastic.Apm.Agent.Tracer.CurrentTransaction?.OutgoingDistributedTracingData.SerializeToString();

Elastic.Apm.Agent.Tracer.CaptureTransaction(transactionName, Elastic.Apm.Api.ApiConstants.TypeRequest, transaction =\>  
{  
transaction.Tags.Add("UserId", UserId.ToString());  
transaction.Tags.Add("EntityId", EntityId.ToString());  
transaction.Tags.Add("UserName", UserName);  
}, DistributedTracingData.TryDeserializeFromString(tracingData));

However I do not get any SQL queries or detailed traces of my call. My project structure is:

1. API Layer
2. Service/Application layer
3. Repository Layer(Which has linq queries or calls to Stored Procedure)

I have added above code to start and capture transaction and distributed tracing in API layer. So now what more changes I have to make to get all SQL queries executed and distributed tracing.

Below is what I get currently-

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/4/0424f0af1e5d9e0944c82f8b27d926991db7db31.png)

---

<div class="post-metadata">

**Author:** ![Sergey\_Kleyman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_kleyman/32/39333_2.png) [@Sergey\_Kleyman](https://discuss.elastic.co/u/Sergey_Kleyman)\
**Post date:** [August 29, 2019, 2:28pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/2 "2019-08-29T14:28:51Z")

</div>

Hi @sonammarda,

First I would recommend to use the latest version of the agent (which is 1.0.1 at the moment). Please note that we renamed `Tags` to `Labels` before 1.0 release.

Regarding the code you have provided - you do run

```auto
var tracingData = Elastic.Apm.Agent.Tracer.CurrentTransaction?.OutgoingDistributedTracingData.SerializeToString();

```

on the client side of your service communication (for example on `API layer` in the case of the first leg of communication between the layers of your application) and

```auto
Elastic.Apm.Agent.Tracer.CaptureTransaction(transactionName, Elastic.Apm.Api.ApiConstants.TypeRequest, transaction =>

```

on the server side of the of your service communication (for example on `Service/Application layer` in the case of the first leg of communication between the layers of your application), right?

---

<div class="post-metadata">

**Author:** ![Sergey\_Kleyman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_kleyman/32/39333_2.png) [@Sergey\_Kleyman](https://discuss.elastic.co/u/Sergey_Kleyman)\
**Post date:** [August 29, 2019, 2:34pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/3 "2019-08-29T14:34:22Z")

</div>

An additional question - which one is true in your case:

1. You see transactions with SQL queries but they are not connected with other layers into one trace
2. You don't see transactions with SQL queries

If it's (2) then the follow up question - have you added code like the one you provided:

```auto
Elastic.Apm.Agent.Tracer.CaptureTransaction(transactionName, Elastic.Apm.Api.ApiConstants.TypeRequest, transaction =>

```

to `Repository Layer(Which has linq queries or calls to Stored Procedure)` service's code?

---

<div class="post-metadata">

**Author:** ![sonammarda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonammarda/32/52241_2.png) [@sonammarda](https://discuss.elastic.co/u/sonammarda)\
**Post date:** [September 3, 2019, 9:33am UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/4 "2019-09-03T09:33:31Z")

</div>

Second one is true in my case. I do not see any SQL queries. I have written an attribute in web api to start and capture transaction. Below is my code:

public override void OnActionExecuting(HttpActionContext actionContext)  
{  
Elastic.Apm.Agent.Tracer.StartTransaction(someTransactionName, Elastic.Apm.Api.ApiConstants.TypeRequest);  
}

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)  
{  
var tracingData = Elastic.Apm.Agent.Tracer.CurrentTransaction?.OutgoingDistributedTracingData.SerializeToString();  
if (actionExecutedContext.Exception != null)  
{ Elastic.Apm.Agent.Tracer.CurrentTransaction?.CaptureException(actionExecutedContext.Exception, apiContextInfo, false, transactionName);  
}

```
        Elastic.Apm.Agent.Tracer.CaptureTransaction(transactionName, Elastic.Apm.Api.ApiConstants.TypeRequest,
           transaction =>
           {
               transaction.Tags.Add("UserId", apiContext?.UserId.ToString());
               transaction.Tags.Add("EntityId", apiContext?.EntityId.ToString());
               transaction.Tags.Add("UserName", apiContext?.UserName);
           }, DistributedTracingData.TryDeserializeFromString(tracingData));
    }

```

Both the above calls are made in web api layer only.

I also tried to start and capture transaction with distributed tracing data in Repository layer(where I have my linq and call to SP). Below is the code for that-

public CartSummaryDetailsDTO GetCartSummaryDetails(int userId)  
{  
Elastic.Apm.Agent.Tracer.StartTransaction("GetCartSummaryDetails-CartRepo Call", Elastic.Apm.Api.ApiConstants.TypeRequest);

```
        //My project Linq code here

```

Elastic.Apm.Agent.Tracer.CaptureTransaction("GetCartSummaryDetails-CartRepo Call", Elastic.Apm.Api.ApiConstants.TypeRequest,  
transaction =\>  
{  
transaction.Tags.Add("UserId", "someid");  
transaction.Tags.Add("EntityId", "someentityid");  
transaction.Tags.Add("UserName", "someusername");  
}, DistributedTracingData.TryDeserializeFromString(Elastic.Apm.Agent.Tracer.CurrentTransaction?.OutgoingDistributedTracingData.SerializeToString()));  
}

But with both the above methods, I am not able to see any SQL queries in kibana.  
I can only see the transaction captured in kibana with the transaction name that I have mentioned in code and added tags are seen under metadata. But no SQL queries.

Screenshot of what I see-

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/b/4bba24aaa1344f8016e68ad843df7f51c231e017.png)

Appreciate any help!

---

<div class="post-metadata">

**Author:** ![Sergey\_Kleyman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_kleyman/32/39333_2.png) [@Sergey\_Kleyman](https://discuss.elastic.co/u/Sergey_Kleyman)\
**Post date:** [September 3, 2019, 8:19pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/5 "2019-09-03T20:19:33Z")

</div>

The reason for SQL queries not being captured is because Elastic APM .NET agent doesn't have support for automatically capturing LINQ to SQL queries. As you can see in [Supported Technologies section of the documentation](https://www.elastic.co/guide/en/apm/agent/dotnet/current/supported-technologies.html) the only data access technology for which .NET agent currently supports automatic instrumentation is Entity Framework (EF) Core.

I didn't see that this is the actual reason for SQL queries not being captured - I focused on distributed tracing aspect of the question.

I opened an [issue](https://github.com/elastic/apm-agent-dotnet/issues/469) to add automatic instrumentation for LINQ to SQL. Although it's quite dated technology so I'm not sure if we will get to it in the near future. Of course community contributions are very welcome in case you would like to give a try. I added a few potential directions for implementation in the issue's description.

---

<div class="post-metadata">

**Author:** ![sonammarda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sonammarda/32/52241_2.png) [@sonammarda](https://discuss.elastic.co/u/sonammarda)\
**Post date:** [September 4, 2019, 5:10pm UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/6 "2019-09-04T17:10:02Z")

</div>

Thanks a lot for all your help!

I have one more query. As I mentioned earlier, I am able to see traces to my application(API calls) and exceptions raised in my application.

While capturing transaction, I am able to add tags which can be seen in metadata-  
Elastic.Apm.Agent.Tracer.CaptureTransaction(transactionName, Elastic.Apm.Api.ApiConstants.TypeRequest,  
transaction =\>  
{  
transaction.Tags.Add("UserId", apiContext?.UserId.ToString());  
transaction.Tags.Add("EntityId", apiContext?.EntityId.ToString());  
transaction.Tags.Add("UserName", apiContext?.UserName);  
}, DistributedTracingData.TryDeserializeFromString(tracingData));

But while capturing exception, I do not see any option to add tags to be seen in metadata. I want to add additional tags/labels under metadata while capturing exception . Please help me to do this.

---

<div class="post-metadata">

**Author:** ![Sergey\_Kleyman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_kleyman/32/39333_2.png) [@Sergey\_Kleyman](https://discuss.elastic.co/u/Sergey_Kleyman)\
**Post date:** [September 5, 2019, 4:58am UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/7 "2019-09-05T04:58:08Z")

</div>

Unfortunately providing additional labels in CaptureError and CaptureException agent's public API is not supported by .NET agent yet. I've opened [an issue for this enhancement](https://github.com/elastic/apm-agent-dotnet/issues/473).

The only workaround I can suggest until we implement the above enhancement is to add error specific labels to the transaction using [`Labels` API](https://www.elastic.co/guide/en/apm/agent/dotnet/current/public-api.html#api-transaction-tags) - the labels will be inherited by the error events generated by subsequent CaptureError and CaptureException calls because error events inherit their context (which in particular includes labels) from the corresponding transaction.

---

<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:** [September 26, 2019, 12:58am UTC](https://discuss.elastic.co/t/how-to-use-distributed-tracing-in-with-elastic-apm-in-non-net-core-application/197356/8 "2019-09-26T00:58:14Z")

</div>

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