Skip to content

Commit

Permalink
提升对ISpan.Value的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Mar 26, 2024
1 parent 6cb42e1 commit 318491e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
22 changes: 22 additions & 0 deletions NewLife.Core/Log/ITracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ public virtual ISpan NewSpan(String name, Object? tag)
if (len <= 0 || tag == null) return span;

if (tag is String str)
{
span.Tag = str.Cut(len);
span.Value = str.Length;
}
else if (tag is StringBuilder builder)
{
span.Tag = builder.Length <= len ? builder.ToString() : builder.ToString(0, len);
span.Value = builder.Length;
}
else if (tag != null && span is DefaultSpan ds && ds.TraceFlag > 0)
{
if (tag is Packet pk)
Expand All @@ -212,6 +218,8 @@ public virtual ISpan NewSpan(String name, Object? tag)
span.Tag = pk.ToStr(null, 0, len);
else
span.Tag = pk.ToHex(len / 2);

span.Value = pk.Total;
}
//else if (tag is IMessage msg)
// span.Tag = msg.ToPacket().ToHex(len / 2);
Expand Down Expand Up @@ -290,6 +298,20 @@ public static HttpClient CreateHttpClient(this ITracer? tracer, HttpMessageHandl
return client;
}

/// <summary>开始一个Span,指定数据标签和用户数值</summary>
/// <param name="tracer">跟踪器</param>
/// <param name="name">操作名</param>
/// <param name="tag">数据</param>
/// <param name="value">用户数值。星尘平台汇总统计</param>
/// <returns></returns>
public static ISpan NewSpan(this ITracer tracer, String name, Object? tag, Int64 value)
{
var span = tracer.NewSpan(name, tag);
if (span != null) span.Value = value;

return span!;
}

/// <summary>为Http请求创建Span</summary>
/// <param name="tracer">跟踪器</param>
/// <param name="request">Http请求</param>
Expand Down
6 changes: 2 additions & 4 deletions NewLife.Core/Messaging/PacketCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public virtual IList<Packet> Parse(Packet pk)
CheckCache();
ms = Stream;

using var span = Tracer?.NewSpan("net:PacketCodec:MergeCache", $"Position={ms.Position} Length={ms.Length} NewData=[{pk.Total}]{pk.ToHex(500)}");
if (span != null) span.Value = pk.Total;
using var span = Tracer?.NewSpan("net:PacketCodec:MergeCache", $"Position={ms.Position} Length={ms.Length} NewData=[{pk.Total}]{pk.ToHex(500)}", pk.Total);

// 合并数据到最后面
if (pk != null && pk.Total > 0)
Expand Down Expand Up @@ -134,8 +133,7 @@ protected virtual void CheckCache()
if (retain > 0 && (Last.AddMilliseconds(Expire) < now || MaxCache > 0 && MaxCache <= retain))
{
var buf = ms.ReadBytes(retain > 64 ? 64 : retain);
using var span = Tracer?.NewSpan("net:PacketCodec:DropCache", $"[{retain}]{buf.ToHex()}");
if (span != null) span.Value = retain;
using var span = Tracer?.NewSpan("net:PacketCodec:DropCache", $"[{retain}]{buf.ToHex()}", retain);
span?.SetError(new Exception($"数据包编码器放弃数据 retain={retain} MaxCache={MaxCache}"), null);

if (XTrace.Debug) XTrace.Log.Debug("数据包编码器放弃数据 {0:n0},Last={1},MaxCache={2:n0}", retain, Last, MaxCache);
Expand Down
5 changes: 5 additions & 0 deletions NewLife.Core/Model/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ protected virtual void Loop()
var box = MailBox;
if (box == null || _source == null) return;

var span = DefaultSpan.Current;
var token = _source.Token;
while (!_source.IsCancellationRequested && !box.IsCompleted)
{
Expand All @@ -223,6 +224,8 @@ protected virtual void Loop()
var ctx = box.Take(token);
var task = ReceiveAsync(ctx, token);
task?.Wait(token);

if (span != null) span.Value++;
}
else
{
Expand All @@ -239,6 +242,8 @@ protected virtual void Loop()

list.Add(ctx);
}
if (span != null) span.Value += list.Count;

var task = ReceiveAsync(list.ToArray(), token);
task?.Wait(token);
}
Expand Down
2 changes: 2 additions & 0 deletions NewLife.Core/Net/IDnsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class DnsResolver : IDnsResolver
span?.AppendTag($"addrs={addrs.Join(",")}");
if (addrs != null && addrs.Length > 0)
{
if (span != null) span.Value = addrs.Length;

// 更新缓存数据
if (item == null)
{
Expand Down
4 changes: 1 addition & 3 deletions NewLife.Core/Net/NetSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ private void Ss_Received(Object? sender, ReceivedEventArgs e)
{
var ns = (this as INetSession).Host;
var tracer = ns?.Tracer;
using var span = tracer?.NewSpan($"net:{ns?.Name}:Receive", e.Message);
if (span != null) span.Value = e.Packet?.Total ?? 0;
using var span = tracer?.NewSpan($"net:{ns?.Name}:Receive", e.Message, e.Packet?.Total ?? 0);

try
{
Expand Down Expand Up @@ -223,7 +222,6 @@ public virtual INetSession Send(Packet data)
{
var ns = (this as INetSession).Host;
using var span = ns?.Tracer?.NewSpan($"net:{ns.Name}:Send", data);
if (span != null) span.Value = data.Total;

Session.Send(data);

Expand Down
3 changes: 1 addition & 2 deletions NewLife.Core/Net/SessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ private void ProcessReceive(Packet pk, IPEndPoint remote)
// 打断上下文调用链,这里必须是起点
DefaultSpan.Current = null;

using var span = Tracer?.NewSpan($"net:{Name}:ProcessReceive", pk.Total + "");
if (span != null) span.Value = pk.Total;
using var span = Tracer?.NewSpan($"net:{Name}:ProcessReceive", pk.Total + "", pk.Total);
try
{
LastTime = DateTime.Now;
Expand Down
3 changes: 1 addition & 2 deletions NewLife.Core/Net/TcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ protected override Int32 OnSend(Packet pk)

if (Log != null && Log.Enable && LogSend) WriteLog("Send [{0}]: {1}", count, pk.ToHex(LogDataLength));

using var span = Tracer?.NewSpan($"net:{Name}:Send", pk.Total + "");
if (span != null) span.Value = pk.Total;
using var span = Tracer?.NewSpan($"net:{Name}:Send", pk.Total + "", pk.Total);

var rs = count;
var sock = Client;
Expand Down
3 changes: 1 addition & 2 deletions NewLife.Core/Net/UdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ internal Int32 OnSend(Packet pk, IPEndPoint remote)
{
var count = pk.Total;

using var span = Tracer?.NewSpan($"net:{Name}:Send", pk.Total + "");
if (span != null) span.Value = pk.Total;
using var span = Tracer?.NewSpan($"net:{Name}:Send", pk.Total + "", pk.Total);

try
{
Expand Down

0 comments on commit 318491e

Please sign in to comment.