Skip to content

Commit

Permalink
Remove Moq from Instrumentation.StackExchangeRedis.Tests (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson authored Dec 11, 2023
1 parent 5efd368 commit dc70f42
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using System;
using System.Diagnostics;
using System.Net;
using OpenTelemetry.Instrumentation.StackExchangeRedis.Tests;

#if !NETFRAMEWORK
using System.Net.Sockets;
#endif
using Moq;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using StackExchange.Redis;
using StackExchange.Redis.Profiling;
using Xunit;

namespace OpenTelemetry.Instrumentation.StackExchangeRedis.Implementation;
Expand Down Expand Up @@ -48,11 +48,9 @@ public void Dispose()
public void ProfilerCommandToActivity_UsesCommandAsName()
{
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
profiledCommand.Setup(m => m.Command).Returns("SET");
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.Equal("SET", result.DisplayName);
Expand All @@ -63,10 +61,9 @@ public void ProfilerCommandToActivity_UsesTimestampAsStartTime()
{
var now = DateTimeOffset.Now;
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(now.DateTime);
var profiledCommand = new TestProfiledCommand(now.DateTime);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.Equal(now, result.StartTimeUtc);
Expand All @@ -76,10 +73,9 @@ public void ProfilerCommandToActivity_UsesTimestampAsStartTime()
public void ProfilerCommandToActivity_SetsDbTypeAttributeAsRedis()
{
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeDbSystem));
Expand All @@ -90,11 +86,9 @@ public void ProfilerCommandToActivity_SetsDbTypeAttributeAsRedis()
public void ProfilerCommandToActivity_UsesCommandAsDbStatementAttribute()
{
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
profiledCommand.Setup(m => m.Command).Returns("SET");
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeDbStatement));
Expand All @@ -105,13 +99,9 @@ public void ProfilerCommandToActivity_UsesCommandAsDbStatementAttribute()
public void ProfilerCommandToActivity_UsesFlagsForFlagsAttribute()
{
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
var expectedFlags = CommandFlags.FireAndForget |
CommandFlags.NoRedirect;
profiledCommand.Setup(m => m.Flags).Returns(expectedFlags);
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow, CommandFlags.FireAndForget | CommandFlags.NoRedirect);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(StackExchangeRedisConnectionInstrumentation.RedisFlagsKeyName));
Expand All @@ -131,11 +121,9 @@ public void ProfilerCommandToActivity_UsesIpEndPointAsEndPoint()

var activity = new Activity("redis-profiler");
IPEndPoint ipLocalEndPoint = new IPEndPoint(address, port);
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
profiledCommand.Setup(m => m.EndPoint).Returns(ipLocalEndPoint);
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow, ipLocalEndPoint);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerIp));
Expand All @@ -150,11 +138,9 @@ public void ProfilerCommandToActivity_UsesDnsEndPointAsEndPoint()
var dnsEndPoint = new DnsEndPoint("https://opentelemetry.io/", 443);

var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
profiledCommand.Setup(m => m.EndPoint).Returns(dnsEndPoint);
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow, dnsEndPoint);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerName));
Expand All @@ -169,11 +155,9 @@ public void ProfilerCommandToActivity_UsesOtherEndPointAsEndPoint()
{
var unixEndPoint = new UnixDomainSocketEndPoint("https://opentelemetry.io/");
var activity = new Activity("redis-profiler");
var profiledCommand = new Mock<IProfiledCommand>();
profiledCommand.Setup(m => m.CommandCreated).Returns(DateTime.UtcNow);
profiledCommand.Setup(m => m.EndPoint).Returns(unixEndPoint);
var profiledCommand = new TestProfiledCommand(DateTime.UtcNow, unixEndPoint);

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributePeerService));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="Implementation\SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\TestSampler.cs" Link="TestSampler.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\TestSampler.cs" Link="Includes\TestSampler.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.StackExchangeRedis\OpenTelemetry.Instrumentation.StackExchangeRedis.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="$(OpenTelemetryCoreLatestVersion)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsOptionsPkgVer)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using StackExchange.Redis;
Expand Down Expand Up @@ -45,10 +45,10 @@ public void SuccessfulCommandTestWithKey(string value)
var db = connection.GetDatabase();
db.KeyDelete("key1");

var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
var sampler = new TestSampler();
using (Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.SetSampler(sampler)
.AddRedisInstrumentation(connection, c => c.SetVerboseDatabaseStatements = true)
.Build())
Expand All @@ -72,15 +72,14 @@ public void SuccessfulCommandTestWithKey(string value)

// Disposing SDK should flush the Redis profiling session immediately.

Assert.Equal(11, activityProcessor.Invocations.Count);
Assert.Equal(4, exportedItems.Count);

var scriptActivity = (Activity)activityProcessor.Invocations[1].Arguments[0];
Assert.Equal("EVAL", scriptActivity.DisplayName);
Assert.Equal("EVAL redis.call('set', ARGV[1], ARGV[2])", scriptActivity.GetTagValue(SemanticConventions.AttributeDbStatement));
Assert.Equal("EVAL", exportedItems[0].DisplayName);
Assert.Equal("EVAL redis.call('set', ARGV[1], ARGV[2])", exportedItems[0].GetTagValue(SemanticConventions.AttributeDbStatement));

