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 DD_TRACE_GLOBAL_TAGS Configuration Option #533

Merged
merged 13 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/Datadog.Trace/Configuration/ConfigurationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public static class ConfigurationKeys
/// <seealso cref="TracerSettings.AnalyticsEnabled"/>
public const string GlobalAnalyticsEnabled = "DD_TRACE_ANALYTICS_ENABLED";

/// <summary>
/// Configuration key for a list of tags to be applied globally to spans.
/// </summary>
public const string GlobalTags = "DD_TRACE_GLOBAL_TAGS";

/// <summary>
/// Configuration key for enabling or disabling the automatic injection
/// of correlation identifiers into the logging context.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down
23 changes: 23 additions & 0 deletions src/Datadog.Trace/Configuration/TracerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;

namespace Datadog.Trace.Configuration
{
Expand Down Expand Up @@ -76,6 +77,23 @@ public TracerSettings(IConfigurationSource source)
false;

Integrations = new IntegrationSettingsCollection(source);

if (source == null)
{
GlobalTags = new Dictionary<string, string>();
}
else
{
var gTags = source.GetString(ConfigurationKeys.GlobalTags);
lucaspimentel marked this conversation as resolved.
Show resolved Hide resolved
if (gTags != null)
{
GlobalTags = JsonConvert.DeserializeObject<Dictionary<string, string>>(gTags);
lucaspimentel marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
GlobalTags = new Dictionary<string, string>();
}
}
}

/// <summary>
Expand Down Expand Up @@ -141,6 +159,11 @@ public TracerSettings(IConfigurationSource source)
/// </summary>
public IntegrationSettingsCollection Integrations { get; }

/// <summary>
/// Gets or sets the global tags, which are applied to all <see cref="Span"/>s.
/// </summary>
public Dictionary<string, string> GlobalTags { get; set; }
bobuva marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Create a <see cref="TracerSettings"/> populated from the default sources
/// returned by <see cref="CreateDefaultConfigurationSource"/>.
Expand Down
1 change: 1 addition & 0 deletions src/Datadog.Trace/Datadog.Trace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Title>Datadog APM</Title>
<Description>Manual instrumentation library for Datadog APM</Description>
<Authors>lucas.pimentel.datadog;colinhigginsdatadog;zachmontoyadd</Authors>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
lucaspimentel marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
Expand Down
10 changes: 10 additions & 0 deletions src/Datadog.Trace/Span.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using System.Text;
using Datadog.Trace.ExtensionMethods;
using Datadog.Trace.Interfaces;
Expand All @@ -27,6 +28,15 @@ internal Span(SpanContext context, DateTimeOffset? start)
ServiceName = context.ServiceName;
StartTime = start ?? Context.TraceContext.UtcNow;

// Apply any global tags
if (Tracer.Instance.Settings.GlobalTags.Count > 0)
{
foreach (var entry in Tracer.Instance.Settings.GlobalTags)
{
SetTag(entry.Key, entry.Value);
}
}

Log.Debug(
"Span started: [s_id: {0}, p_id: {1}, t_id: {2}]",
SpanId,
Expand Down
29 changes: 29 additions & 0 deletions src/Datadog.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,35 @@ public Span StartSpan(string operationName, ISpanContext parent = null, string s
return span;
}

/// <summary>
/// Adds the <paramref name="key"/>, <paramref name="value"/> pair as a global tag to be applied to all <see cref="Span"/>s.
/// </summary>
/// <remarks><paramref name="key"/> is whitespace-trimmed before being added.</remarks>
/// <param name="key">The global tag key</param>
/// <param name="value">The global tag value</param>
public void AddGlobalTag(string key, string value)
{
if (string.IsNullOrWhiteSpace(key))
{
Log.Warning("Cannot create a global tag with an empty key.");
bobuva marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Settings.GlobalTags[key.Trim()] = value;
}

/// <summary>
/// Adds each <see cref="KeyValuePair{TKey, TValue}"/> in <paramref name="tags"/> as global tags.
/// </summary>
/// <param name="tags"><see cref="KeyValuePair{TKey, TValue}"/>s to be added as global tags.</param>
public void AddGlobalTags(IEnumerable<KeyValuePair<string, string>> tags)
bobuva marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var entry in tags)
{
AddGlobalTag(entry.Key, entry.Value);
}
}

