Skip to content

Commit f738152

Browse files
committed
refactor: extract Defaults
1 parent e954e73 commit f738152

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

src/Defaults.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Prometheus.Client.MetricServer;
2+
3+
internal static class Defaults
4+
{
5+
internal const string MapPath = "/metrics";
6+
internal const string ContentType = "text/plain; version=0.0.4";
7+
}

src/MetricServer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
106106

107107
public void Configure(IApplicationBuilder app)
108108
{
109-
var contentType = "text/plain; version=0.0.4";
110-
111-
if (_options.ResponseEncoding != null)
112-
contentType += $"; charset={_options.ResponseEncoding.BodyName}";
109+
var contentType = _options.ResponseEncoding != null
110+
? $"{Defaults.ContentType}; charset={_options.ResponseEncoding.BodyName}"
111+
: Defaults.ContentType;
113112

114113
app.Map(_options.MapPath, coreapp =>
115114
{

src/MetricServerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class MetricServerOptions
2323
/// <summary>
2424
/// Endpoint path
2525
/// </summary>
26-
public string MapPath { get; set; } = "/metrics";
26+
public string MapPath { get; set; } = Defaults.MapPath;
2727

2828
/// <summary>
2929
/// Https certificate

src/Prometheus.Client.MetricServer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Prometheus.Client" Version="[5.2.0,6.0.0)" />
1212
</ItemGroup>
13+
<ItemGroup>
14+
<InternalsVisibleTo Include="$(MSBuildProjectName).Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010035b85a8ca7e3391c4ac9123c6e51491977a1229b85e572e70350884579334c7d44e3c00de913f684eaab330ceeaba6eb41bbb359b7157ff17542a4e112856c4a18ab521eb7acb4f5a03f3d0a7e849e5406bae28d6780bb2453122e60b52aa76ae10611a1d4a0ffba682558c05ae2b5a0709c260b8a0c3afb15cf2ca0eed949ee" />
15+
</ItemGroup>
1316
</Project>

tests/MetricServerTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void Start_Stop_IsRunning()
4040
Assert.False(_metricServer.IsRunning);
4141
}
4242

43-
4443
[Fact]
4544
public void Start_DoubleStop_IsRunning()
4645
{
@@ -73,7 +72,6 @@ public void Start_Stop_WithDefaultPort_IsRunning()
7372
Assert.False(_metricServer.IsRunning);
7473
}
7574

76-
7775
[Fact]
7876
public void Start_Stop_WithDefaultRegisry_IsRunning()
7977
{
@@ -93,7 +91,7 @@ public async Task BaseMapPath_FindMetrics()
9391
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", "help");
9492
counter.Inc();
9593
using var httpClient = new HttpClient();
96-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
94+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
9795
Assert.False(string.IsNullOrEmpty(response));
9896
Assert.Contains("process_private_memory_bytes", response);
9997
Assert.Contains("dotnet_total_memory_bytes", response);
@@ -136,8 +134,8 @@ public async Task SetMapPath_FindMetricsWithEndSlash()
136134
}
137135

138136
[Theory]
137+
[InlineData(Defaults.MapPath)]
139138
[InlineData("metrics")]
140-
[InlineData("/metrics")]
141139
[InlineData("metrics12")]
142140
[InlineData("/metrics965")]
143141
public async Task SetMapPath_FindMetrics(string mapPath)
@@ -181,7 +179,7 @@ public async Task CustomCounter_FindMetric()
181179
counter.Inc();
182180

183181
using var httpClient = new HttpClient();
184-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
182+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
185183
Assert.Contains(metricName, response);
186184
}
187185
catch (Exception ex)
@@ -202,7 +200,7 @@ public async Task AddLegacyMetrics_False_FindMetrics()
202200
{
203201
_metricServer.Start();
204202
using var httpClient = new HttpClient();
205-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
203+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
206204
Assert.Contains("process_private_memory_bytes", response);
207205
Assert.Contains("dotnet_total_memory_bytes", response);
208206
Assert.DoesNotContain("process_private_bytes", response);
@@ -228,7 +226,7 @@ public async Task AddLegacyMetrics_True_FindMetrics()
228226
{
229227
_metricServer.Start();
230228
using var httpClient = new HttpClient();
231-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
229+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
232230
Assert.Contains("process_private_memory_bytes", response);
233231
Assert.Contains("dotnet_total_memory_bytes", response);
234232
Assert.Contains("process_private_bytes", response);
@@ -285,7 +283,7 @@ public async Task CustormEncoding_FindHelp()
285283
counter.Inc();
286284

287285
using var httpClient = new HttpClient();
288-
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
286+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}{Defaults.MapPath}");
289287
Assert.Contains(help, response);
290288
}
291289
catch (Exception ex)

0 commit comments

Comments
 (0)