VerifyActivityData((Activity)activityProcessor.Invocations[3].Arguments[0], false, connection.GetEndPoints()[0], true);
VerifyActivityData((Activity)activityProcessor.Invocations[5].Arguments[0], true, connection.GetEndPoints()[0], true);
VerifyActivityData((Activity)activityProcessor.Invocations[7].Arguments[0], false, connection.GetEndPoints()[0], true);
VerifyActivityData(exportedItems[1], false, connection.GetEndPoints()[0], true);
VerifyActivityData(exportedItems[2], true, connection.GetEndPoints()[0], true);
VerifyActivityData(exportedItems[3], false, connection.GetEndPoints()[0], true);
VerifySamplingParameters(sampler.LatestSamplingParameters);
}

Expand All @@ -96,7 +95,7 @@ public void SuccessfulCommandTest(string value)
connectionOptions.EndPoints.Add(RedisEndPoint);

ConnectionMultiplexer? connection = null;
var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
var sampler = new TestSampler();
using (Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
Expand All @@ -106,7 +105,7 @@ public void SuccessfulCommandTest(string value)
return connection = ConnectionMultiplexer.Connect(connectionOptions);
});
})
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.SetSampler(sampler)
.AddRedisInstrumentation(c => c.SetVerboseDatabaseStatements = false)
.Build())
Expand All @@ -127,10 +126,10 @@ public void SuccessfulCommandTest(string value)

// Disposing SDK should flush the Redis profiling session immediately.

Assert.Equal(7, activityProcessor.Invocations.Count);
Assert.Equal(2, exportedItems.Count);

VerifyActivityData((Activity)activityProcessor.Invocations[1].Arguments[0], true, connection.GetEndPoints()[0], false);
VerifyActivityData((Activity)activityProcessor.Invocations[3].Arguments[0], false, connection.GetEndPoints()[0], false);
VerifyActivityData(exportedItems[0], true, connection.GetEndPoints()[0], false);
VerifyActivityData(exportedItems[1], false, connection.GetEndPoints()[0], false);
VerifySamplingParameters(sampler.LatestSamplingParameters);
}

Expand Down Expand Up @@ -169,11 +168,11 @@ public void CanEnrichActivityFromCommand(string value)
connectionOptions.EndPoints.Add(RedisEndPoint);
using var connection = ConnectionMultiplexer.Connect(connectionOptions);

var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
var sampler = new TestSampler();

var builder = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.SetSampler(sampler)
.AddRedisInstrumentation(c => c.Enrich = (activity, command) =>
{
Expand Down Expand Up @@ -204,12 +203,10 @@ public void CanEnrichActivityFromCommand(string value)

// Disposing SDK should flush the Redis profiling session immediately.

Assert.Equal(7, activityProcessor.Invocations.Count);
Assert.Equal(2, exportedItems.Count);

var setActivity = (Activity)activityProcessor.Invocations[1].Arguments[0];
Assert.Equal(true, setActivity.GetTagValue("is_fast"));
var getActivity = (Activity)activityProcessor.Invocations[3].Arguments[0];
Assert.Equal(true, getActivity.GetTagValue("is_fast"));
Assert.Equal(true, exportedItems[0].GetTagValue("is_fast"));
Assert.Equal(true, exportedItems[1].GetTagValue("is_fast"));
}

[Fact]
Expand Down Expand Up @@ -348,12 +345,9 @@ public void StackExchangeRedis_StackExchangeRedisInstrumentation_Test()
connectionOptions.EndPoints.Add("localhost");

using var connection = ConnectionMultiplexer.Connect(connectionOptions);

var activityProcessor = new Mock<BaseProcessor<Activity>>();
var sampler = new TestSampler();

var builder = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.SetSampler(sampler)
.AddRedisInstrumentation(c => c.Enrich = (activity, command) =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Net;
using StackExchange.Redis;
using StackExchange.Redis.Profiling;

namespace OpenTelemetry.Instrumentation.StackExchangeRedis.Tests;

internal class TestProfiledCommand(DateTime commandCreated) : IProfiledCommand
{
private readonly DateTime commandCreated = commandCreated;
private readonly CommandFlags flags = CommandFlags.None;
private readonly EndPoint endPoint = new IPEndPoint(0, 0);

public TestProfiledCommand(DateTime commandCreated, CommandFlags flags)
: this(commandCreated)
{
this.flags = flags;
}

public TestProfiledCommand(DateTime commandCreated, EndPoint endpoint)
: this(commandCreated)
{
this.endPoint = endpoint;
}

public EndPoint EndPoint => this.endPoint;

public int Db => 0;

public string Command => "SET";

public CommandFlags Flags => this.flags;

public DateTime CommandCreated => this.commandCreated;

public TimeSpan CreationToEnqueued => default;

public TimeSpan EnqueuedToSending => default;

public TimeSpan SentToResponse => default;

public TimeSpan ResponseToCompletion => default;

public TimeSpan ElapsedTime => default;

public IProfiledCommand RetransmissionOf => throw new NotImplementedException();

public RetransmissionReasonType? RetransmissionReason => throw new NotImplementedException();
}

0 comments on commit dc70f42

Please sign in to comment.