/// <summary>
/// Writes the specified <see cref="Span"/> collection to the agent writer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static IEnumerable<object[]> GetDefaultTestData()
yield return new object[] { CreateFunc(s => s.ServiceName), null };
yield return new object[] { CreateFunc(s => s.DisabledIntegrationNames.Count), 0 };
yield return new object[] { CreateFunc(s => s.LogsInjectionEnabled), false };
yield return new object[] { CreateFunc(s => s.GlobalTags.Count), 0 };
}

public static IEnumerable<object[]> GetTestData()
Expand All @@ -36,6 +37,8 @@ public static IEnumerable<object[]> GetTestData()
yield return new object[] { ConfigurationKeys.ServiceName, "web-service", CreateFunc(s => s.ServiceName), "web-service" };

yield return new object[] { ConfigurationKeys.DisabledIntegrations, "integration1;integration2", CreateFunc(s => s.DisabledIntegrationNames.Count), 2 };

yield return new object[] { ConfigurationKeys.GlobalTags, string.Empty, CreateFunc(s => s.GlobalTags.Count), 0 };
}

public static Func<TracerSettings, object> CreateFunc(Func<TracerSettings, object> settingGetter)
Expand Down
4 changes: 4 additions & 0 deletions test/Datadog.Trace.Tests/Datadog.Trace.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
Expand Down
64 changes: 64 additions & 0 deletions test/Datadog.Trace.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Datadog.Trace.Agent;
Expand Down Expand Up @@ -337,5 +338,68 @@ public void SetServiceName(string envServiceName, string tracerServiceName, stri
// reset the environment variable to its original values (if any) when done
Environment.SetEnvironmentVariable(name, originalEnv);
}

[Theory]
[InlineData(null, null, 0)]
[InlineData(null, "", 0)]
[InlineData("", null, 0)]
[InlineData("", "avalue", 0)]
[InlineData(" ", "123", 0)]
[InlineData("key1", "value1", 1)]
[InlineData("key1", null, 1)]
[InlineData("key1", "", 1)]
public void AddGlobalTag(string key, string value, int expectedCount)
{
_tracer.AddGlobalTag(key, value);

Assert.Equal(expectedCount, _tracer.Settings.GlobalTags.Count);
}

[Fact]
public void AddGlobalTag_OverwriteExpected()
{
string key = "k1";
string firstvalue = "v1";
string lastValue = "asdf";

_tracer.AddGlobalTag(key, firstvalue);
_tracer.AddGlobalTag(key, lastValue);

Assert.True(_tracer.Settings.GlobalTags.Count == 1);
Assert.True(_tracer.Settings.GlobalTags[key] == lastValue);
}

[Fact]
public void AddGlobalTags_AllValid()
{
var tags = new Dictionary<string, string>()
{
{ "k1", "v1" },
{ "k2", string.Empty },
{ "k3", null }
};

_tracer.AddGlobalTags(tags);

Assert.Equal(tags.Count, _tracer.Settings.GlobalTags.Count);
}

[Fact]
public void AddGlobalTags_IncludesTwoInValidKeys()
{
var tags = new Dictionary<string, string>()
{
{ "k1", "v1" },
{ "k2", string.Empty },
{ "k3", null },
{ string.Empty, "yo" },
{ "abc", "def" },
{ " ", "xyz" }
};

_tracer.AddGlobalTags(tags);

Assert.Equal(tags.Count - 2, _tracer.Settings.GlobalTags.Count);
}
}
}