Fixing culture in PrometheusMetricBuilder (#839)

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Eddy Nakamura 2020-07-18 12:57:55 -03:00 committed by GitHub
parent e13c33f596
commit 5c20cf5b34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 1 deletions

View File

@ -13,3 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Runtime.CompilerServices;
#if SIGNED
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
#else
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus.Tests")]
#endif

View File

@ -15,6 +15,7 @@
// </copyright>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
@ -163,7 +164,7 @@ namespace OpenTelemetry.Exporter.Prometheus.Implementation
// standard numerical values, Nan, +Inf, and -Inf are valid values representing not a number,
// positive infinity, and negative infinity, respectively.
writer.Write(" ");
writer.Write(m.Value);
writer.Write(m.Value.ToString(CultureInfo.InvariantCulture));
writer.Write(" ");
// The timestamp is an int64 (milliseconds since epoch, i.e. 1970-01-01 00:00:00 UTC, excluding

View File

@ -0,0 +1,62 @@
// <copyright file="PrometheusMetricBuilderTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using OpenTelemetry.Exporter.Prometheus.Implementation;
using Xunit;
namespace OpenTelemetry.Exporter.Prometheus.Tests.Implementation
{
public class PrometheusMetricBuilderTests
{
[Theory]
[InlineData(null)]
[InlineData("pt-BR")]
public void Test(string cultureInfo)
{
if (!string.IsNullOrEmpty(cultureInfo))
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureInfo);
}
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
var builder = new PrometheusMetricBuilder();
builder.WithName("test-builder");
builder.WithDescription("test-description");
builder.WithType("test-type");
var metricValueBuilder = builder.AddValue();
metricValueBuilder = metricValueBuilder.WithValue(10.0123);
metricValueBuilder.WithLabel("test-double", "double");
builder.Write(writer);
writer.Flush();
// assert
string actual = Encoding.UTF8.GetString(stream.ToArray());
string[] lines = actual.Split('\n');
Assert.Equal("# HELP test_buildertest-description", lines[0]);
Assert.Equal("# TYPE test_builder test-type", lines[1]);
Assert.StartsWith("test_builder{test_double=\"double\"} 10.01", lines[2]);
}
}
}