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

Remove Moq dependency #5106

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,50 @@
// <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 OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Instrumentation.Http.Tests;

internal sealed class CustomTextMapPropagator : TextMapPropagator
{
private static readonly PropagationContext DefaultPropagationContext = default;
#pragma warning disable SA1010
private readonly Dictionary<string, Func<PropagationContext, string>> values = [];

public event EventHandler<PropagationContextEventArgs> Injected;

public override ISet<string> Fields => null;

public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
{
return DefaultPropagationContext;
}

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

this.Injected?.Invoke(this, new PropagationContextEventArgs(context));
}

internal void Add(string key, Func<PropagationContext, string> func)
{
this.values.Add(key, func);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
// </copyright>

using System.Diagnostics;

#if NETFRAMEWORK
using System.Net;
using System.Net.Http;
#endif
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Metrics;
Expand Down Expand Up @@ -224,14 +223,6 @@ public async Task InjectsHeadersAsync(bool shouldEnrich)
[Fact]
public async Task InjectsHeadersAsync_CustomFormat()
{
var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, message, action) =>
{
action(message, "custom_traceparent", $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
action(message, "custom_tracestate", Activity.Current.TraceStateString);
});

var exportedItems = new List<Activity>();

using var request = new HttpRequestMessage
Expand All @@ -246,7 +237,11 @@ public async Task InjectsHeadersAsync_CustomFormat()
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;

Sdk.SetDefaultTextMapPropagator(propagator.Object);
var propagator = new CustomTextMapPropagator();
propagator.Add("custom_traceParent", context => $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
propagator.Add("custom_traceState", context => Activity.Current.TraceStateString);

Sdk.SetDefaultTextMapPropagator(propagator);

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
Expand All @@ -271,13 +266,13 @@ public async Task InjectsHeadersAsync_CustomFormat()
// not the HttpRequestMessage passed to HttpClient.
Assert.Empty(request.Headers);
#else
Assert.True(request.Headers.TryGetValues("custom_traceparent", out var traceparents));
Assert.True(request.Headers.TryGetValues("custom_tracestate", out var tracestates));
Assert.Single(traceparents);
Assert.Single(tracestates);
Assert.True(request.Headers.TryGetValues("custom_traceParent", out var traceParents));
Assert.True(request.Headers.TryGetValues("custom_traceState", out var traceStates));
Assert.Single(traceParents);
Assert.Single(traceStates);

Assert.Equal($"00/{activity.Context.TraceId}/{activity.Context.SpanId}/01", traceparents.Single());
Assert.Equal("k1=v1,k2=v2", tracestates.Single());
Assert.Equal($"00/{activity.Context.TraceId}/{activity.Context.SpanId}/01", traceParents.Single());
Assert.Equal("k1=v1,k2=v2", traceStates.Single());
#endif

Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
Expand All @@ -292,13 +287,9 @@ public async Task RespectsSuppress()
{
try
{
var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, message, action) =>
{
action(message, "custom_traceparent", $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
action(message, "custom_tracestate", Activity.Current.TraceStateString);
});
var propagator = new CustomTextMapPropagator();
propagator.Add("custom_traceParent", context => $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
propagator.Add("custom_traceState", context => Activity.Current.TraceStateString);

var exportedItems = new List<Activity>();

Expand All @@ -314,7 +305,7 @@ public async Task RespectsSuppress()
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;

Sdk.SetDefaultTextMapPropagator(propagator.Object);
Sdk.SetDefaultTextMapPropagator(propagator);

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
Expand All @@ -331,8 +322,8 @@ public async Task RespectsSuppress()
// If suppressed, activity is not emitted and
// propagation is also not performed.
Assert.Empty(exportedItems);
Assert.False(request.Headers.Contains("custom_traceparent"));
Assert.False(request.Headers.Contains("custom_tracestate"));
Assert.False(request.Headers.Contains("custom_traceParent"));
Assert.False(request.Headers.Contains("custom_traceState"));
}
finally
{
Expand Down Expand Up @@ -368,14 +359,14 @@ public async Task ExportsSpansCreatedForRetries()
// number of exported spans should be 3(maxRetries)
Assert.Equal(maxRetries, exportedItems.Count);

var spanid1 = exportedItems[0].SpanId;
var spanid2 = exportedItems[1].SpanId;
var spanid3 = exportedItems[2].SpanId;
var spanId1 = exportedItems[0].SpanId;
var spanId2 = exportedItems[1].SpanId;
var spanId3 = exportedItems[2].SpanId;

// Validate span ids are different
Assert.NotEqual(spanid1, spanid2);
Assert.NotEqual(spanid3, spanid1);
Assert.NotEqual(spanid2, spanid3);
Assert.NotEqual(spanId1, spanId2);
Assert.NotEqual(spanId3, spanId1);
Assert.NotEqual(spanId2, spanId3);
}

[Theory]
Expand Down Expand Up @@ -685,27 +676,15 @@ public async Task CustomPropagatorCalled(bool sample, bool createParentActivity)
ActivityContext parentContext = default;
ActivityContext contextFromPropagator = default;

var propagator = new Mock<TextMapPropagator>();

#if NETFRAMEWORK
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpWebRequest>(), It.IsAny<Action<HttpWebRequest, string, string>>()))
.Callback<PropagationContext, HttpWebRequest, Action<HttpWebRequest, string, string>>((context, carrier, setter) =>
{
contextFromPropagator = context.ActivityContext;

setter(carrier, "custom_traceparent", $"00/{contextFromPropagator.TraceId}/{contextFromPropagator.SpanId}/01");
setter(carrier, "custom_tracestate", contextFromPropagator.TraceState);
});
#else
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, carrier, setter) =>
{
contextFromPropagator = context.ActivityContext;
void Propagator_Injected(object sender, PropagationContextEventArgs e)
{
contextFromPropagator = e.Context.ActivityContext;
}

setter(carrier, "custom_traceparent", $"00/{contextFromPropagator.TraceId}/{contextFromPropagator.SpanId}/01");
setter(carrier, "custom_tracestate", contextFromPropagator.TraceState);
});
#endif
var propagator = new CustomTextMapPropagator();
propagator.Injected += Propagator_Injected;
propagator.Add("custom_traceParent", context => $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
propagator.Add("custom_traceState", context => Activity.Current.TraceStateString);

var exportedItems = new List<Activity>();

Expand All @@ -716,7 +695,7 @@ public async Task CustomPropagatorCalled(bool sample, bool createParentActivity)
.Build())
{
var previousDefaultTextMapPropagator = Propagators.DefaultTextMapPropagator;
Sdk.SetDefaultTextMapPropagator(propagator.Object);
Sdk.SetDefaultTextMapPropagator(propagator);

Activity parent = null;
if (createParentActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Tests;
Expand Down Expand Up @@ -220,26 +219,15 @@ public async Task CustomPropagatorCalled(bool sample, bool createParentActivity)
ActivityContext parentContext = default;
ActivityContext contextFromPropagator = default;

var propagator = new Mock<TextMapPropagator>();
#if NETFRAMEWORK
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpWebRequest>(), It.IsAny<Action<HttpWebRequest, string, string>>()))
.Callback<PropagationContext, HttpWebRequest, Action<HttpWebRequest, string, string>>((context, carrier, setter) =>
{
contextFromPropagator = context.ActivityContext;

setter(carrier, "traceparent", $"00/{contextFromPropagator.TraceId}/{contextFromPropagator.SpanId}/01");
setter(carrier, "tracestate", contextFromPropagator.TraceState);
});
#else
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, carrier, setter) =>
{
contextFromPropagator = context.ActivityContext;
void Propagator_Injected(object sender, PropagationContextEventArgs e)
{
contextFromPropagator = e.Context.ActivityContext;
}

setter(carrier, "traceparent", $"00/{contextFromPropagator.TraceId}/{contextFromPropagator.SpanId}/01");
setter(carrier, "tracestate", contextFromPropagator.TraceState);
});
#endif
var propagator = new CustomTextMapPropagator();
propagator.Injected += Propagator_Injected;
propagator.Add("custom_traceParent", context => $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
propagator.Add("custom_traceState", context => Activity.Current.TraceStateString);

var exportedItems = new List<Activity>();

Expand All @@ -250,7 +238,7 @@ public async Task CustomPropagatorCalled(bool sample, bool createParentActivity)
.Build())
{
var previousDefaultTextMapPropagator = Propagators.DefaultTextMapPropagator;
Sdk.SetDefaultTextMapPropagator(propagator.Object);
Sdk.SetDefaultTextMapPropagator(propagator);

Activity parent = null;
if (createParentActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All">
Expand Down
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;

namespace OpenTelemetry.Instrumentation.Http.Tests;

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