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

Prometheus: Unit tests part 4 #2465

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace OpenTelemetry.Exporter
[ExportModes(ExportModes.Pull)]
public class PrometheusExporter : BaseExporter<Metric>, IPullMetricExporter
{
internal const string HttpListenerStartFailureExceptionMessage = "PrometheusExporter http listener could not be started.";
internal readonly PrometheusExporterOptions Options;
internal Batch<Metric> Metrics;
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
Expand All @@ -52,7 +53,7 @@ public PrometheusExporter(PrometheusExporterOptions options)
}
catch (Exception ex)
{
throw new InvalidOperationException("PrometheusExporter http listener could not be started.", ex);
throw new InvalidOperationException(HttpListenerStartFailureExceptionMessage, ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// <copyright file="PrometheusExporterMetricsHttpServerTests.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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using OpenTelemetry.Metrics;
using Xunit;

namespace OpenTelemetry.Exporter.Prometheus.Tests
{
public class PrometheusExporterMetricsHttpServerTests
{
private const string MeterName = "PrometheusExporterMetricsHttpServerTests.Meter";

[Fact]
public async Task PrometheusExporterMetricsHttpServerIntegration()
{
Random random = new Random();
int port = 0;
int retryCount = 5;
MeterProvider provider;
string address = null;

while (true)
{
try
{
port = random.Next(2000, 5000);
provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(MeterName)
.AddPrometheusExporter(o =>
{
o.GetUtcNowDateTimeOffset = () => new DateTimeOffset(2021, 9, 30, 22, 30, 0, TimeSpan.Zero);
#if NET461
bool expectedDefaultState = true;
#else
bool expectedDefaultState = false;
#endif
if (o.StartHttpListener != expectedDefaultState)
{
throw new InvalidOperationException("StartHttpListener value is unexpected.");
}

if (!o.StartHttpListener)
{
o.StartHttpListener = true;
}

address = $"http://localhost:{port}/";
o.HttpListenerPrefixes = new string[] { address };
})
.Build();
break;
}
catch (Exception ex)
{
if (ex.Message != PrometheusExporter.HttpListenerStartFailureExceptionMessage)
{
throw;
}

if (retryCount-- <= 0)
{
throw new InvalidOperationException("HttpListener could not be started.");
}
}
}

var tags = new KeyValuePair<string, object>[]
{
new KeyValuePair<string, object>("key1", "value1"),
new KeyValuePair<string, object>("key2", "value2"),
};

using var meter = new Meter(MeterName, "0.0.1");

var counter = meter.CreateCounter<double>("counter_double");
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);

using HttpClient client = new HttpClient();

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

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

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

Assert.Equal(
$"# TYPE counter_double counter\ncounter_double{{key1=\"value1\",key2=\"value2\"}} 101.17 1633041000000\n",
content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task PrometheusExporterMiddlewareIntegration()
counter.Add(100.18D, tags);
counter.Add(0.99D, tags);

using var response = await host.GetTestClient().GetAsync("/metrics");
using var response = await host.GetTestClient().GetAsync("/metrics").ConfigureAwait(false);

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

Expand Down