Skip to content

Commit 0cbd128

Browse files
committed
Unit tests.
1 parent 66941fd commit 0cbd128

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ void CheckMetricReaderDefaults()
7979
}
8080
}
8181

82+
[Fact]
83+
public void TestAddOtlpExporter_NamedOptions()
84+
{
85+
int defaultExporterOptionsConfigureOptionsInvocations = 0;
86+
int namedExporterOptionsConfigureOptionsInvocations = 0;
87+
88+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
89+
.ConfigureServices(services =>
90+
{
91+
services.Configure<OtlpExporterOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
92+
services.Configure<MetricReaderOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
93+
94+
services.Configure<OtlpExporterOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
95+
services.Configure<MetricReaderOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
96+
97+
services.Configure<OtlpExporterOptions>("Exporter3", o => namedExporterOptionsConfigureOptionsInvocations++);
98+
services.Configure<MetricReaderOptions>("Exporter3", o => namedExporterOptionsConfigureOptionsInvocations++);
99+
})
100+
.AddOtlpExporter()
101+
.AddOtlpExporter("Exporter2", o => { })
102+
.AddOtlpExporter("Exporter3", (eo, ro) => { })
103+
.Build();
104+
105+
Assert.Equal(2, defaultExporterOptionsConfigureOptionsInvocations);
106+
Assert.Equal(4, namedExporterOptionsConfigureOptionsInvocations);
107+
}
108+
82109
[Fact]
83110
public void UserHttpFactoryCalled()
84111
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <copyright file="PrometheusExporterMeterProviderBuilderExtensionsTests.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.Extensions.DependencyInjection;
18+
using OpenTelemetry.Metrics;
19+
using Xunit;
20+
21+
namespace OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests;
22+
23+
public sealed class PrometheusExporterMeterProviderBuilderExtensionsTests
24+
{
25+
[Fact]
26+
public void TestAddPrometheusExporter_NamedOptions()
27+
{
28+
int defaultExporterOptionsConfigureOptionsInvocations = 0;
29+
int namedExporterOptionsConfigureOptionsInvocations = 0;
30+
31+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
32+
.ConfigureServices(services =>
33+
{
34+
services.Configure<PrometheusExporterOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
35+
36+
services.Configure<PrometheusExporterOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
37+
})
38+
.AddPrometheusExporter()
39+
.AddPrometheusExporter("Exporter2", o => { })
40+
.Build();
41+
42+
Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations);
43+
Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations);
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <copyright file="PrometheusHttpListenerMeterProviderBuilderExtensionsTests.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.Extensions.DependencyInjection;
18+
using OpenTelemetry.Metrics;
19+
using Xunit;
20+
21+
namespace OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests;
22+
23+
public sealed class PrometheusHttpListenerMeterProviderBuilderExtensionsTests
24+
{
25+
[Fact]
26+
public void TestAddPrometheusHttpListener_NamedOptions()
27+
{
28+
int defaultExporterOptionsConfigureOptionsInvocations = 0;
29+
int namedExporterOptionsConfigureOptionsInvocations = 0;
30+
31+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
32+
.ConfigureServices(services =>
33+
{
34+
services.Configure<PrometheusHttpListenerOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
35+
36+
services.Configure<PrometheusHttpListenerOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
37+
})
38+
.AddPrometheusHttpListener()
39+
.AddPrometheusHttpListener("Exporter2", o => { })
40+
.Build();
41+
42+
Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations);
43+
Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations);
44+
}
45+
}

0 commit comments

Comments
 (0)