Skip to content

Commit 02f9ca5

Browse files
committed
feat: add response encoding to Options
1 parent e4b048b commit 02f9ca5

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

src/Prometheus.Client.MetricServer/MetricServer.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Start()
8787
options.Listen(IPAddress.Any, _options.Port, listenOptions => { listenOptions.UseHttps(_options.Certificate); });
8888
})
8989
.UseUrls($"http{(_options.Certificate != null ? "s" : "")}://{_options.Host}:{_options.Port}")
90-
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_options.CollectorRegistryInstance, _options.MapPath)); })
90+
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_options)); })
9191
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
9292
.Build();
9393

@@ -106,15 +106,11 @@ public void Stop()
106106

107107
internal class Startup : IStartup
108108
{
109-
private const string _contentType = "text/plain; version=0.0.4";
110-
private readonly string _mapPath;
109+
private readonly MetricServerOptions _options;
111110

112-
private readonly ICollectorRegistry _registry;
113-
114-
public Startup(ICollectorRegistry registry, string mapPath)
111+
public Startup(MetricServerOptions options)
115112
{
116-
_registry = registry;
117-
_mapPath = mapPath;
113+
_options = options;
118114

119115
var builder = new ConfigurationBuilder();
120116
Configuration = builder.Build();
@@ -129,15 +125,20 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
129125

130126
public void Configure(IApplicationBuilder app)
131127
{
132-
app.Map(_mapPath, coreapp =>
128+
var contentType = "text/plain; version=0.0.4";
129+
130+
if (_options.ResponseEncoding != null)
131+
contentType += $"; charset={_options.ResponseEncoding.BodyName}";
132+
133+
app.Map(_options.MapPath, coreapp =>
133134
{
134135
coreapp.Run(async context =>
135136
{
136137
var response = context.Response;
137-
response.ContentType = _contentType;
138+
response.ContentType = contentType;
138139

139140
using var outputStream = response.Body;
140-
await ScrapeHandler.ProcessAsync(_registry, outputStream);
141+
await ScrapeHandler.ProcessAsync(_options.CollectorRegistryInstance, outputStream);
141142
});
142143
});
143144
}

src/Prometheus.Client.MetricServer/MetricServerOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Cryptography.X509Certificates;
2+
using System.Text;
23
using Prometheus.Client.Collectors;
34

45
namespace Prometheus.Client.MetricServer
@@ -37,5 +38,10 @@ public class MetricServerOptions
3738
/// Use default collectors(dotnet and process stats)
3839
/// </summary>
3940
public bool UseDefaultCollectors { get; set; } = false;
41+
42+
/// <summary>
43+
/// Charset of text response.
44+
/// </summary>
45+
public Encoding ResponseEncoding { get; set; }
4046
}
4147
}

tests/Prometheus.Client.MetricServer.Tests/MetricServerTests.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Net;
33
using System.Net.Http;
4+
using System.Text;
45
using System.Threading.Tasks;
56
using Prometheus.Client.Collectors;
67
using Xunit;
@@ -125,7 +126,7 @@ public async Task Find_Metric()
125126
{
126127
var registry = new CollectorRegistry();
127128
var factory = new MetricFactory(registry);
128-
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry});
129+
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry });
129130

130131
try
131132
{
@@ -179,12 +180,7 @@ public async Task Url_NotFound()
179180
public async Task Find_Default_Metric()
180181
{
181182
var registry = new CollectorRegistry();
182-
var metricServer = new MetricServer(new MetricServerOptions
183-
{
184-
Port = _port,
185-
CollectorRegistryInstance = registry,
186-
UseDefaultCollectors = true
187-
});
183+
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry, UseDefaultCollectors = true });
188184

189185
try
190186
{
@@ -205,5 +201,33 @@ public async Task Find_Default_Metric()
205201
metricServer.Stop();
206202
}
207203
}
204+
205+
[Fact]
206+
public async Task Add_Encoding()
207+
{
208+
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, ResponseEncoding = Encoding.UTF8 });
209+
210+
try
211+
{
212+
metricServer.Start();
213+
214+
const string help = "русский хелп";
215+
var counter = Metrics.DefaultFactory.CreateCounter("test_counter", help);
216+
counter.Inc();
217+
218+
using var httpClient = new HttpClient();
219+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
220+
Assert.Contains(help, response);
221+
}
222+
catch (Exception ex)
223+
{
224+
_testOutputHelper.WriteLine(ex.ToString());
225+
throw;
226+
}
227+
finally
228+
{
229+
metricServer.Stop();
230+
}
231+
}
208232
}
209233
}

0 commit comments

Comments
 (0)