public void ProfiledCommandToSpanDataUsesCommandAsDbStatementAttribute()
        {
            var profiledCommand = new Mock <IProfiledCommand>();

            profiledCommand.Setup(m => m.Command).Returns("SET");
            var result = RedisProfilerEntryToSpanConverter.ProfiledCommandToSpanData(SpanContext.Invalid, "another name", SpanId.Invalid, profiledCommand.Object);

            Assert.Contains("db.statement", result.Attributes.AttributeMap.Keys);
            Assert.Equal(AttributeValue.StringAttributeValue("SET"), result.Attributes.AttributeMap["db.statement"]);
        }
        public void ProfiledCommandToSpanDataUsesTimestampAsStartTime()
        {
            var profiledCommand = new Mock <IProfiledCommand>();
            var now             = DateTimeOffset.Now;

            profiledCommand.Setup(m => m.CommandCreated).Returns(now.DateTime);
            var result = RedisProfilerEntryToSpanConverter.ProfiledCommandToSpanData(SpanContext.Invalid, "SET", SpanId.Invalid, profiledCommand.Object);

            Assert.Equal(Timestamp.FromMillis(now.ToUnixTimeMilliseconds()), result.StartTimestamp);
        }
        public void ShouldSampleRespectsSamplerChoice()
        {
            var m = new Mock <ISampler>();

            m.Setup(x => x.ShouldSample(It.IsAny <ISpanContext>(), It.IsAny <bool>(), It.IsAny <ITraceId>(), It.IsAny <ISpanId>(), It.IsAny <string>(), It.IsAny <IEnumerable <ISpan> >())).Returns(true);
            Assert.True(RedisProfilerEntryToSpanConverter.ShouldSample(SpanContext.Invalid, "SET", m.Object, out var context, out var parentId));

            m = new Mock <ISampler>();
            m.Setup(x => x.ShouldSample(It.IsAny <ISpanContext>(), It.IsAny <bool>(), It.IsAny <ITraceId>(), It.IsAny <ISpanId>(), It.IsAny <string>(), It.IsAny <IEnumerable <ISpan> >())).Returns(false);
            Assert.False(RedisProfilerEntryToSpanConverter.ShouldSample(SpanContext.Invalid, "SET", m.Object, out context, out parentId));
        }
        public void ProfiledCommandToSpanDataUsesFlagsForFlagsAttribute()
        {
            var profiledCommand = new Mock <IProfiledCommand>();
            var expectedFlags   = StackExchange.Redis.CommandFlags.FireAndForget | StackExchange.Redis.CommandFlags.NoRedirect;

            profiledCommand.Setup(m => m.Flags).Returns(expectedFlags);
            var result = RedisProfilerEntryToSpanConverter.ProfiledCommandToSpanData(SpanContext.Invalid, "SET", SpanId.Invalid, profiledCommand.Object);

            Assert.Contains("redis.flags", result.Attributes.AttributeMap.Keys);
            Assert.Equal(AttributeValue.StringAttributeValue("None, FireAndForget, NoRedirect"), result.Attributes.AttributeMap["redis.flags"]);
        }
        public void DrainSessionUsesCommandAsName()
        {
            var parentSpan      = BlankSpan.Instance;
            var profiledCommand = new Mock <IProfiledCommand>();
            var sampler         = new Mock <ISampler>();

            sampler.Setup(x => x.ShouldSample(It.IsAny <ISpanContext>(), It.IsAny <bool>(), It.IsAny <ITraceId>(), It.IsAny <ISpanId>(), It.IsAny <string>(), It.IsAny <IEnumerable <ISpan> >())).Returns(true);
            profiledCommand.Setup(m => m.Command).Returns("SET");
            var result = new List <ISpanData>();

            RedisProfilerEntryToSpanConverter.DrainSession(parentSpan, new IProfiledCommand[] { profiledCommand.Object }, sampler.Object, result);
            Assert.Single(result);
            Assert.Equal("SET", result[0].Name);
        }
        public void ShouldSamplePassesArgumentsToSamplerAndReturnsInContext()
        {
            var m             = new Mock <ISampler>();
            var r             = new RandomGenerator();
            var traceId       = TraceId.GenerateRandomId(r);
            var parentContext = SpanContext.Create(traceId, SpanId.GenerateRandomId(r), TraceOptions.Sampled, Tracestate.Builder.Set("a", "b").Build());

            RedisProfilerEntryToSpanConverter.ShouldSample(parentContext, "SET", m.Object, out var context, out var parentId);

            m.Verify(x => x.ShouldSample(
                         It.Is <ISpanContext>(y => y == parentContext),
                         It.Is <bool>(y => y == false),
                         It.Is <ITraceId>(y => y == traceId && y == context.TraceId),
                         It.Is <ISpanId>(y => y.IsValid && y == context.SpanId),
                         It.Is <string>(y => y == "SET"),
                         It.Is <IEnumerable <ISpan> >(y => y == null)));
        }
        public void ShouldSampleGeneratesNewTraceIdForInvalidContext()
        {
            var m = new Mock <ISampler>();

            m.Setup(x => x.ShouldSample(It.IsAny <ISpanContext>(), It.IsAny <bool>(), It.IsAny <ITraceId>(), It.IsAny <ISpanId>(), It.IsAny <string>(), It.IsAny <IEnumerable <ISpan> >())).Returns((ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable <ISpan> parentLinks) => parentContext.TraceOptions.IsSampled);

            RedisProfilerEntryToSpanConverter.ShouldSample(SpanContext.Invalid, "SET", m.Object, out var context, out var parentId);

            m.Verify(x => x.ShouldSample(
                         It.Is <ISpanContext>(y => y == SpanContext.Invalid),
                         It.Is <bool>(y => y == false),
                         It.Is <ITraceId>(y => y.IsValid && y == context.TraceId),
                         It.Is <ISpanId>(y => y.IsValid && y == context.SpanId),
                         It.Is <string>(y => y == "SET"),
                         It.Is <IEnumerable <ISpan> >(y => y == null)));

            Assert.Equal(TraceOptions.Default, context.TraceOptions);
        }
 public void ShouldSampleDoesntThrowWithoutSampler()
 {
     RedisProfilerEntryToSpanConverter.ShouldSample(SpanContext.Invalid, "SET", null, out var context, out var parentId);
 }