Skip to content

feat(trace-exporter): handle exceptions when creating Trace Exporter in TracerManager #6925

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
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
20 changes: 15 additions & 5 deletions tracer/src/Datadog.Trace/LibDatadog/TraceExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#nullable enable

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Datadog.Trace.Agent;
Expand Down Expand Up @@ -47,12 +48,21 @@ public Task<bool> SendTracesAsync(ArraySegment<byte> traces, int numberOfTraces,
};

var responsePtr = IntPtr.Zero;
using var error = NativeInterop.Exporter.Send(this, tracesSlice, (UIntPtr)numberOfTraces, ref responsePtr);
if (!error.IsInvalid)
try
{
var ex = error.ToException();
_log.Error(ex, "An error occurred while sending data to the agent. Error Code: {ErrorCode}, message: {Message}", ex.ErrorCode, ex.Message);
throw ex;
using var error = NativeInterop.Exporter.Send(this, tracesSlice, (UIntPtr)numberOfTraces, ref responsePtr);
if (!error.IsInvalid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find this so confusing 😅 error.IsInvalid means there wasn't an error?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

negation of negation, yes there wasn't any error
else our test will blow.

{
var ex = error.ToException();
#pragma warning disable DDLOG004
_log.Error(ex, "An error occurred while sending data to the agent. Error Code: " + ex.ErrorCode + ", message: {Message}", ex.Message);
#pragma warning restore DDLOG004
throw ex;
}
}
catch (Exception ex) when (ex is not TraceExporterException)
{
_log.Error(ex, "An error occurred while sending data to the agent.");
}
}
}
Expand Down
71 changes: 39 additions & 32 deletions tracer/src/Datadog.Trace/TracerManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,43 +363,50 @@ private IApi GetApi(TracerSettings settings, IDogStatsd statsd, Action<Dictionar
{
if (settings.DataPipelineEnabled)
{
var telemetrySettings = TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, TelemetryFactory.Config, settings, isAgentAvailable: null);
TelemetryClientConfiguration? telemetryClientConfiguration = null;

// We don't know how to handle telemetry in Agentless mode yet
// so we disable telemetry in this case
if (telemetrySettings.TelemetryEnabled && telemetrySettings.Agentless == null)
try
{
telemetryClientConfiguration = new TelemetryClientConfiguration
var telemetrySettings = TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, TelemetryFactory.Config, settings, isAgentAvailable: null);
TelemetryClientConfiguration? telemetryClientConfiguration = null;

// We don't know how to handle telemetry in Agentless mode yet
// so we disable telemetry in this case
if (telemetrySettings.TelemetryEnabled && telemetrySettings.Agentless == null)
{
Interval = (ulong)telemetrySettings.HeartbeatInterval.Milliseconds,
RuntimeId = new CharSlice(Tracer.RuntimeId),
DebugEnabled = telemetrySettings.DebugEnabled
};
}
telemetryClientConfiguration = new TelemetryClientConfiguration
{
Interval = (ulong)telemetrySettings.HeartbeatInterval.Milliseconds,
RuntimeId = new CharSlice(Tracer.RuntimeId),
DebugEnabled = telemetrySettings.DebugEnabled
};
}

// When APM is disabled, we don't want to compute stats at all
// A common use case is in Application Security Monitoring (ASM) scenarios:
// when APM is disabled but ASM is enabled.
var clientComputedStats = !settings.StatsComputationEnabled && !settings.ApmTracingEnabled;
// When APM is disabled, we don't want to compute stats at all
// A common use case is in Application Security Monitoring (ASM) scenarios:
// when APM is disabled but ASM is enabled.
var clientComputedStats = !settings.StatsComputationEnabled && !settings.ApmTracingEnabled;

using var configuration = new TraceExporterConfiguration
{
Url = GetUrl(settings),
TraceVersion = TracerConstants.AssemblyVersion,
Env = settings.Environment,
Version = settings.ServiceVersion,
Service = settings.ServiceName,
Hostname = HostMetadata.Instance.Hostname,
Language = ".NET",
LanguageVersion = FrameworkDescription.Instance.ProductVersion,
LanguageInterpreter = FrameworkDescription.Instance.Name,
ComputeStats = settings.StatsComputationEnabled,
TelemetryClientConfiguration = telemetryClientConfiguration,
ClientComputedStats = clientComputedStats
};
using var configuration = new TraceExporterConfiguration
{
Url = GetUrl(settings),
TraceVersion = TracerConstants.AssemblyVersion,
Env = settings.Environment,
Version = settings.ServiceVersion,
Service = settings.ServiceName,
Hostname = HostMetadata.Instance.Hostname,
Language = ".NET",
LanguageVersion = FrameworkDescription.Instance.ProductVersion,
LanguageInterpreter = FrameworkDescription.Instance.Name,
ComputeStats = settings.StatsComputationEnabled,
TelemetryClientConfiguration = telemetryClientConfiguration,
ClientComputedStats = clientComputedStats
};

return new TraceExporter(configuration);
return new TraceExporter(configuration);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to create native Trace Exporter, falling back to managed API");
}
}

return new Api(apiRequestFactory, statsd, updateSampleRates, partialFlushEnabled);
Expand Down
Loading