|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Microsoft.Agents.A365.Observability.Tests.Tracing.Scopes; |
| 5 | + |
| 6 | +using System.Diagnostics; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.Agents.A365.Observability.Runtime.Tracing.Contracts; |
| 9 | +using Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes; |
| 10 | + |
| 11 | +[TestClass] |
| 12 | +public sealed class SpanLinksTest : ActivityTest |
| 13 | +{ |
| 14 | + private static readonly ActivityLink[] SampleLinks = new[] |
| 15 | + { |
| 16 | + new ActivityLink( |
| 17 | + new ActivityContext( |
| 18 | + ActivityTraceId.CreateFromString("0aa4621e5ae09963a3de354f3d18aa65"), |
| 19 | + ActivitySpanId.CreateFromString("c1aaa519600b1bf0"), |
| 20 | + ActivityTraceFlags.Recorded)), |
| 21 | + new ActivityLink( |
| 22 | + new ActivityContext( |
| 23 | + ActivityTraceId.CreateFromString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 24 | + ActivitySpanId.CreateFromString("aaaaaaaaaaaaaaaa"), |
| 25 | + ActivityTraceFlags.None), |
| 26 | + new ActivityTagsCollection(new[] { new KeyValuePair<string, object?>("link.reason", "retry") })), |
| 27 | + }; |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void ExecuteToolScope_RecordsSpanLinks_WithFullContextAndAttributes() |
| 31 | + { |
| 32 | + var activity = ListenForActivity(() => |
| 33 | + { |
| 34 | + using var scope = ExecuteToolScope.Start( |
| 35 | + Util.GetDefaultRequest(), |
| 36 | + new ToolCallDetails("my-tool", "args"), |
| 37 | + Util.GetAgentDetails(), |
| 38 | + spanDetails: new SpanDetails(spanLinks: SampleLinks)); |
| 39 | + }); |
| 40 | + |
| 41 | + var links = activity.Links.ToList(); |
| 42 | + links.Should().HaveCount(2); |
| 43 | + links[0].Context.TraceId.ToHexString().Should().Be("0aa4621e5ae09963a3de354f3d18aa65"); |
| 44 | + links[0].Context.SpanId.ToHexString().Should().Be("c1aaa519600b1bf0"); |
| 45 | + links[0].Context.TraceFlags.Should().Be(ActivityTraceFlags.Recorded); |
| 46 | + links[1].Context.TraceId.ToHexString().Should().Be("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); |
| 47 | + links[1].Context.SpanId.ToHexString().Should().Be("aaaaaaaaaaaaaaaa"); |
| 48 | + links[1].Tags.Should().Contain(new KeyValuePair<string, object?>("link.reason", "retry")); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void ExecuteToolScope_HasEmptyLinks_WhenSpanLinksOmitted() |
| 53 | + { |
| 54 | + var activity = ListenForActivity(() => |
| 55 | + { |
| 56 | + using var scope = ExecuteToolScope.Start( |
| 57 | + Util.GetDefaultRequest(), |
| 58 | + new ToolCallDetails("my-tool", "args"), |
| 59 | + Util.GetAgentDetails()); |
| 60 | + }); |
| 61 | + |
| 62 | + activity.Links.Should().BeEmpty(); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public void InvokeAgentScope_ForwardsSpanLinks() |
| 67 | + { |
| 68 | + var activity = ListenForActivity(() => |
| 69 | + { |
| 70 | + using var scope = InvokeAgentScope.Start( |
| 71 | + Util.GetDefaultRequest(), |
| 72 | + ScopeDetails, |
| 73 | + TestAgentDetails, |
| 74 | + spanDetails: new SpanDetails(spanLinks: SampleLinks)); |
| 75 | + }); |
| 76 | + |
| 77 | + var links = activity.Links.ToList(); |
| 78 | + links.Should().HaveCount(2); |
| 79 | + links[0].Context.TraceId.ToHexString().Should().Be("0aa4621e5ae09963a3de354f3d18aa65"); |
| 80 | + links[0].Context.SpanId.ToHexString().Should().Be("c1aaa519600b1bf0"); |
| 81 | + links[0].Context.TraceFlags.Should().Be(ActivityTraceFlags.Recorded); |
| 82 | + links[1].Context.TraceId.ToHexString().Should().Be("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); |
| 83 | + links[1].Context.SpanId.ToHexString().Should().Be("aaaaaaaaaaaaaaaa"); |
| 84 | + links[1].Context.TraceFlags.Should().Be(ActivityTraceFlags.None); |
| 85 | + links[1].Tags.Should().Contain(new KeyValuePair<string, object?>("link.reason", "retry")); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void InferenceScope_ForwardsSpanLinks() |
| 90 | + { |
| 91 | + var details = new InferenceCallDetails( |
| 92 | + InferenceOperationType.Chat, "gpt-4", "openai"); |
| 93 | + |
| 94 | + var activity = ListenForActivity(() => |
| 95 | + { |
| 96 | + using var scope = InferenceScope.Start( |
| 97 | + Util.GetDefaultRequest(), |
| 98 | + details, |
| 99 | + Util.GetAgentDetails(), |
| 100 | + spanDetails: new SpanDetails(spanLinks: SampleLinks)); |
| 101 | + }); |
| 102 | + |
| 103 | + var links = activity.Links.ToList(); |
| 104 | + links.Should().HaveCount(2); |
| 105 | + links[0].Context.TraceId.ToHexString().Should().Be("0aa4621e5ae09963a3de354f3d18aa65"); |
| 106 | + links[0].Context.SpanId.ToHexString().Should().Be("c1aaa519600b1bf0"); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void OutputScope_ForwardsSpanLinks() |
| 111 | + { |
| 112 | + var response = new Response(new[] { "hello" }); |
| 113 | + |
| 114 | + var activity = ListenForActivity(() => |
| 115 | + { |
| 116 | + using var scope = OutputScope.Start( |
| 117 | + Util.GetDefaultRequest(), |
| 118 | + response, |
| 119 | + Util.GetAgentDetails(), |
| 120 | + spanDetails: new SpanDetails(spanLinks: SampleLinks)); |
| 121 | + }); |
| 122 | + |
| 123 | + var links = activity.Links.ToList(); |
| 124 | + links.Should().HaveCount(2); |
| 125 | + links[0].Context.TraceId.ToHexString().Should().Be("0aa4621e5ae09963a3de354f3d18aa65"); |
| 126 | + links[0].Context.SpanId.ToHexString().Should().Be("c1aaa519600b1bf0"); |
| 127 | + } |
| 128 | + |
| 129 | + [TestMethod] |
| 130 | + public void SpanLinks_PreservesTypedAttributes() |
| 131 | + { |
| 132 | + var linksWithAttrs = new[] |
| 133 | + { |
| 134 | + new ActivityLink( |
| 135 | + new ActivityContext( |
| 136 | + ActivityTraceId.CreateFromString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 137 | + ActivitySpanId.CreateFromString("bbbbbbbbbbbbbbbb"), |
| 138 | + ActivityTraceFlags.Recorded), |
| 139 | + new ActivityTagsCollection(new[] |
| 140 | + { |
| 141 | + new KeyValuePair<string, object?>("link.type", "causal"), |
| 142 | + new KeyValuePair<string, object?>("link.index", 0), |
| 143 | + })), |
| 144 | + }; |
| 145 | + |
| 146 | + var activity = ListenForActivity(() => |
| 147 | + { |
| 148 | + using var scope = InvokeAgentScope.Start( |
| 149 | + Util.GetDefaultRequest(), |
| 150 | + ScopeDetails, |
| 151 | + new AgentDetails("attr-agent"), |
| 152 | + spanDetails: new SpanDetails(spanLinks: linksWithAttrs)); |
| 153 | + }); |
| 154 | + |
| 155 | + var links = activity.Links.ToList(); |
| 156 | + links.Should().HaveCount(1); |
| 157 | + links[0].Tags.Should().Contain(new KeyValuePair<string, object?>("link.type", "causal")); |
| 158 | + links[0].Tags.Should().Contain(new KeyValuePair<string, object?>("link.index", 0)); |
| 159 | + } |
| 160 | +} |
0 commit comments