cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Logging Sentry exceptions to Dyntrace

bsheehy
Newcomer

We currently log exceptions using Sentry (which we like very much) and an ILogger interface in .Net Core. We also have Dynatrace and would like these exceptions to be available within Dynatrace and be able to track\trace related api calls etc..

These exceptions are data rich exceptions which include:

  • stack traces
  • custom data
  • front end console log dumps
  • Sentry also includes http headers and lots of other data including injected distributed tracing ids, info about hosts and other data similar to what Dynatrace would track

 

Im confused on what’s the best way to get these exception into Dynatrace and have a few queries:

  1. What’s the best way to log these exceptions and related info into Dynatrace?
  2. Is it possible in our ILogger interface implementation to log these exceptions directly to Dynatrace?
    1. Do we log to OneAgent or directly to Dynatrace?
    2. Is this fire and forget\queued?

Any help or best practice for this would be very helpful.

1 REPLY 1

pawel_harbuz
Participant

1. Data as exception stacktraces and headers/custom info you can log using OneAgent SDK for .NET (https://github.com/Dynatrace/OneAgent-SDK-for-dotnet). It requires manual instrumentation. Depending on type of data you can ingest the mentioned data as logs and corelate it with traces or report as errors.

2.In this case in my opinion is to create a new class that inherits from your Logger implementation that overrides your logger methods and add required headers using OneAgent SDK for .NET. In this case you can log your custom error and attributes which will be corelated with trace.

namespace LicenseManager.WebApi;

public interface ILogger
{
    void LogError(string message);
    void LogInformation(string message);
}

public class Logger : ILogger
{
    public void LogError(string message)
    {
        Console.WriteLine($"Error: {message}");
    }

    public void LogInformation(string message)
    {
        Console.WriteLine($"Info: {message}");
    }
}

public class MyLogger : Logger
{
    public new void LogError(string message)
    {
        IOneAgentSdk oneAgentSdk = OneAgentSdkFactory.CreateInstance();
        ITraceContextInfo traceContextInfo = oneAgentSdk.TraceContextInfo;
        string traceId = traceContextInfo.TraceId;
        string spanId = traceContextInfo.SpanId;
        string yourCustomError = message.GetError();
        dbTracer.Error(yourCustomError);
        oneAgentSdk.AddCustomRequestAttribute("region", "EMEA");
        oneAgentSdk.AddCustomRequestAttribute("salesAmount", 2500);
        oneAgentSdk.AddCustomRequestAttribute("service-quality", 0.707106);
        base.LogError($"[!dt dt.trace_id={traceId},dt.span_id={spanId}] {message}");
    }

    public new void LogInformation(string message)
    {
        IOneAgentSdk oneAgentSdk = OneAgentSdkFactory.CreateInstance();
        ITraceContextInfo traceContextInfo = oneAgentSdk.TraceContextInfo;
        string traceId = traceContextInfo.TraceId;
        string spanId = traceContextInfo.SpanId;
        base.LogInformation($"[!dt dt.trace_id={traceId},dt.span_id={spanId}] {message}");
    }
}

Of course you can also add that headers inside of your logger implementation.

Basically, logs are collected by OneAgent and forwarded by it to your Dynatrace tenant/cluster, but it is not corelated with traces. The information mentioned above allows you to corelate traces with logs.

Featured Posts