Skip to content

Commit

Permalink
set "env" tag from DD_DEV environment variable (#225)
Browse files Browse the repository at this point in the history
* add support for `DD_ENV` environment variable

* define constant `Tags.Env`

* add unit test

* fix comment

* fix comment
  • Loading branch information
lucaspimentel authored Dec 3, 2018
1 parent 0e67324 commit 2288666
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Datadog.Trace/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace Datadog.Trace
/// </summary>
public static class Tags
{
/// <summary>
/// The environment of the profiled service.
/// </summary>
public const string Env = "env";

/// <summary>
/// The URL of an HTTP request
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Datadog.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Tracer : IDatadogTracer
private const string UnknownServiceName = "UnknownService";
private const string DefaultTraceAgentHost = "localhost";
private const string DefaultTraceAgentPort = "8126";
private const string EnvVariableName = "DD_ENV";

private static readonly string[] TraceAgentHostEnvironmentVariableNames =
{
Expand Down Expand Up @@ -146,6 +147,15 @@ public Span StartSpan(string operationName, SpanContext childOf = null, string s
}

var span = new Span(this, childOf, operationName, serviceName, startTime);

var env = Environment.GetEnvironmentVariable(EnvVariableName);

// automatically add the "env" tag if environment variable is defined
if (!string.IsNullOrWhiteSpace(env))
{
span.SetTag(Tags.Env, env);
}

span.TraceContext.AddSpan(span);
return span;
}
Expand Down
17 changes: 17 additions & 0 deletions test/Datadog.Trace.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,22 @@ public void SetHostAndPortEnvironmentVariables(string host, string port, string
Environment.SetEnvironmentVariable("DD_AGENT_HOST", originalHost);
Environment.SetEnvironmentVariable("DD_TRACE_AGENT_PORT", originalPort);
}

[Theory]
[InlineData(null)]
[InlineData("test")]
public void SetEnvEnvironmentVariable(string env)
{
var name = "DD_ENV";
string originalEnv = Environment.GetEnvironmentVariable(name);

Environment.SetEnvironmentVariable(name, env);
Span span = Tracer.Instance.StartSpan("operation");

Assert.Equal(env, span.GetTag(Tags.Env));

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

0 comments on commit 2288666

Please sign in to comment.