skip to Main Content

When the ASP.NET application is starting up, some logs are sent and they are visible in NewRelic. But when the application handles an http request, the logs sent through ILogger<T> never appear in NewRelic.

I have the OTEL_DIAGNOSTICS.json file, but no errors are written. ConsoleExporter works flawlessly. I can see traces normally. In NewRelic nothing appears in NrIntegrationError collection.

What am I doing wrong? It seems NewRelic only shows logs that are not correlated with traces and spans.

builder.Services.AddOpenTelemetry()
    .ConfigureResource(builder => builder.AddService(Otel.ServiceName, serviceVersion: Otel.ServiceVersion))
    .WithLogging(logging => logging
        .AddConsoleExporter()
        .AddOtlpExporter(otlp =>
        {
            otlp.Endpoint = new Uri($"{_otelEndpoint}/v1/logs");
            otlp.Headers = $"api-key={_otelApiKey}";
            otlp.Protocol = _otelProtocol; // Using Http/Protobuf
        }), options =>
        {
            options.IncludeFormattedMessage = true;
            options.IncludeScopes = true;
            options.ParseStateValues = true;
        })
    .WithTracing(tracing => tracing
        .AddSource(Otel.ServiceName)
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddRedisInstrumentation()
        .AddAWSInstrumentation()
        .AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri($"{_otelEndpoint}/v1/traces");
            options.Headers = $"api-key={_otelApiKey}";
            options.Protocol = _otelProtocol;
        }))
    .WithMetrics(metrics => metrics
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri($"{_otelEndpoint}/v1/metrics");
            options.Headers = $"api-key={_otelApiKey}";
            options.Protocol = _otelProtocol;
        }));

2

