Skip to content

Add ILogger.ErrorSkipTelemetry() to exclude certain logs from telemetry #7028

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

Merged
merged 6 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
}

// is it an IDatadogLogger logging method?
if (method.Name is not "Debug" and not "Information" and not "Warning" and not "Error")
if (method.Name is not "Debug" and not "Information" and not "Warning" and not "Error" and not "ErrorSkipTelemetry")
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tracer/src/Datadog.Trace/Agent/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private async Task<bool> SendWithRetry<T>(Uri endpoint, SendCallback<T> callback
if (isFinalTry || success == SendResult.Failed_DontRetry)
{
// stop retrying
_log.Error(exception, FailedToSendMessageTemplate, _apiRequestFactory.Info(endpoint));
_log.ErrorSkipTelemetry(exception, FailedToSendMessageTemplate, _apiRequestFactory.Info(endpoint));
return false;
}
else if (_log.IsEnabled(LogEventLevel.Debug))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ protected override void Setup(IGitInfo gitInfo)
{
if (string.IsNullOrEmpty(defaultValue))
{
Log.Error("DD_GIT_REPOSITORY_URL is set with an empty value, and the Git repository could not be automatically extracted");
Log.ErrorSkipTelemetry("DD_GIT_REPOSITORY_URL is set with an empty value, and the Git repository could not be automatically extracted");
}
else
{
Log.Error("DD_GIT_REPOSITORY_URL is set with an empty value, defaulting to '{Default}'", defaultValue);
Log.ErrorSkipTelemetry("DD_GIT_REPOSITORY_URL is set with an empty value, defaulting to '{Default}'", defaultValue);
}

return false;
Expand All @@ -195,11 +195,11 @@ protected override void Setup(IGitInfo gitInfo)
{
if (string.IsNullOrEmpty(defaultValue))
{
Log.Error("DD_GIT_REPOSITORY_URL is set with an invalid value ('{Value}'), and the Git repository could not be automatically extracted", value);
Log.ErrorSkipTelemetry("DD_GIT_REPOSITORY_URL is set with an invalid value ('{Value}'), and the Git repository could not be automatically extracted", value);
}
else
{
Log.Error("DD_GIT_REPOSITORY_URL is set with an invalid value ('{Value}'), defaulting to '{Default}'", value, defaultValue);
Log.ErrorSkipTelemetry("DD_GIT_REPOSITORY_URL is set with an invalid value ('{Value}'), defaulting to '{Default}'", value, defaultValue);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using Datadog.Trace.Util;
using Datadog.Trace.Vendors.Serilog;
using Datadog.Trace.Vendors.Serilog.Core;
using Datadog.Trace.Vendors.Serilog.Events;
using Datadog.Trace.Vendors.Serilog.Filters;

namespace Datadog.Trace.Logging;

Expand Down Expand Up @@ -96,7 +98,7 @@ static bool Contains(string?[]? array, string toMatch)
.WriteTo.Logger(
lc => lc
.MinimumLevel.Error()
.Filter.ByExcluding(log => IsExcludedMessage(log.MessageTemplate.Text))
.Filter.ByExcluding(Matching.WithProperty(DatadogSerilogLogger.SkipTelemetryProperty))
.WriteTo.Sink(new RedactedErrorLogSink(telemetry.Collector)));
}

Expand All @@ -105,6 +107,7 @@ static bool Contains(string?[]? array, string toMatch)
var managedLogPath = Path.Combine(fileConfig.LogDirectory, $"dotnet-tracer-managed-{domainMetadata.ProcessName}-{domainMetadata.ProcessId.ToString(CultureInfo.InvariantCulture)}.log");

loggerConfiguration
.Enrich.With(new RemovePropertyEnricher(LogEventLevel.Error, DatadogSerilogLogger.SkipTelemetryProperty))
.WriteTo.File(
managedLogPath,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Exception} {Properties}{NewLine}",
Expand Down Expand Up @@ -147,13 +150,6 @@ static bool Contains(string?[]? array, string toMatch)
return new DatadogSerilogLogger(internalLogger, rateLimiter, config.File?.LogDirectory);
}

private static bool IsExcludedMessage(string messageTemplateText)
=> ReferenceEquals(messageTemplateText, Api.FailedToSendMessageTemplate)
#if NETFRAMEWORK
|| ReferenceEquals(messageTemplateText, PerformanceCountersListener.InsufficientPermissionsMessageTemplate)
#endif
;

// Internal for testing
internal static string GetLogDirectory(IConfigurationTelemetry telemetry)
=> GetLogDirectory(GlobalConfigurationSource.CreateDefaultConfigurationSource(), telemetry);
Expand Down Expand Up @@ -276,4 +272,18 @@ private static string GetDefaultLogDirectory(IConfigurationSource source, IConfi
// If telemetry is disabled
return null;
}

private class RemovePropertyEnricher(LogEventLevel minLevel, string propertyName) : ILogEventEnricher
{
private readonly LogEventLevel _minLevel = minLevel;
private readonly string _propertyName = propertyName;

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent.Level >= _minLevel)
{
logEvent.RemovePropertyIfPresent(_propertyName);
}
}
}
}
Loading
Loading