Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New relic TraceId Not getting poplulated in .net core middleware #2707

Closed
cpr4377 opened this issue Aug 22, 2024 · 2 comments
Closed

New relic TraceId Not getting poplulated in .net core middleware #2707

cpr4377 opened this issue Aug 22, 2024 · 2 comments
Labels
bug Something isn't working community To tag external issues and PRs

Comments

@cpr4377
Copy link

cpr4377 commented Aug 22, 2024

New relic TraceId Not getting poplulated in .net core middleware.

Description
I am trying to access the newrelic traceid in asp.net core middleware and pass it as a response header in the response.

Please see the implementation below :

`public sealed class TraceIdMiddleware
{
       private readonly RequestDelegate _next;
       private readonly ILogger<TraceIdMiddleware> _logger;

   public TraceIdMiddleware(RequestDelegate next, ILogger<TraceIdMiddleware> logger)
   {
       _next = next;
       _logger = logger;
   }

   public async Task InvokeAsync(HttpContext context)
   {
       if (!context.Request.Headers.ContainsKey("X-Trace-Id"))
       {
           var tracer = NewRelic.Api.Agent.NewRelic.GetAgent();
           var traceId = tracer.TraceMetadata.TraceId;
           
           _logger.LogWarning("NewRelic Trace Id " + traceId);
           _logger.LogWarning("Http context trace Id " + context.TraceIdentifier);
         
           if (!string.IsNullOrEmpty(traceId))
           {
               context.Response.Headers.Append("X-Trace-Id", traceId);
           }
       }
       await _next(context);
   }
   }`

But the trace id is always coming as empty . The agent is running and distributed tracing is enabled, still the issue persist. Has anyone encountered this issue ?

Expected Behavior
Traceid should not be coming empty

Using the latest agent and latest donet newrelic api

@cpr4377 cpr4377 added the bug Something isn't working label Aug 22, 2024
@workato-integration
Copy link

@github-actions github-actions bot added the community To tag external issues and PRs label Aug 22, 2024
@tippmar-nr
Copy link
Member

tippmar-nr commented Aug 22, 2024

Thanks @cpr4377 for your bug report.

I created a simple test app targeting .NET 8 (using the default Web API app from Visual Studio), added your middleware code and ran some tests. The NewRelic trace id was generated for each request:

image

How are you adding the middleware to the asp.net core request pipeline? The .NET agent injects our middleware at the very beginning of the request pipeline, so if you're doing anything other than just calling app.UseMiddleware<TraceIdMiddleware>(), it's possible you're interfering with the agent's middleware injection.

I'm going to close this bug since I can't reproduce it, but if you have further information that would help reproduce it, feel free to re-open and provide a repro app.

Here's my repro app (not including the bits generated by Visual Studio for the Web API sample app):

{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllers();
            var app = builder.Build();

            // Configure the HTTP request pipeline.

            app.UseAuthorization();
            app.UseMiddleware<TraceIdMiddleware>();

            app.MapControllers();

            app.Run();
        }
    }

    public sealed class TraceIdMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger<TraceIdMiddleware> _logger;

        public TraceIdMiddleware(RequestDelegate next, ILogger<TraceIdMiddleware> logger)
        {
            _next = next;
            _logger = logger;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            if (!context.Request.Headers.ContainsKey("X-Trace-Id"))
            {
                var tracer = NewRelic.Api.Agent.NewRelic.GetAgent();
                var traceId = tracer.TraceMetadata.TraceId;

                _logger.LogWarning("NewRelic Trace Id " + traceId);
                _logger.LogWarning("Http context trace Id " + context.TraceIdentifier);

                if (!string.IsNullOrEmpty(traceId))
                {
                    context.Response.Headers.Append("X-Trace-Id", traceId);
                }
            }
            await _next(context);
        }
    }
}

@tippmar-nr tippmar-nr closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community To tag external issues and PRs
Projects
None yet
Development

No branches or pull requests

2 participants