forked from microsoft/agent-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivityToSpanModelAdapter.cs
More file actions
188 lines (160 loc) · 5.53 KB
/
ActivityToSpanModelAdapter.cs
File metadata and controls
188 lines (160 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using ManagedCode.AgentLightning.Core.Tracing;
using OpenTelemetry.Resources;
namespace ManagedCode.AgentLightning.Core.Adapters;
/// <summary>
/// Converts <see cref="Activity"/> instances into serialized <see cref="SpanModel"/> records.
/// </summary>
public sealed class ActivityToSpanModelAdapter : OtelTraceAdapter<IReadOnlyList<SpanModel>>
{
private readonly string? _defaultRolloutId;
private readonly string? _defaultAttemptId;
private readonly int _sequenceSeed;
private readonly IComparer<Activity> _ordering;
private readonly Func<Activity, Resource?>? _resourceResolver;
public ActivityToSpanModelAdapter(
string? defaultRolloutId = null,
string? defaultAttemptId = null,
int sequenceSeed = 0,
IComparer<Activity>? ordering = null,
Func<Activity, Resource?>? resourceResolver = null)
{
_defaultRolloutId = defaultRolloutId;
_defaultAttemptId = defaultAttemptId;
_sequenceSeed = sequenceSeed < 0 ? 0 : sequenceSeed;
_ordering = ordering ?? ActivityStartComparer.Instance;
_resourceResolver = resourceResolver;
}
protected override IReadOnlyList<SpanModel> ConvertCore(IReadOnlyList<Activity> source)
{
if (source.Count == 0)
{
return Array.Empty<SpanModel>();
}
var ordered = source.Count == 1
? source
: source.OrderBy(activity => activity, _ordering).ToArray();
var result = new List<SpanModel>(ordered.Count);
var sequenceId = Math.Max(0, _sequenceSeed);
foreach (var activity in ordered)
{
sequenceId++;
var rolloutId = ResolveRolloutId(activity);
var attemptId = ResolveAttemptId(activity);
var explicitSequence = ResolveSequenceId(activity) ?? sequenceId;
var resource = _resourceResolver?.Invoke(activity);
var span = SpanModel.FromActivity(
activity,
rolloutId,
attemptId,
explicitSequence,
resource);
result.Add(span);
}
return result;
}
private string ResolveRolloutId(Activity activity)
{
return GetTagString(activity, "agentlightning.rollout_id")
?? GetTagString(activity, "rollout.id")
?? _defaultRolloutId
?? "rollout-unknown";
}
private string ResolveAttemptId(Activity activity)
{
return GetTagString(activity, "agentlightning.attempt_id")
?? GetTagString(activity, "attempt.id")
?? _defaultAttemptId
?? "attempt-unknown";
}
private static int? ResolveSequenceId(Activity activity)
{
var sequenceValue = GetTag(activity, "agentlightning.sequence_id")
?? GetTag(activity, "gen_ai.sequence_id");
if (sequenceValue is null)
{
return null;
}
return TryConvertToInt(sequenceValue, out var sequenceId) ? sequenceId : null;
}
private static object? GetTag(Activity activity, string name)
{
#if NET9_0_OR_GREATER
return activity.GetTagItem(name);
#else
return activity.TagObjects?.FirstOrDefault(tag => string.Equals(tag.Key, name, StringComparison.Ordinal)).Value;
#endif
}
private static string? GetTagString(Activity activity, string name)
{
var value = GetTag(activity, name);
if (value is null)
{
return null;
}
return value switch
{
string s => s,
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
_ => value.ToString(),
};
}
private static bool TryConvertToInt(object value, out int result)
{
switch (value)
{
case int i:
result = i;
return true;
case long l when l is >= int.MinValue and <= int.MaxValue:
result = (int)l;
return true;
case string s:
if (int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedFromString))
{
result = parsedFromString;
return true;
}
break;
case IFormattable formattable:
var asString = formattable.ToString(null, CultureInfo.InvariantCulture);
if (int.TryParse(asString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedFromFormattable))
{
result = parsedFromFormattable;
return true;
}
break;
}
result = default;
return false;
}
private sealed class ActivityStartComparer : IComparer<Activity>
{
public static ActivityStartComparer Instance { get; } = new();
public int Compare(Activity? x, Activity? y)
{
if (ReferenceEquals(x, y))
{
return 0;
}
if (x is null)
{
return -1;
}
if (y is null)
{
return 1;
}
var startComparison = x.StartTimeUtc.CompareTo(y.StartTimeUtc);
if (startComparison != 0)
{
return startComparison;
}
return string.CompareOrdinal(x.Id, y.Id);
}
}
}