Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test improvements #2612

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/OpenTelemetry.Exporter.Prometheus/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Runtime.CompilerServices;

#if SIGNED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)
}
}

this.previousDataView = new ArraySegment<byte>(this.buffer, 0, cursor);
this.previousDataView = new ArraySegment<byte>(this.buffer, 0, Math.Max(cursor - 1, 0));
return ExportResult.Success;
}
catch (Exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ internal static partial class PrometheusSerializer
{
private static readonly string[] MetricTypes = new string[] { "untyped", "counter", "gauge", "histogram", "summary" };

public static int WriteMetrics(byte[] buffer, int cursor, Batch<Metric> metrics)
{
var spacing = false;

foreach (var metric in metrics)
{
if (spacing)
{
buffer[cursor++] = ASCII_LINEFEED;
}
else
{
spacing = true;
}

cursor = WriteMetric(buffer, cursor, metric);
}

return cursor;
}

public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
{
if (!string.IsNullOrWhiteSpace(metric.Description))
Expand Down Expand Up @@ -199,6 +178,8 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
}
}

buffer[cursor++] = ASCII_LINEFEED;

return cursor;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus\OpenTelemetry.Exporter.Prometheus.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public async Task PrometheusExporterHttpServerIntegration()
new KeyValuePair<string, object>("key2", "value2"),
};

var beginTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

var counter = meter.CreateCounter<double>("counter_double");
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);
Expand All @@ -99,29 +97,11 @@ public async Task PrometheusExporterHttpServerIntegration()

using var response = await client.GetAsync($"{address}metrics").ConfigureAwait(false);

var endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

string[] lines = content.Split('\n');

Assert.Equal(
$"# TYPE counter_double counter",
lines[0]);

Assert.Contains(
$"counter_double{{key1=\"value1\",key2=\"value2\"}} 101.17",
lines[1]);

var index = content.LastIndexOf(' ');

Assert.Equal('\n', content[content.Length - 1]);

var timestamp = long.Parse(content.Substring(index, content.Length - index - 1));

Assert.True(beginTimestamp <= timestamp && timestamp <= endTimestamp);
Assert.Matches(
"^# TYPE counter_double counter\ncounter_double{key1='value1',key2='value2'} 101.17 \\d+\n$".Replace('\'', '"'),
await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// <copyright file="PrometheusSerializerTests.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;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Text;
using OpenTelemetry.Metrics;
using OpenTelemetry.Tests;
using Xunit;

namespace OpenTelemetry.Exporter.Prometheus.Tests
{
public sealed class PrometheusSerializerTests
{
[Fact]
public void ZeroDimension()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

meter.CreateObservableGauge("test_gauge", () => 123);

provider.ForceFlush();

var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
"^# TYPE test_gauge gauge\ntest_gauge 123 \\d+\n$",
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void ZeroDimensionWithDescription()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

meter.CreateObservableGauge("test_gauge", () => 123, description: "Hello, world!");

provider.ForceFlush();

var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
"^# HELP test_gauge Hello, world!\n# TYPE test_gauge gauge\ntest_gauge 123 \\d+\n$",
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void ZeroDimensionWithUnit()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

meter.CreateObservableGauge("test_gauge", () => 123, unit: "seconds");

provider.ForceFlush();

var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
"^# TYPE test_gauge_seconds gauge\ntest_gauge_seconds 123 \\d+\n$",
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void OneDimension()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

var counter = meter.CreateCounter<long>("test_counter");
counter.Add(123, new KeyValuePair<string, object>("tagKey", "tagValue"));

provider.ForceFlush();

var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
"^# TYPE test_counter counter\ntest_counter{tagKey='tagValue'} 123 \\d+\n$".Replace('\'', '"'),
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void DoubleInfinites()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.Build();

var counter = meter.CreateCounter<double>("test_counter");
counter.Add(1.0E308);
counter.Add(1.0E308);

provider.ForceFlush();

var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]);
Assert.Matches(
"^# TYPE test_counter counter\ntest_counter \\+Inf \\d+\n$",
Encoding.UTF8.GetString(buffer, 0, cursor));
}
}
}
28 changes: 12 additions & 16 deletions test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,25 @@ public class InMemoryExporterTests
[Fact(Skip = "To be run after https://github.com/open-telemetry/opentelemetry-dotnet/issues/2361 is fixed")]
public void InMemoryExporterShouldDeepCopyMetricPoints()
{
var meter = new Meter(Utils.GetCurrentMethodName());

var exportedItems = new List<Metric>();
using var inMemoryReader = new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems))
{
PreferredAggregationTemporality = AggregationTemporality.Delta,
};
var metrics = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddReader(inMemoryReader)
.Build();
.AddMeter(meter.Name)
.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(metrics))
{
PreferredAggregationTemporality = AggregationTemporality.Delta,
})
.Build();

