Skip to content

Commit 5a5d749

Browse files
authored
Merge pull request #24 from prom-client-net/response-encoding
Response encoding
2 parents 46757c9 + 4cb3d02 commit 5a5d749

File tree

8 files changed

+57
-38
lines changed

8 files changed

+57
-38
lines changed

.github/workflows/branch.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ name: 💿 CI Branch
22

33
on:
44
push:
5-
branches:
6-
- '*'
7-
- '!master'
8-
tags:
9-
- '!refs/tags/*'
5+
branches-ignore:
6+
- "master"
107
pull_request:
118
branches:
12-
- master
13-
9+
- "master"
1410
jobs:
1511
build-branch:
1612
name: 🏭 Build Branch

.github/workflows/master.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ name: 💿 CI Master
33
on:
44
push:
55
branches:
6-
- 'master'
7-
tags:
8-
- '!refs/tags/*'
6+
- "master"
97

108
jobs:
119
build-dev:

.github/workflows/production.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: 💿 CI Prod
33
on:
44
push:
55
tags:
6-
- 'v*'
6+
- "v*"
77

88
jobs:
99
build-prod:

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
[![NuGet](https://img.shields.io/nuget/v/Prometheus.Client.MetricServer.svg)](https://www.nuget.org/packages/Prometheus.Client.MetricServer)
88
[![NuGet](https://img.shields.io/nuget/dt/Prometheus.Client.MetricServer.svg)](https://www.nuget.org/packages/Prometheus.Client.MetricServer)
9-
[![CI](https://github.com/PrometheusClientNet/Prometheus.Client.MetricServer/workflows/CI/badge.svg)](https://github.com/PrometheusClientNet/Prometheus.Client.MetricServer/actions?query=workflow%3ACI)
10-
[![Gitter](https://img.shields.io/gitter/room/PrometheusClientNet/community.svg)](https://gitter.im/PrometheusClientNet/community)
9+
[![CI](https://img.shields.io/github/workflow/status/prom-client-net/prom-client-metricserver/%F0%9F%92%BF%20CI%20Master?label=CI&logo=github)](https://github.com/prom-client-net/prom-client-metricserver/actions/workflows/master.yml)
1110
[![License MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT)
1211

1312
Extension for [Prometheus. Client](https://github.com/PrometheusClientNet/Prometheus.Client)

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
}

src/Prometheus.Client.MetricServer/Prometheus.Client.MetricServer.csproj

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Standalone Kestrel server for the Prometheus.Client</Description>
4-
<Copyright>2020 © Serge K, Oleksandr Poliakov</Copyright>
5-
<AssemblyTitle>Prometheus.Client.MetricServer</AssemblyTitle>
6-
<VersionPrefix>4.2.0</VersionPrefix>
7-
<Authors>Serge K, Oleksandr Poliakov</Authors>
4+
<VersionPrefix>4.3.0</VersionPrefix>
85
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks>
9-
<LangVersion>latest</LangVersion>
106
<AssemblyName>Prometheus.Client.MetricServer</AssemblyName>
117
<PackageId>Prometheus.Client.MetricServer</PackageId>
128
<PackageTags>prometheus;metrics</PackageTags>
139
<PackageIcon>icon.png</PackageIcon>
14-
<PackageProjectUrl>https://github.com/prom-client-net/Prometheus.Client.MetricServer</PackageProjectUrl>
1510
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1611
<RepositoryType>git</RepositoryType>
17-
<RepositoryUrl>https://github.com/prom-client-net/Prometheus.Client.MetricServer</RepositoryUrl>
12+
<RepositoryUrl>https://github.com/prom-client-net/prom-client-metricserver</RepositoryUrl>
1813
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1914
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2015
<SignAssembly>true</SignAssembly>

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_rus", 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)