Skip to content
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

add support for DD_AGENT_HOST environment variable #208

Merged
merged 7 commits into from
Nov 13, 2018
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
63 changes: 44 additions & 19 deletions src/Datadog.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,39 @@ namespace Datadog.Trace
public class Tracer : IDatadogTracer
{
private const string UnknownServiceName = "UnknownService";
private static readonly ILog _log = LogProvider.For<Tracer>();
private static readonly string _defaultTraceAgentHost = "localhost";
private static readonly string _defaultTraceAgentPort = "8126";
private const string DefaultTraceAgentHost = "localhost";
private const string DefaultTraceAgentPort = "8126";

private AsyncLocalScopeManager _scopeManager;
private IAgentWriter _agentWriter;
private bool _isDebugEnabled;
private static readonly string[] TraceAgentHostEnvironmentVariableNames =
{
// officially documented name
"DD_AGENT_HOST",
// backwards compatibility for names used in the past
"DD_TRACE_AGENT_HOSTNAME",
"DATADOG_TRACE_AGENT_HOSTNAME"
};

private static readonly string[] TraceAgentPortEnvironmentVariableNames =
{
// officially documented name
"DD_TRACE_AGENT_PORT",
// backwards compatibility for names used in the past
"DATADOG_TRACE_AGENT_PORT"
};

private static readonly ILog Log = LogProvider.For<Tracer>();
private static readonly Uri DefaultAgentUri;

private readonly AsyncLocalScopeManager _scopeManager;
private readonly IAgentWriter _agentWriter;
private readonly bool _isDebugEnabled;

static Tracer()
{
// create Agent uri once and save it
DefaultAgentUri = CreateAgentUri();

// create the default global Tracer
Instance = Create();
}

Expand Down Expand Up @@ -76,11 +99,11 @@ internal Tracer(IAgentWriter agentWriter, string defaultServiceName = null, bool
/// <returns>The newly created tracer</returns>
public static Tracer Create(Uri agentEndpoint = null, string defaultServiceName = null, bool isDebugEnabled = false)
{
return Create(agentEndpoint ?? DefaultAgentUri(), defaultServiceName, null, isDebugEnabled);
return Create(agentEndpoint ?? DefaultAgentUri, defaultServiceName, null, isDebugEnabled);
}

/// <summary>
/// Make a span active and return a scope that can be disposed to desactivate the span
/// Make a span active and return a scope that can be disposed to close the span
/// </summary>
/// <param name="span">The span to activate</param>
/// <param name="finishOnClose">If set to false, closing the returned scope will not close the enclosed span </param>
Expand Down Expand Up @@ -144,18 +167,20 @@ internal static Tracer Create(Uri agentEndpoint, string serviceName, DelegatingH
return tracer;
}

private static Uri DefaultAgentUri()
/// <summary>
/// Create an Uri to the Agent using host and port from
/// environment variables or defaults if not set.
/// </summary>
/// <returns>An Uri that can be used to send traces to the Agent.</returns>
internal static Uri CreateAgentUri()
{
var prefixes = new string[] { "DD", "DATADOG" };
var host = TraceAgentHostEnvironmentVariableNames.Select(Environment.GetEnvironmentVariable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice Linq usage 👍

.FirstOrDefault(str => !string.IsNullOrEmpty(str))
?.Trim() ?? DefaultTraceAgentHost;

var host = prefixes.
Select(prefix => Environment.GetEnvironmentVariable($"{prefix}_TRACE_AGENT_HOSTNAME")).
Where(str => !string.IsNullOrEmpty(str)).
FirstOrDefault() ?? _defaultTraceAgentHost;
var port = prefixes.
Select(prefix => Environment.GetEnvironmentVariable($"{prefix}_TRACE_AGENT_PORT")).
Where(str => !string.IsNullOrEmpty(str)).
FirstOrDefault() ?? _defaultTraceAgentPort;
var port = TraceAgentPortEnvironmentVariableNames.Select(Environment.GetEnvironmentVariable)
.FirstOrDefault(str => !string.IsNullOrEmpty(str))
?.Trim() ?? DefaultTraceAgentPort;

return new Uri($"http://{host}:{port}");
}
Expand All @@ -177,7 +202,7 @@ private static string CreateDefaultServiceName()
}
catch (Exception ex)
{
_log.ErrorException("Error creating default service name.", ex);
Log.ErrorException("Error creating default service name.", ex);
return null;
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/Datadog.Trace.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,26 @@ public async Task StartActive_AsyncChildrenCreation_ChildrenParentProperlySet()
Assert.Equal(root.Span.Context.SpanId, span.Span.Context.ParentId);
}
}

[Theory]
[InlineData("ddagent", "5000", "http://ddagent:5000")]
[InlineData("", "", "http://localhost:8126")]
[InlineData(null, null, "http://localhost:8126")]
public void SetHostAndPortEnvironmentVariables(string host, string port, string expectedUri)
{
string originalHost = Environment.GetEnvironmentVariable("DD_AGENT_HOST");
string originalPort = Environment.GetEnvironmentVariable("DD_TRACE_AGENT_PORT");

Environment.SetEnvironmentVariable("DD_AGENT_HOST", host);
Environment.SetEnvironmentVariable("DD_TRACE_AGENT_PORT", port);

Uri uri = Tracer.CreateAgentUri();

Assert.Equal(new Uri(expectedUri), uri);

// reset the environment variables to their original values (if any) when done
Environment.SetEnvironmentVariable("DD_AGENT_HOST", originalHost);
Environment.SetEnvironmentVariable("DD_TRACE_AGENT_PORT", originalPort);
}
}
}