Skip to content

Commit 8530b2f

Browse files
author
Dean Ward
committed
Allow ConfigureSources to accept the instance of MetricSourceOptions that we want to use
Remove the addition of default metric sources
1 parent 8f869ae commit 8530b2f

5 files changed

Lines changed: 16 additions & 33 deletions

File tree

src/StackExchange.Metrics/DependencyInjection/MetricsCollectorBuilder.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ public MetricsCollectorBuilder(IServiceCollection services)
1818

1919
Options = new MetricsCollectorOptions();
2020
Services = services;
21-
22-
this.ConfigureSources(
23-
o => o.DefaultTags.Add("host", Environment.MachineName)
24-
);
25-
this.AddDefaultSources();
2621
}
2722

2823
public IServiceCollection Services { get; }

src/StackExchange.Metrics/DependencyInjection/MetricsCollectorBuilderExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,21 @@ public static IMetricsCollectorBuilder ConfigureSources(this IMetricsCollectorBu
113113
{
114114
var options = new MetricSourceOptions();
115115
action?.Invoke(options);
116+
return builder.ConfigureSources(options);
117+
}
118+
119+
/// <summary>
120+
/// Configures the default <see cref="MetricSourceOptions"/> used for <see cref="MetricSource"/> instances
121+
/// passed to the collector.
122+
/// </summary>
123+
public static IMetricsCollectorBuilder ConfigureSources(this IMetricsCollectorBuilder builder, MetricSourceOptions options)
124+
{
116125
builder.Services.RemoveAll<MetricSourceOptions>();
117126
builder.Services.AddSingleton(options);
118127
return builder;
119128
}
120129

130+
121131
/// <summary>
122132
/// Registers an <see cref="MetricSource" /> for use with the collector.
123133
/// </summary>

src/StackExchange.Metrics/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.Extensions.Hosting;
22
using StackExchange.Metrics;
33
using StackExchange.Metrics.DependencyInjection;
4+
#if NETCOREAPP
45
using StackExchange.Metrics.Infrastructure;
6+
#endif
57

68
namespace Microsoft.Extensions.DependencyInjection
79
{

src/StackExchange.Metrics/MetricSourceOptions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public MetricSourceOptions()
3535
MetricNameValidator = s_defaultMetricNameValidator;
3636
TagNameValidator = s_defaultTagNameValidator;
3737
TagValueValidator = s_defaultTagValueValidator;
38-
DefaultTags = new TagDictionary(this);
38+
DefaultTags = new TagDictionary(this)
39+
{
40+
["host"] = Environment.MachineName
41+
};
3942
}
4043

4144
/// <summary>

tests/StackExchange.Metrics.Tests/MetricsCollectorBuilderTests.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ public MetricsCollectorBuilderTests(ITestOutputHelper output)
2020
_output = output;
2121
}
2222

23-
[Fact]
24-
public void DefaultOptions_HasHostTag()
25-
{
26-
var services = CreateServices();
27-
var builder = new MetricsCollectorBuilder(services);
28-
29-
var serviceProvider = services.BuildServiceProvider();
30-
var options = builder.Build(serviceProvider);
31-
32-
var sourceOptions = serviceProvider.GetRequiredService<MetricSourceOptions>();
33-
34-
Assert.Contains(
35-
sourceOptions.DefaultTags, x => x.Key == "host" && x.Value == Environment.MachineName.ToLower()
36-
);
37-
}
38-
39-
40-
[Fact]
41-
public void DefaultOptions_HasDefaultSources()
42-
{
43-
var options = CreateOptions();
44-
45-
Assert.Contains(options.Sources, x => x is AspNetMetricSource);
46-
Assert.Contains(options.Sources, x => x is RuntimeMetricSource);
47-
Assert.Contains(options.Sources, x => x is ProcessMetricSource);
48-
}
49-
5023
[Fact]
5124
public void Endpoints_LocalAddedByDefault()
5225
{

0 commit comments

Comments
 (0)