Skip to content

Commit

Permalink
set service name from DD_SERVICE_NAME env var (#226)
Browse files Browse the repository at this point in the history
* add support for `DD_SERVICE_NAME` environment variable

* add comments

* add unit test

* add more unit test cases
  • Loading branch information
lucaspimentel authored Dec 3, 2018
1 parent 2288666 commit 69f3c68
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Datadog.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Tracer : IDatadogTracer
private const string DefaultTraceAgentHost = "localhost";
private const string DefaultTraceAgentPort = "8126";
private const string EnvVariableName = "DD_ENV";
private const string ServiceNameVariableName = "DD_SERVICE_NAME";

private static readonly string[] TraceAgentHostEnvironmentVariableNames =
{
Expand Down Expand Up @@ -195,18 +196,33 @@ internal static Uri CreateAgentUri()
return new Uri($"http://{host}:{port}");
}

/// <summary>
/// Determines the default service name for the executing application by looking at
/// environment variables, hosted app name (.NET Framework on IIS only), assembly name, and process name.
/// </summary>
/// <returns>The default service name.</returns>
private static string CreateDefaultServiceName()
{
try
{
// allow users to override this with an environment variable
var serviceName = Environment.GetEnvironmentVariable(ServiceNameVariableName);

if (!string.IsNullOrWhiteSpace(serviceName))
{
return serviceName;
}

#if !NETSTANDARD2_0
// System.Web.dll is only available on .NET Framework
if (System.Web.Hosting.HostingEnvironment.IsHosted)
{
// if we are hosted as an ASP.NET application, return "SiteName/ApplicationVirtualPath".
// note that ApplicationVirtualPath includes a leading slash.
return (System.Web.Hosting.HostingEnvironment.SiteName + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath).TrimEnd('/');
}
#endif

return Assembly.GetEntryAssembly()?.GetName().Name ??
Process.GetCurrentProcess().ProcessName;
}
Expand Down
35 changes: 34 additions & 1 deletion test/Datadog.Trace.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public void SetHostAndPortEnvironmentVariables(string host, string port, string
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
Expand All @@ -293,5 +292,39 @@ public void SetEnvEnvironmentVariable(string env)
// reset the environment variable to its original value (if any) when done
Environment.SetEnvironmentVariable(name, originalEnv);
}

[Theory]
// if no service name is specified, fallback to a best guess (e.g. assembly name, process name)
[InlineData(null, null, null, null)]
// if only one is set, use that one
[InlineData("envService", null, null, "envService")]
[InlineData(null, "tracerService", null, "tracerService")]
[InlineData(null, null, "spanService", "spanService")]
// if more than one is set, follow precedence: span > tracer > env > default
[InlineData(null, "tracerService", "spanService", "spanService")]
[InlineData("envService", null, "spanService", "spanService")]
[InlineData("envService", "tracerService", null, "tracerService")]
[InlineData("envService", "tracerService", "spanService", "spanService")]
public void SetServiceName(string envServiceName, string tracerServiceName, string spanServiceName, string expectedServiceName)
{
var name = "DD_SERVICE_NAME";
string originalEnv = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, envServiceName);

var tracer = new Tracer(_writerMock.Object, defaultServiceName: tracerServiceName);
Span span = tracer.StartSpan("operationName", serviceName: spanServiceName);

if (expectedServiceName == null)
{
Assert.Contains(span.ServiceName, TestRunners.ValidNames);
}
else
{
Assert.Equal(expectedServiceName, span.ServiceName);
}

// reset the environment variable to its original values (if any) when done
Environment.SetEnvironmentVariable(name, originalEnv);
}
}
}

0 comments on commit 69f3c68

Please sign in to comment.