Skip to content

Commit 8a2f9c4

Browse files
committed
Merge branch 'main' into remove-moq-instrumentation-grpc-tests
2 parents a6b57f7 + f11801d commit 8a2f9c4

File tree

17 files changed

+382
-176
lines changed

17 files changed

+382
-176
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/AssemblyInfo.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@
2020
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
2121
[assembly: InternalsVisibleTo("Benchmarks, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
2222
[assembly: InternalsVisibleTo("MockOpenTelemetryCollector, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
23-
24-
// Used by Moq.
25-
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
2623
#else
2724
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests")]
2825
[assembly: InternalsVisibleTo("Benchmarks")]
2926
[assembly: InternalsVisibleTo("MockOpenTelemetryCollector")]
30-
31-
// Used by Moq.
32-
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
3327
#endif

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Implementation/ExportClient/OtlpHttpTraceExportClientTests.cs

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#if !NET6_0_OR_GREATER
1919
using System.Net.Http;
2020
#endif
21-
using Moq;
22-
using Moq.Protected;
2321
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
2422
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
2523
using OpenTelemetry.Resources;
@@ -94,51 +92,20 @@ public void SendExportRequest_ExportTraceServiceRequest_SendsCorrectHttpRequest(
9492
Headers = $"{header1.Name}={header1.Value}, {header2.Name} = {header2.Value}",
9593
};
9694

97-
var httpHandlerMock = new Mock<HttpMessageHandler>();
95+
var testHttpHandler = new TestHttpMessageHandler();
9896

99-
HttpRequestMessage httpRequest = null;
10097
var httpRequestContent = Array.Empty<byte>();
10198

102-
httpHandlerMock.Protected()
103-
#if NET6_0_OR_GREATER
104-
.Setup<HttpResponseMessage>("Send", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
105-
.Returns((HttpRequestMessage request, CancellationToken token) =>
106-
{
107-
return new HttpResponseMessage();
108-
})
109-
.Callback<HttpRequestMessage, CancellationToken>(async (r, ct) =>
110-
{
111-
httpRequest = r;
112-
113-
// We have to capture content as it can't be accessed after request is disposed inside of SendExportRequest method
114-
httpRequestContent = await r.Content.ReadAsByteArrayAsync(ct);
115-
})
116-
#else
117-
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
118-
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
119-
{
120-
return new HttpResponseMessage();
121-
})
122-
.Callback<HttpRequestMessage, CancellationToken>(async (r, ct) =>
123-
{
124-
httpRequest = r;
125-
126-
// We have to capture content as it can't be accessed after request is disposed inside of SendExportRequest method
127-
httpRequestContent = await r.Content.ReadAsByteArrayAsync();
128-
})
129-
#endif
130-
.Verifiable();
131-
132-
var exportClient = new OtlpHttpTraceExportClient(options, new HttpClient(httpHandlerMock.Object));
99+
var exportClient = new OtlpHttpTraceExportClient(options, new HttpClient(testHttpHandler));
133100

134101
var resourceBuilder = ResourceBuilder.CreateEmpty();
135102
if (includeServiceNameInResource)
136103
{
137104
resourceBuilder.AddAttributes(
138105
new List<KeyValuePair<string, object>>
139106
{
140-
new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, "service_name"),
141-
new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceNamespace, "ns_1"),
107+
new(ResourceSemanticConventions.AttributeServiceName, "service_name"),
108+
new(ResourceSemanticConventions.AttributeServiceNamespace, "ns_1"),
142109
});
143110
}
144111

@@ -166,7 +133,7 @@ public void SendExportRequest_ExportTraceServiceRequest_SendsCorrectHttpRequest(
166133

167134
processor.Shutdown();
168135

169-
var batch = new Batch<Activity>(exportedItems.ToArray(), exportedItems.Count);
136+
var batch = new Batch<Activity>([.. exportedItems], exportedItems.Count);
170137
RunTest(batch);
171138

172139
void RunTest(Batch<Activity> batch)
@@ -178,6 +145,8 @@ void RunTest(Batch<Activity> batch)
178145
// Act
179146
var result = exportClient.SendExportRequest(request);
180147

148+
var httpRequest = testHttpHandler.HttpRequestMessage;
149+
181150
// Assert
182151
Assert.True(result);
183152
Assert.NotNull(httpRequest);
@@ -192,11 +161,11 @@ void RunTest(Batch<Activity> batch)
192161
Assert.Contains(httpRequest.Headers, entry => entry.Key == OtlpExporterOptions.StandardHeaders[i].Key && entry.Value.First() == OtlpExporterOptions.StandardHeaders[i].Value);
193162
}
194163

195-
Assert.NotNull(httpRequest.Content);
164+
Assert.NotNull(testHttpHandler.HttpRequestContent);
196165
Assert.IsType<OtlpHttpTraceExportClient.ExportRequestContent>(httpRequest.Content);
197166
Assert.Contains(httpRequest.Content.Headers, h => h.Key == "Content-Type" && h.Value.First() == OtlpHttpTraceExportClient.MediaContentType);
198167

199-
var exportTraceRequest = OtlpCollector.ExportTraceServiceRequest.Parser.ParseFrom(httpRequestContent);
168+
var exportTraceRequest = OtlpCollector.ExportTraceServiceRequest.Parser.ParseFrom(testHttpHandler.HttpRequestContent);
200169
Assert.NotNull(exportTraceRequest);
201170
Assert.Single(exportTraceRequest.ResourceSpans);
202171

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<PackageReference Include="Microsoft.Extensions.Hosting" />
1313
<PackageReference Include="Microsoft.Extensions.Http" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" />
15-
<PackageReference Include="Moq" />
1615
<PackageReference Include="xunit" />
1716
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All">
1817
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
using Microsoft.Extensions.Hosting;
2323
using Microsoft.Extensions.Logging;
2424
using Microsoft.Extensions.Options;
25-
using Moq;
2625
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
27-
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
2826
using OpenTelemetry.Internal;
2927
using OpenTelemetry.Logs;
3028
using OpenTelemetry.Resources;
@@ -589,37 +587,34 @@ public void CheckToOtlpLogRecordRespectsAttributeLimits()
589587
public void Export_WhenExportClientIsProvidedInCtor_UsesProvidedExportClient()
590588
{
591589
// Arrange.
592-
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
590+
var testExportClient = new TestExportClient<OtlpCollector.ExportLogsServiceRequest>();
593591
var emptyLogRecords = Array.Empty<LogRecord>();
594592
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
595593
var sut = new OtlpLogExporter(
596594
new OtlpExporterOptions(),
597595
new SdkLimitOptions(),
598596
new ExperimentalOptions(),
599-
fakeExportClient.Object);
597+
testExportClient);
600598

601599
// Act.
602-
var result = sut.Export(emptyBatch);
600+
sut.Export(emptyBatch);
603601

604602
// Assert.
605-
fakeExportClient.Verify(x => x.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default), Times.Once());
603+
Assert.True(testExportClient.SendExportRequestCalled);
606604
}
607605

