Beispiel #1
0
        public void CallProperty()
        {
            // Given
            var target = new Mock <IDummyTarget>(MockBehavior.Loose);

            target.Setup(e => e.Database).Returns("mydb");
            target.Setup(e => e.Container).Returns("mycontainer");

            var activity = new Mock <IActivity>(MockBehavior.Loose);

            var activitySource = new Mock <IActivitySource>(MockBehavior.Loose);

            activitySource
            .Setup(e => e.StartActivity(
                       It.IsAny <string>(),
                       It.IsAny <ActivityKind>(),
                       It.IsAny <ActivityContext>(),
                       It.IsAny <ActivityTagsCollection>(),
                       It.IsAny <IEnumerable <ActivityLink> >(),
                       It.IsAny <DateTimeOffset>()))
            .Returns(activity.Object);

            var options = new OpenTelemetryInterceptorOptions();

            var builder = new OpenTelemetryInterceptorBuilder()
                          .WithActivitySource(activitySource.Object)
                          .WithActivityKind(ActivityKind.Consumer)
                          .WithTypeName("CustomType")
                          .WithOptions(options)
                          .TagWith("common.tag", "common value")
                          .TagTarget <IDummyTarget>(t => new { t.Database, t.Container })
                          .TagParameter <string>("arg1", arg1 => new { Arg1 = arg1 })
                          .TagResult <int>(result => new { Result = result })
                          .TagError <FormatException>(e => new { Message = e.Message });

            var interceptor = builder.Build();

            var intercepted = interceptor.Intercept(target.Object);

            // When
            var result = intercepted.Database;

            // Then
            result.Should().Be("mydb");

            target.Verify(e => e.Database, Times.Once);

            activitySource.Verify(
                e => e.StartActivity(
                    It.IsAny <string>(),
                    It.IsAny <ActivityKind>(),
                    It.IsAny <ActivityContext>(),
                    It.IsAny <ActivityTagsCollection>(),
                    It.IsAny <IEnumerable <ActivityLink> >(),
                    It.IsAny <DateTimeOffset>()),
                Times.Never);

            activity.Verify(
                e => e.SetTag(It.IsAny <string>(), It.IsAny <object>()),
                Times.Never);

            activity.Verify(
                e => e.Stop(),
                Times.Never);
        }
Beispiel #2
0
        public async Task CallValueTaskMethod()
        {
            // Given
            var target = new Mock <IDummyTarget>(MockBehavior.Loose);

            target.Setup(e => e.Database).Returns("mydb");
            target.Setup(e => e.Container).Returns("mycontainer");
            target
            .Setup(e => e.ValueTaskMethod(It.IsAny <string>()))
            .Returns(ValueTask.FromResult(42));

            var activity = new Mock <IActivity>(MockBehavior.Loose);

            var activitySource = new Mock <IActivitySource>(MockBehavior.Loose);

            activitySource
            .Setup(e => e.StartActivity(
                       It.IsAny <string>(),
                       It.IsAny <ActivityKind>(),
                       It.IsAny <ActivityContext>(),
                       It.IsAny <ActivityTagsCollection>(),
                       It.IsAny <IEnumerable <ActivityLink> >(),
                       It.IsAny <DateTimeOffset>()))
            .Returns(activity.Object);

            var options = new OpenTelemetryInterceptorOptions();

            var builder = new OpenTelemetryInterceptorBuilder()
                          .WithActivitySource(activitySource.Object)
                          .WithActivityKind(ActivityKind.Producer)
                          .WithTypeName("CustomType")
                          .WithOptions(options)
                          .TagWith("common.tag", "common value")
                          .TagTarget <IDummyTarget>(t => new { t.Database, t.Container })
                          .TagParameter <string>("arg1", arg1 => new { Arg1 = arg1 })
                          .TagResult <int>(result => new { Result = result })
                          .TagError <FormatException>(e => new { Message = e.Message });

            var interceptor = builder.Build();

            var intercepted = interceptor.Intercept(target.Object);

            // When
            var result = await intercepted.ValueTaskMethod("value1");

            // Then
            result.Should().Be(42);

            target.Verify(e => e.ValueTaskMethod("value1"), Times.Once);

            activitySource.Verify(
                e => e.StartActivity(
                    "CustomType.ValueTaskMethod",
                    ActivityKind.Producer,
                    It.IsAny <ActivityContext>(),
                    It.Is <ActivityTagsCollection>(tags =>
                                                   tags.Count == 4 &&
                                                   tags["common.tag"] == "common value" &&
                                                   tags["Database"] == "mydb" &&
                                                   tags["Container"] == "mycontainer" &&
                                                   tags["Arg1"] == "value1"),
                    null,
                    It.IsAny <DateTimeOffset>()),
                Times.Once);

            activity.Verify(
                e => e.SetTag("Result", 42),
                Times.Once);

            activity.Verify(
                e => e.Stop(),
                Times.Once);
        }
