From 69f3c68f964f25e10a7dae1e7546f9cd180d4b0f Mon Sep 17 00:00:00 2001 From: Lucas Pimentel Date: Mon, 3 Dec 2018 18:12:16 -0500 Subject: [PATCH] set service name from DD_SERVICE_NAME env var (#226) * add support for `DD_SERVICE_NAME` environment variable * add comments * add unit test * add more unit test cases --- src/Datadog.Trace/Tracer.cs | 16 +++++++++++ test/Datadog.Trace.Tests/TracerTests.cs | 35 ++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Datadog.Trace/Tracer.cs b/src/Datadog.Trace/Tracer.cs index f2a87d3f9f9b..2634ca63f660 100644 --- a/src/Datadog.Trace/Tracer.cs +++ b/src/Datadog.Trace/Tracer.cs @@ -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 = { @@ -195,11 +196,25 @@ internal static Uri CreateAgentUri() return new Uri($"http://{host}:{port}"); } + /// + /// 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. + /// + /// The default service name. 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". @@ -207,6 +222,7 @@ private static string CreateDefaultServiceName() return (System.Web.Hosting.HostingEnvironment.SiteName + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath).TrimEnd('/'); } #endif + return Assembly.GetEntryAssembly()?.GetName().Name ?? Process.GetCurrentProcess().ProcessName; } diff --git a/test/Datadog.Trace.Tests/TracerTests.cs b/test/Datadog.Trace.Tests/TracerTests.cs index 20ad65357fa3..527a9a6dc373 100644 --- a/test/Datadog.Trace.Tests/TracerTests.cs +++ b/test/Datadog.Trace.Tests/TracerTests.cs @@ -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 @@ -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); + } } }