608606
[Fact]
609607
public void Export_WhenExportClientThrowsException_ReturnsExportResultFailure()
610608
{
611609
// Arrange.
612-
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
610+
var testExportClient = new TestExportClient<OtlpCollector.ExportLogsServiceRequest>(throwException: true);
613611
var emptyLogRecords = Array.Empty<LogRecord>();
614612
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
615-
fakeExportClient
616-
.Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default))
617-
.Throws(new Exception("Test Exception"));
618613
var sut = new OtlpLogExporter(
619614
new OtlpExporterOptions(),
620615
new SdkLimitOptions(),
621616
new ExperimentalOptions(),
622-
fakeExportClient.Object);
617+
testExportClient);
623618

624619
// Act.
625620
var result = sut.Export(emptyBatch);
@@ -632,17 +627,14 @@ public void Export_WhenExportClientThrowsException_ReturnsExportResultFailure()
632627
public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess()
633628
{
634629
// Arrange.
635-
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
630+
var testExportClient = new TestExportClient<OtlpCollector.ExportLogsServiceRequest>();
636631
var emptyLogRecords = Array.Empty<LogRecord>();
637632
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
638-
fakeExportClient
639-
.Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default))
640-
.Returns(true);
641633
var sut = new OtlpLogExporter(
642634
new OtlpExporterOptions(),
643635
new SdkLimitOptions(),
644636
new ExperimentalOptions(),
645-
fakeExportClient.Object);
637+
testExportClient);
646638

