Skip to content

Commit

Permalink
Merge pull request #111 from DataDog/feature/config-retries
Browse files Browse the repository at this point in the history
Allow retries to be configurable
  • Loading branch information
jszwedko authored May 9, 2024
2 parents 1cf5910 + aebb543 commit fc66603
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ internal static class ApplyMicrosoftExtensionsConfiguration
/// Create the DatadogConfiguration object or apply any configuration changes to it.
/// </summary>
/// <param name="datadogConfiguration">An optional externally-created DatadogConfiguration object to be updated with additional configuration values.</param>
/// <param name="config">A configuration section typically named "configurationSection".</param>
/// <param name="configurationSection">A configuration section typically named "configurationSection".</param>
/// <returns>The "merged" DatadogConfiguration object.</returns>
internal static DatadogConfiguration ConfigureDatadogConfiguration(DatadogConfiguration datadogConfiguration, IConfigurationSection configurationOption)
internal static DatadogConfiguration ConfigureDatadogConfiguration(DatadogConfiguration datadogConfiguration, IConfigurationSection configurationSection)
{
if (configurationOption == null || !configurationOption.GetChildren().Any()) return datadogConfiguration ?? new DatadogConfiguration();
if (configurationSection == null || !configurationSection.GetChildren().Any()) return datadogConfiguration ?? new DatadogConfiguration();

var section = configurationOption.Get<DatadogConfiguration>();
var section = configurationSection.Get<DatadogConfiguration>();

return new DatadogConfiguration(
url: datadogConfiguration?.Url ?? section.Url,
port: datadogConfiguration?.Port ?? section.Port,
useSSL: datadogConfiguration?.UseSSL ?? section.UseSSL,
useTCP: datadogConfiguration?.UseTCP ?? section.UseTCP
useTCP: datadogConfiguration?.UseTCP ?? section.UseTCP,
maxRetries:datadogConfiguration?.MaxRetries ?? section.MaxRetries
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ public class DatadogConfiguration
/// </summary>
public bool UseTCP { get; set; }

/// <summary>
/// Number of retries before the client gives up logging.
/// </summary>
public int MaxRetries { get; set; }

public DatadogConfiguration() : this(DDUrl, DDPort, true, false) {
}

public DatadogConfiguration(string url = DDUrl, int port = DDPort, bool useSSL = true, bool useTCP = false)
public DatadogConfiguration(string url = DDUrl, int port = DDPort, bool useSSL = true, bool useTCP = false, int maxRetries = 10)
{
Url = url;
Port = port;
UseSSL = useSSL;
UseTCP = useTCP;
MaxRetries = maxRetries;
}

public override string ToString()
{
return string.Format("{{ Url: {0}, Port: {1}, UseSSL: {2}, UseTCP: {3} }}", Url, Port, UseSSL, UseTCP);
}
public override string ToString() => $"{{ Url: {Url}, Port: {Port}, UseSSL: {UseSSL}, UseTCP: {UseTCP} }}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ public class DatadogHttpClient : IDatadogClient
private readonly string _url;
private readonly DatadogLogRenderer _renderer;
private readonly HttpClient _client;

/// <summary>
/// Max number of retries when sending failed.
/// </summary>
private const int MaxRetries = 10;
private readonly int _maxRetries;

/// <summary>
/// Max backoff used when sending failed.
/// </summary>
private const int MaxBackoff = 30;

public DatadogHttpClient(string url, DatadogLogRenderer renderer, HttpClient client)
public DatadogHttpClient(string url, DatadogLogRenderer renderer, HttpClient client, int maxRetries)
{
_url = url;
_renderer = renderer;
_client = client;
_maxRetries = maxRetries;
}

public Task WriteAsync(IEnumerable<LogEvent> events)
Expand Down Expand Up @@ -79,7 +76,7 @@ private async Task Post(JsonPayloadBuilder payloadBuilder)
var content = new StringContent(payload, Encoding.UTF8, _content);
HttpResponseMessage lastResult = null;
Exception lastException = null;
for (int retry = 0; retry < MaxRetries; retry++)
for (int retry = 0; retry < _maxRetries; retry++)
{
int backoff = (int)Math.Min(Math.Pow(2, retry), MaxBackoff);
if (retry > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static IDatadogClient CreateDatadogClient(string apiKey, DatadogLogRende
else
{
var httpIntakeClient = new DatadogHttpIntakeClient(apiKey);
return new DatadogHttpClient($"{configuration.Url}/api/v2/logs", renderer, httpIntakeClient);
return new DatadogHttpClient($"{configuration.Url}/api/v2/logs", renderer, httpIntakeClient, configuration.MaxRetries);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class DatadogTcpClient : IDatadogClient
/// </summary>
private const string MessageDelimiter = "\n";

/// <summary>
/// Max number of retries when sending failed.
/// </summary>
private const int MaxRetries = 5;

/// <summary>
/// Max backoff used when sending failed.
/// </summary>
Expand Down Expand Up @@ -102,7 +97,7 @@ public async Task WriteAsync(IEnumerable<LogEvent> events)
}
string payload = payloadBuilder.ToString();
var dataSent = false;
for (int retry = 0; retry < MaxRetries; retry++)
for (int retry = 0; retry < _config.MaxRetries; retry++)
{
int backoff = (int)Math.Min(Math.Pow(retry, 2), MaxBackoff);
if (retry > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019 Datadog, Inc.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog.Events;
Expand Down

0 comments on commit fc66603

Please sign in to comment.