File tree Expand file tree Collapse file tree
src/StackExchange.Metrics
tests/StackExchange.Metrics.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff 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"/>.
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments