Skip to content

Commit d53f626

Browse files
author
Dean Ward
committed
Fix bug whereby the host tag (or any other default, default tag) would not have a transformer applied to it
1 parent e3706dc commit d53f626

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/StackExchange.Metrics/MetricSourceOptions.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ public MetricSourceOptions()
4747
MetricNameValidator = s_defaultMetricNameValidator;
4848
TagNameValidator = s_defaultTagNameValidator;
4949
TagValueValidator = s_defaultTagValueValidator;
50-
DefaultTags = new TagDictionary(this)
51-
{
52-
["host"] = Environment.MachineName
53-
};
5450
}
5551

5652
/// <summary>
@@ -113,10 +109,26 @@ public ValidationDelegate TagValueValidator
113109
set => _tagValueValidator = value ?? throw new ArgumentNullException(nameof(value));
114110
}
115111

112+
private IDictionary<string, string> _defaultTags;
116113
/// <summary>
117114
/// Gets tag name/value pairs that are applied to all metrics.
118115
/// </summary>
119-
public IDictionary<string, string> DefaultTags { get; }
116+
public IDictionary<string, string> DefaultTags
117+
{
118+
get
119+
{
120+
if (_defaultTags == null)
121+
{
122+
_defaultTags = new TagDictionary(this);
123+
// important *not* to use an object initializer here
124+
// we want to be sure that we're calling the explicitly
125+
// implemented Add rather than the one defined on Dictionary
126+
_defaultTags.Add("host", Environment.MachineName);
127+
}
128+
129+
return _defaultTags;
130+
}
131+
}
120132

121133
/// <summary>
122134
/// Gets an immutable version of <see cref="DefaultTagsFrozen"/>.

tests/StackExchange.Metrics.Tests/MetricSourceOptionsTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public void DefaultTags_TagNamesAreTransformed()
4040
Assert.True(options.DefaultTags.ContainsKey("NAME"));
4141
}
4242

43+
[Fact]
44+
public void DefaultTags_HasTransformedHost()
45+
{
46+
var options = new MetricSourceOptions
47+
{
48+
TagValueTransformer = (_, __) => "qwertyYTREWQ"
49+
};
50+
51+
Assert.True(options.DefaultTags.TryGetValue("host", out var value) && value == "qwertyYTREWQ");
52+
}
53+
4354
[Fact]
4455
public void DefaultTags_TagValuesAreTransformed()
4556
{

0 commit comments

Comments
 (0)