Do not export metrics description if it is empty (#2583)

This commit is contained in:
Reiley Yang 2021-11-09 19:22:45 -08:00 committed by GitHub
parent 9ad2cfbec9
commit f0311751d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 18 deletions

View File

@ -29,7 +29,7 @@ namespace Examples.Console
{
private static readonly Meter MyMeter = new Meter("TestMeter");
private static readonly Counter<double> Counter = MyMeter.CreateCounter<double>("myCounter", description: "A counter for demonstration purpose.");
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram", description: "A histogram for demonstration purpose.");
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram");
private static readonly ThreadLocal<Random> ThreadLocalRandom = new ThreadLocal<Random>(() => new Random());
internal static object Run(int port, int totalDurationInMins)

View File

@ -131,7 +131,8 @@ namespace OpenTelemetry.Exporter.Prometheus
try
{
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = PrometheusMetricsFormatHelper.ContentType;
ctx.Response.Headers.Add("Server", string.Empty);
ctx.Response.ContentType = "text/plain; charset=utf-8; version=0.0.4";
this.exporter.OnExport = (metrics) =>
{

View File

@ -115,7 +115,7 @@ namespace OpenTelemetry.Exporter.Prometheus
internal static async Task WriteMetricsToResponse(byte[] buffer, int count, HttpResponse response)
{
response.StatusCode = 200;
response.ContentType = PrometheusMetricsFormatHelper.ContentType;
response.ContentType = "text/plain; charset=utf-8; version=0.0.4";
await response.Body.WriteAsync(buffer, 0, count).ConfigureAwait(false);
}

View File

@ -27,8 +27,6 @@ namespace OpenTelemetry.Exporter.Prometheus
{
internal static class PrometheusMetricsFormatHelper
{
public const string ContentType = "text/plain; version = 0.0.4";
#if NETCOREAPP3_1_OR_GREATER
private static readonly SpanAction<char, (string, Func<char, bool, bool>)> CreateName
= (Span<char> data, (string name, Func<char, bool, bool> isCharacterAllowedFunc) state) =>

View File

@ -48,7 +48,7 @@ namespace OpenTelemetry.Exporter.Prometheus
public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
{
if (metric.Description != null)
if (!string.IsNullOrWhiteSpace(metric.Description))
{
cursor = WriteHelpText(buffer, cursor, metric.Name, metric.Unit, metric.Description);
}

View File

@ -107,17 +107,13 @@ namespace OpenTelemetry.Exporter.Prometheus.Tests
string[] lines = content.Split('\n');
Assert.Equal(
$"# HELP counter_double",
lines[0]);
Assert.Equal(
$"# TYPE counter_double counter",
lines[1]);
lines[0]);
Assert.Contains(
$"counter_double{{key1=\"value1\",key2=\"value2\"}} 101.17",
lines[2]);
lines[1]);
var index = content.LastIndexOf(' ');

View File

@ -68,17 +68,13 @@ namespace OpenTelemetry.Exporter.Prometheus.Tests
string[] lines = content.Split('\n');
Assert.Equal(
$"# HELP counter_double",
lines[0]);
Assert.Equal(
$"# TYPE counter_double counter",
lines[1]);
lines[0]);
Assert.Contains(
$"counter_double{{key1=\"value1\",key2=\"value2\"}} 101.17",
lines[2]);
lines[1]);
var index = content.LastIndexOf(' ');