Answers


  1. It seems like your issue may be related to the correlation between logs and traces in NewRelic. Here are a few things to check:

    Context Correlation: Ensure that your OpenTelemetry setup for logs (AddOtlpExporter) includes proper context propagation so that logs are correlated with trace IDs.

    Log Level Settings: Make sure the log levels aren’t being filtered out. Set the log level to Debug or Information to verify if logs are appearing.

    Endpoint and Protocol: Double-check the OTLP endpoint (_otelEndpoint/v1/logs), headers (api-key), and protocol settings to make sure they’re correct and compatible with NewRelic.

    Logs in Context: Use NewRelic’s "Logs in Context" feature to help correlate logs with traces. Consider adding the NewRelic.LogEnrichers package for automatic enrichment.

    Logging Processor: Make sure you’re using a BatchLogProcessor to manage log exports, and that Activity.Current is properly propagated to maintain context.

    Login or Signup to reply.
  2. It sounds like you’re experiencing an issue where logs are successfully sent to New Relic during application startup but fail to appear when handling HTTP requests. This discrepancy typically points to differences in how logging is handled in these two phases, possibly related to log correlation with traces and spans.

    Here are several steps and considerations to help you identify and resolve the issue:

    1- Verify Log Correlation Attributes

    Issue: During HTTP request handling, logs are often correlated with traces and spans. If these logs lack the necessary correlation attributes, New Relic might not display them as expected.

    Solution:

    • Check Trace and Span IDs: Ensure that the logs emitted during HTTP requests include the trace_id and span_id. You can verify this by inspecting the logs in the ConsoleExporter. They should contain these IDs when correlated with a trace.
    // Example log with trace context
    logger.LogInformation("Handling request {RequestPath}", context.Request.Path);
    
    
    • Resource Configuration: Make sure that the Resource is correctly configured to include necessary attributes that New Relic expects for correlation.
    .ConfigureResource(resourceBuilder => resourceBuilder
        .AddService(Otel.ServiceName, serviceVersion: Otel.ServiceVersion)
        // Add additional attributes if required by New Relic
    )
    

    2- Ensure Proper OpenTelemetry Pipeline Configuration

    Issue: Misconfigurations in the OpenTelemetry logging pipeline can prevent logs from being exported correctly during HTTP requests.

    Solution:

    • Separate Pipelines:
      Although youre using a single AddOpenTelemetry setup, consider separating logging, tracing, and metrics pipelines to isolate and identify issues more effectively.
    builder.Services.AddOpenTelemetryLogging(logging => {
        logging
            .AddConsoleExporter()
            .AddOtlpExporter(otlp => {
                otlp.Endpoint = new Uri($"{_otelEndpoint}/v1/logs");
                otlp.Headers = $"api-key={_otelApiKey}";
                otlp.Protocol = _otelProtocol;
            })
            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(Otel.ServiceName));
    });
    
    builder.Services.AddOpenTelemetryTracing(tracing => {
        tracing
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            // Add other instrumentation as needed
            .AddOtlpExporter(otlp => {
                otlp.Endpoint = new Uri($"{_otelEndpoint}/v1/traces");
                otlp.Headers = $"api-key={_otelApiKey}";
                otlp.Protocol = _otelProtocol;
            })
            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(Otel.ServiceName));
    });
    
    builder.Services.AddOpenTelemetryMetrics(metrics => {
        metrics
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            // Add other instrumentation as needed
            .AddOtlpExporter(otlp => {
                otlp.Endpoint = new Uri($"{_otelEndpoint}/v1/metrics");
                otlp.Headers = $"api-key={_otelApiKey}";
                otlp.Protocol = _otelProtocol;
            })
            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(Otel.ServiceName));
    });
    
    
    • Flush the Exporters: Ensure that the exporters are properly flushed on application shutdown to prevent loss of logs.
    var app = builder.Build();
    app.Lifetime.ApplicationStopping.Register(() => {
        app.Services.GetRequiredService<TracerProvider>().Dispose();
        app.Services.GetRequiredService<LoggerProvider>().Dispose();
        app.Services.GetRequiredService<MeterProvider>().Dispose();
    });
    
    
    • Consult Documentation: Review New Relic’s OpenTelemetry Logs Documentation to ensure that your logs comply with their ingestion requirements.

    • Required Attributes: Make sure that logs include attributes like service.name, trace_id, and span_id, which are crucial for correlation.

    4 – Validate Exporter Configuration

    Issue: Incorrect OTLP exporter settings can prevent logs from being sent correctly to New Relic.

    Solution:

    • Endpoint Verification: Double-check that the OTLP log endpoint (${_otelEndpoint}/v1/logs) is correct and accessible from your application environment.

    • Headers and Authentication: Ensure that the api-key and any other required headers are correctly set. Incorrect headers can result in silent failures where logs are not ingested.

    otlp.Headers = $"api-key={_otelApiKey}";
    
    
    • Protocol Compatibility: Confirm that the protocol (Http/Protobuf) is supported and correctly configured on both the exporter and New Relic’s side.

    5 – Increase Diagnostic Logging

    Issue: Silent failures can make it challenging to identify why logs aren’t appearing in New Relic.

    Solution:

    - **Enable OpenTelemetry Diagnostics:** Enhance the verbosity of OpenTelemetry diagnostics to capture more detailed information about the logging pipeline.
    
    // OTEL_DIAGNOSTICS.json
    {
        "Logging": {
            "LogLevel": {
                "Default": "Debug",
                "OpenTelemetry": "Debug"
            }
        }
    }
    
    
    • Review Logs: After increasing verbosity, inspect the diagnostic logs to identify any issues or anomalies when handling HTTP requests.

    6 – Test with OpenTelemetry Collector

    Issue: To isolate whether the issue is with your application or New Relic, you can use an intermediary like the OpenTelemetry Collector.

    Solution:

    • Set Up Collector: Configure the OpenTelemetry Collector to receive logs from your application and then forward them to New Relic. This can help determine if logs are correctly exported from your application.
    # Example Collector configuration
    receivers:
      otlp:
        protocols:
          http:
          grpc:
    
    exporters:
      newrelic:
        api_key: YOUR_NEW_RELIC_API_KEY
        endpoint: https://log-api.newrelic.com/log/v1
    
    service:
      pipelines:
        logs:
          receivers: [otlp]
          exporters: [newrelic]
    
    
    • Monitor Collector Logs: Check the Collector’s logs to ensure that logs are being received and forwarded without issues.

    7 – Update OpenTelemetry and New Relic SDKs

    Issue: Incompatibilities or bugs in older versions of SDKs can lead to unexpected behavior.

    Solution:

    • Upgrade Packages: Ensure that you are using the latest versions of OpenTelemetry and New Relic SDKs. Updates often include bug fixes and improvements.
    dotnet add package OpenTelemetry
    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
    dotnet add package NewRelic.OpenTelemetry
    
    
    8 - Contact New Relic Support
    
    Issue: If after all these steps the issue persists, it might be specific to your New Relic account or a deeper integration issue.
    
    Solution:
    
     - **Provide Detailed Information:** Reach out to New Relic Support with detailed information about your setup, configuration files, and any diagnostic logs you've collected.
     - **Share Samples:** If possible, share sample logs that are not appearing in New Relic to help them diagnose the issue more effectively.
    

    Summary

    Your configuration appears mostly correct, but the issue likely revolves around how logs are correlated with traces during HTTP request handling. By verifying that logs include necessary correlation attributes, ensuring compatibility with New Relic’s requirements, and isolating the problem through diagnostic logging and possibly the OpenTelemetry Collector, you should be able to identify and resolve why logs aren’t appearing in New Relic during request handling.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search