647639
// Act.
648640
var result = sut.Export(emptyBatch);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
using System.Diagnostics;
1818
using Google.Protobuf.Collections;
1919
using Microsoft.Extensions.DependencyInjection;
20-
using Moq;
2120
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
22-
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
2321
using OpenTelemetry.Metrics;
2422
using OpenTelemetry.Resources;
2523
using OpenTelemetry.Tests;
@@ -642,13 +640,13 @@ public void UseOpenTelemetryProtocolActivityExporterWithCustomActivityProcessor(
642640
[Fact]
643641
public void Shutdown_ClientShutdownIsCalled()
644642
{
645-
var exportClientMock = new Mock<IExportClient<OtlpCollector.ExportTraceServiceRequest>>();
643+
var exportClientMock = new TestExportClient<OtlpCollector.ExportTraceServiceRequest>();
646644

647-
var exporter = new OtlpTraceExporter(new OtlpExporterOptions(), DefaultSdkLimitOptions, exportClientMock.Object);
645+
var exporter = new OtlpTraceExporter(new OtlpExporterOptions(), DefaultSdkLimitOptions, exportClientMock);
648646

649-
var result = exporter.Shutdown();
647+
exporter.Shutdown();
650648

651-
exportClientMock.Verify(m => m.Shutdown(It.IsAny<int>()), Times.Once());
649+
Assert.True(exportClientMock.ShutdownCalled);
652650
}
653651

654652
[Fact]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <copyright file="TestExportClient.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 OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
18+
19+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
20+
21+
internal class TestExportClient<T>(bool throwException = false) : IExportClient<T>
22+
{
23+
public bool SendExportRequestCalled { get; private set; }
24+
25+
public bool ShutdownCalled { get; private set; }
26+
27+
public bool ThrowException { get; set; } = throwException;
28+
29+
public bool SendExportRequest(T request, CancellationToken cancellationToken = default)
30+
{
31+
if (this.ThrowException)
32+
{
33+
throw new Exception("Exception thrown from SendExportRequest");
34+
}
35+
36+
this.SendExportRequestCalled = true;
37+
return true;
38+
}
39+
40+
public bool Shutdown(int timeoutMilliseconds)
41+
{
42+
this.ShutdownCalled = true;
43+
return true;
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="TestHttpMessageHandler.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+
#if !NET6_0_OR_GREATER
18+
using System.Net.Http;
19+
#endif
20+
21+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
22+
23+
internal class TestHttpMessageHandler : HttpMessageHandler
24+
{
25+
public HttpRequestMessage HttpRequestMessage { get; private set; }
26+
27+
public byte[] HttpRequestContent { get; private set; }
28+
29+
public virtual HttpResponseMessage InternalSend(HttpRequestMessage request, CancellationToken cancellationToken)
30+
{
31+
this.HttpRequestMessage = request;
32+
this.HttpRequestContent = request.Content.ReadAsByteArrayAsync().Result;
33+
return new HttpResponseMessage();
34+
}
35+
36+
#if NET6_0_OR_GREATER
37+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
38+
{
39+
return this.InternalSend(request, cancellationToken);
40+
}
41+
#endif
42+
43+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
44+
{
45+
return Task.FromResult(this.InternalSend(request, cancellationToken));
46+
}
47+
}

0 commit comments

Comments
 (0)