question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to configure the .netcore tracer library

See original GitHub issue

Describe the bug I am configuring APM tracing in a .NET Core 5 API service. We have managed to successfully get APM traces consumed by Datadog, but it seems the Datadog.Tracing library is detached from this. We also want to use the tracing library to add custom tags to APM spans.

There are two reasons I believe the tracing library is “detached”:

  1. The settings (service name, environment and version) initialised with the tracer aren’t those sent to Datadog (rather they appear to be taking on the kubernete’s host tags)
  2. Tracer.Instance.ActiveScope is null.

Dockerfile excerpt:

RUN TRACER_VERSION=$(curl -s \https://api.github.com/repos/DataDog/dd-trace-dotnet/releases/latest | grep tag_name | cut -d '"' -f 4 | cut -c2-) \
    && curl -Lo /tmp/datadog-dotnet-apm.deb https://github.com/DataDog/dd-trace-dotnet/releases/download/v${TRACER_VERSION}/datadog-dotnet-apm_${TRACER_VERSION}_amd64.deb
...
COPY --from=builder /tmp/datadog-dotnet-apm.deb /tmp/datadog-dotnet-apm.deb
RUN mkdir -p /opt/datadog \
    && mkdir -p /var/log/datadog \
    && dpkg -i /tmp/datadog-dotnet-apm.deb \
    && rm /tmp/datadog-dotnet-apm.deb
ENV CORECLR_ENABLE_PROFILING=1
ENV CORECLR_PROFILER={846F5F1C-F9AE-4B07-969E-05C26BC060D8}
ENV CORECLR_PROFILER_PATH=/opt/datadog/Datadog.Trace.ClrProfiler.Native.so
ENV DD_DOTNET_TRACER_HOME=/opt/datadog
ENV DD_INTEGRATIONS=/opt/datadog/integrations.json
ENV DD_TRACE_ROUTE_TEMPLATE_RESOURCE_NAMES_ENABLED=true

.NET Tracer configuration (in Startup.Configure):

var settings = TracerSettings.FromDefaultSources();
settings.TraceEnabled = true;
settings.Environment = environment; // Not represented in Datadog
settings.ServiceName = serviceName; // Not represented in Datadog
settings.ServiceVersion = serviceVersion; // Not represented in Datadog
settings.RuntimeMetricsEnabled = true; // Not represented in Datadog

var tracer = new Tracer(settings);
Tracer.Instance = tracer;

Custom span tagging:

var scope = Tracer.Instance.ActiveScope; // This is null!
scope.Span.SetTag("tenantId", tenantId);

Expected behavior

  • Our Datadog APM service should be configured with the environment, service name and version configured in the .NET tracing library.
  • Tracer.Instance.ActiveScope should expose the Datadog tracing instance (and not be null!)

Runtime environment (please complete the following information):

  • Datadog container agent (Kubernetes): gcr.io/datadoghq/agent:latest
  • Datadog.Trace 1.29. I have also tried with Datadog.Monitoring.Distribution 12.28.8-beta01 (I don’t understand which is correct)
  • Docker linux base mcr.microsoft.com/dotnet/aspnet:5.0
  • .NET 5.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lucaspimentelcommented, Nov 8, 2021

I now have Option 2 working correctly

Glad to see this is resolved! Thanks @andrewlock for jumping in.

1reaction
andrewlockcommented, Nov 5, 2021

Hi @davenewza,

  • Datadog.Trace is only used for manual instrumentation, or to configure the tracer in code
  • Datadog.Monitoring.Distribution is essentially a NuGet alternative to the installer, which includes everything you need for automatic instrumentation. This NuGet package also transitively references Datadog.Trace.

So if, as in your case, you want both automatic instrumentation and manual instrumentation, you will need to either

  • Add the Datadog.Monitoring.Distribution NuGet only, and configure the environment variables as described in the documentation.
  • Add the Datadog.Trace NuGet and install the tracer package. As described in the previous comment, it is important you keep the version of the installer and the NuGet package in sync.

Both of these approaches work the same, but if you use the Datadog.Monitoring.Distribution, NuGet, then you don’t have to worry about keeping things in sync; there’s only one version number to worry about 🙂

Whichever approach you use, be sure to run the createLogPath.sh script (or manually create the logging directory) in your Docker container, as described in the documentation..

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the .NET Core Tracing Library
To configure the tracer in application code, create a TracerSettings instance from the default configuration sources. Set properties on this TracerSettings ...
Read more >
Tracing .NET Core Applications
To enable the .NET Tracer for your service, set the required environment variables and restart the application. For information about the different methods...
Read more >
dotnet-trace diagnostic tool - .NET CLI
The dotnet-trace tool: Is a cross-platform .NET Core tool. Enables the collection of .NET Core traces of a running process without a native ......
Read more >
Add distributed tracing instrumentation - .NET
A tutorial to instrument distributed traces in .NET applications.
Read more >
Monitoring .NET and .NET Core based applications
NET Core tracing for your environment, you can view metrics that are related ... IL-rewriter and Instrumentation libraries for Linux containers; Setting up ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found