var counter = meter.CreateCounter<long>("meter");

// Emit 10 for the MetricPoint with a single key-vaue pair: ("tag1", "value1")
counter.Add(10, new KeyValuePair<string, object>("tag1", "value1"));

// Pull metric data from AggregatorStore
inMemoryReader.Collect();
meterProvider.ForceFlush();

var metric = exportedItems[0]; // Only one Metric object is added to the collection at this point
var metric = metrics[0]; // Only one Metric object is added to the collection at this point
var metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric
ref var metricPointForFirstExport = ref metricPointsEnumerator.Current;
Expand All @@ -57,10 +54,9 @@ public void InMemoryExporterShouldDeepCopyMetricPoints()
// Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1")
counter.Add(25, new KeyValuePair<string, object>("tag1", "value1"));

// Pull metric data from AggregatorStore
inMemoryReader.Collect();
meterProvider.ForceFlush();

metric = exportedItems[1]; // Second Metric object is added to the collection at this point
metric = metrics[1]; // Second Metric object is added to the collection at this point
metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric
var metricPointForSecondExport = metricPointsEnumerator.Current;
Expand Down
4 changes: 2 additions & 2 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public void MultithreadedDoubleCounterTest()
}

[Theory]
[MemberData(nameof(MetricsTestData.InvalidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.InvalidInstrumentNames), MemberType = typeof(MetricTestData))]
public void InstrumentWithInvalidNameIsIgnoredTest(string instrumentName)
{
var exportedItems = new List<Metric>();
Expand All @@ -616,7 +616,7 @@ public void InstrumentWithInvalidNameIsIgnoredTest(string instrumentName)
}

[Theory]
[MemberData(nameof(MetricsTestData.ValidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.ValidInstrumentNames), MemberType = typeof(MetricTestData))]
public void InstrumentWithValidNameIsExportedTest(string name)
{
var exportedItems = new List<Metric>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="MetricsTestData.cs" company="OpenTelemetry Authors">
// <copyright file="MetricTestData.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,7 +18,7 @@

namespace OpenTelemetry.Metrics.Tests
{
public class MetricsTestData
public class MetricTestData
{
public static IEnumerable<object[]> InvalidInstrumentNames
=> new List<object[]>
Expand Down
10 changes: 5 additions & 5 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void ViewToRenameMetric()
}

[Theory]
[MemberData(nameof(MetricsTestData.InvalidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.InvalidInstrumentNames), MemberType = typeof(MetricTestData))]
public void AddViewWithInvalidNameThrowsArgumentException(string viewNewName)
{
var exportedItems = new List<Metric>();
Expand Down Expand Up @@ -93,7 +93,7 @@ public void AddViewWithNullMetricStreamConfigurationThrowsArgumentnullException(
}

[Theory]
[MemberData(nameof(MetricsTestData.InvalidHistogramBounds), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.InvalidHistogramBounds), MemberType = typeof(MetricTestData))]
public void AddViewWithInvalidHistogramBoundsThrowsArgumentException(double[] bounds)
{
var ex = Assert.Throws<ArgumentException>(() => Sdk.CreateMeterProviderBuilder()
Expand All @@ -103,7 +103,7 @@ public void AddViewWithInvalidHistogramBoundsThrowsArgumentException(double[] bo
}

[Theory]
[MemberData(nameof(MetricsTestData.ValidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.ValidInstrumentNames), MemberType = typeof(MetricTestData))]
public void ViewWithValidNameExported(string viewNewName)
{
var exportedItems = new List<Metric>();
Expand Down Expand Up @@ -168,7 +168,7 @@ public void ViewToRenameMetricConditionally()
}

[Theory]
[MemberData(nameof(MetricsTestData.InvalidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.InvalidInstrumentNames), MemberType = typeof(MetricTestData))]
public void ViewWithInvalidNameIgnoredConditionally(string viewNewName)
{
using var meter1 = new Meter("ViewToRenameMetricConditionallyTest");
Expand Down Expand Up @@ -205,7 +205,7 @@ public void ViewWithInvalidNameIgnoredConditionally(string viewNewName)
}

[Theory]
[MemberData(nameof(MetricsTestData.ValidInstrumentNames), MemberType = typeof(MetricsTestData))]
[MemberData(nameof(MetricTestData.ValidInstrumentNames), MemberType = typeof(MetricTestData))]
public void ViewWithValidNameConditionally(string viewNewName)
{
using var meter1 = new Meter("ViewToRenameMetricConditionallyTest");
Expand Down