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

Removed Moq dependency from Instrumentation.AspNetCore.Tests #5123

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Tests;
Expand Down Expand Up @@ -205,22 +204,19 @@ public async Task CustomPropagator(bool addSampler)
var expectedTraceId = ActivityTraceId.CreateRandom();
var expectedSpanId = ActivitySpanId.CreateRandom();

var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Extract(It.IsAny<PropagationContext>(), It.IsAny<HttpRequest>(), It.IsAny<Func<HttpRequest, string, IEnumerable<string>>>())).Returns(
new PropagationContext(
new ActivityContext(
expectedTraceId,
expectedSpanId,
ActivityTraceFlags.Recorded),
default));
var propagator = new CustomTextMapPropagator
{
TraceId = expectedTraceId,
SpanId = expectedSpanId,
};

// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
Sdk.SetDefaultTextMapPropagator(propagator);
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder();

if (addSampler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand All @@ -33,8 +32,10 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\CustomTextMapPropagator.cs" Link="Includes\CustomTextMapPropagator.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\InMemoryEventListener.cs" Link="Includes\InMemoryEventListener.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\PropagationContextEventArgs.cs" Link="Includes\PropagationContextEventArgs.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestEventListener.cs" Link="Includes\TestEventListener.cs" />
</ItemGroup>

Expand Down
64 changes: 64 additions & 0 deletions test/OpenTelemetry.Tests/Shared/CustomTextMapPropagator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <copyright file="CustomTextMapPropagator.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.Diagnostics;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Tests;

internal sealed class CustomTextMapPropagator : TextMapPropagator
{
private static readonly PropagationContext DefaultPropagationContext = default;

public ActivityTraceId TraceId { get; set; }

public ActivitySpanId SpanId { get; set; }

public Action<PropagationContext> Injected { get; set; }

public override ISet<string> Fields => null;

#pragma warning disable SA1201 // Elements should appear in the correct order
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
public Dictionary<string, Func<PropagationContext, string>> InjectValues = [];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly
#pragma warning restore SA1201 // Elements should appear in the correct order

public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
{
if (this.TraceId != default && this.SpanId != default)
{
return new PropagationContext(
new ActivityContext(
this.TraceId,
this.SpanId,
ActivityTraceFlags.Recorded),
default);
}

return DefaultPropagationContext;
}

public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
{
foreach (var kv in this.InjectValues)
{
setter(carrier, kv.Key, kv.Value.Invoke(context));
}

this.Injected?.Invoke(context);
}
}
24 changes: 24 additions & 0 deletions test/OpenTelemetry.Tests/Shared/PropagationContextEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <copyright file="PropagationContextEventArgs.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 OpenTelemetry.Context.Propagation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed now that we are using Action delegate


namespace OpenTelemetry.Tests;

internal class PropagationContextEventArgs(PropagationContext context) : EventArgs
{
public PropagationContext Context { get; private set; } = context;
}
Loading