Beispiel #3
0
        public void CallVoidMethodThrow()
        {
            // Given
            var target = new Mock <IDummyTarget>(MockBehavior.Loose);

            target.Setup(e => e.Database).Returns("mydb");
            target.Setup(e => e.Container).Returns("mycontainer");
            target
            .Setup(e => e.VoidMethod(It.IsAny <string>()))
            .Throws(new FormatException("Oops"));

            var activity = new Mock <IActivity>(MockBehavior.Loose);

            var activitySource = new Mock <IActivitySource>(MockBehavior.Loose);

            activitySource
            .Setup(e => e.StartActivity(
                       It.IsAny <string>(),
                       It.IsAny <ActivityKind>(),
                       It.IsAny <ActivityContext>(),
                       It.IsAny <ActivityTagsCollection>(),
                       It.IsAny <IEnumerable <ActivityLink> >(),
                       It.IsAny <DateTimeOffset>()))
            .Returns(activity.Object);

            var options = new OpenTelemetryInterceptorOptions();

            var builder = new OpenTelemetryInterceptorBuilder()
                          .WithActivitySource(activitySource.Object)
                          .WithActivityKind(ActivityKind.Consumer)
                          .WithTypeName("CustomType")
                          .WithOptions(options)
                          .TagWith("common.tag", "common value")
                          .TagTarget <IDummyTarget>(t => new { t.Database, t.Container })
                          .TagParameter <string>("arg1", arg1 => new { Arg1 = arg1 })
                          .TagResult <int>(result => new { Result = result })
                          .TagError <FormatException>(e => new { Message = e.Message });

            var interceptor = builder.Build();

            var intercepted = interceptor.Intercept(target.Object);

            // When
            Action action = () => intercepted.VoidMethod("value1");

            // Then
            action.Should().Throw <FormatException>();

            target.Verify(e => e.VoidMethod("value1"), Times.Once);

            activitySource.Verify(
                e => e.StartActivity(
                    "CustomType.VoidMethod",
                    ActivityKind.Consumer,
                    It.IsAny <ActivityContext>(),
                    It.Is <ActivityTagsCollection>(tags =>
                                                   tags.Count == 4 &&
                                                   tags["common.tag"] == "common value" &&
                                                   tags["Database"] == "mydb" &&
                                                   tags["Container"] == "mycontainer" &&
                                                   tags["Arg1"] == "value1"),
                    null,
                    It.IsAny <DateTimeOffset>()),
                Times.Once);

            activity.Verify(
                e => e.SetTag(It.IsAny <string>(), It.IsAny <object>()),
                Times.Exactly(3));

            activity.Verify(
                e => e.SetTag("Message", "Oops"),
                Times.Once);

            activity.Verify(
                e => e.SetTag("otel.status_code", 500),
                Times.Once);

            activity.Verify(
                e => e.SetTag("error", true),
                Times.Once);

            activity.Verify(
                e => e.Stop(),
                Times.Once);
        }