| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using Akka.Actor; |
| | 4 | | using Akka.Actor.Internal; |
| | 5 | | using Akka.Dispatch; |
| | 6 | | using Akka.Event; |
| | 7 | |
|
| | 8 | | namespace SeungYongShim.Akka.OpenTelemetry |
| | 9 | | { |
| | 10 | | public class TraceActorCell : ActorCell |
| | 11 | | { |
| | 12 | | internal string ActivityNew { get; set; } |
| | 13 | |
|
| 72 | 14 | | public TraceActorCell(ActorSystemImpl system, IInternalActorRef self, Props props, MessageDispatcher dispatcher, |
| | 15 | |
|
| | 16 | | public override void SendMessage(IActorRef sender, object message) |
| 64 | 17 | | { |
| 64 | 18 | | var ret = message switch |
| 64 | 19 | | { |
| 64 | 20 | | //IAutoReceivedMessage m => m, |
| 100 | 21 | | LogEvent m => m, |
| 114 | 22 | | var m when Activity.Current is not null => new TraceMessage(Activity.Current.Id, m), |
| 70 | 23 | | _ => message |
| 64 | 24 | | }; |
| | 25 | |
|
| 64 | 26 | | base.SendMessage(sender, ret); |
| 64 | 27 | | } |
| | 28 | |
|
| | 29 | | protected override ActorBase CreateNewActorInstance() |
| 38 | 30 | | { |
| 38 | 31 | | if (ActivityNew is not null) |
| 4 | 32 | | { |
| 4 | 33 | | using (var activity = ActivitySourceStatic.Instance.StartActivity($"{Self.Path}@Create", ActivityKind.In |
| 4 | 34 | | { |
| 4 | 35 | | return base.CreateNewActorInstance(); |
| | 36 | | } |
| | 37 | | } |
| | 38 | | else |
| 34 | 39 | | { |
| 34 | 40 | | return base.CreateNewActorInstance(); |
| | 41 | | } |
| 38 | 42 | | } |
| | 43 | |
|
| | 44 | | protected override void ReceiveMessage(object message) |
| 63 | 45 | | { |
| 63 | 46 | | switch (message) |
| | 47 | | { |
| 2 | 48 | | case Error m when m.Cause is TraceException x: |
| 2 | 49 | | using (var activity = ActivitySourceStatic.Instance.StartActivity("Exception", ActivityKind.Internal |
| 2 | 50 | | { |
| 2 | 51 | | activity?.AddTag("actor.path", Self.Path); |
| 2 | 52 | | activity?.AddTagException(x.InnerException?.Demystify()); |
| | 53 | |
|
| 2 | 54 | | base.ReceiveMessage(message); |
| 2 | 55 | | } |
| 2 | 56 | | return; |
| | 57 | |
|
| | 58 | | case TraceMessage m: |
| 23 | 59 | | message = m.Body; |
| 23 | 60 | | using (var activity = ActivitySourceStatic.Instance.StartActivity($"{Self.Path}@{message.GetType().F |
| 23 | 61 | | { |
| 23 | 62 | | var activityId = activity?.Id; |
| 23 | 63 | | activity?.AddTag("actor.path", Self.Path); |
| 23 | 64 | | activity?.AddTag("actor.library", Props.Type.Assembly.GetName().Name); |
| 23 | 65 | | activity?.AddTag("actor.type", Props.Type.Name); |
| 23 | 66 | | activity?.AddTag("actor.message", $"{message}"); |
| | 67 | |
|
| | 68 | | try |
| 23 | 69 | | { |
| 23 | 70 | | if (message is IAutoReceivedMessage) |
| 6 | 71 | | base.AutoReceiveMessage(new Envelope(message, Sender)); |
| | 72 | | else |
| 17 | 73 | | base.ReceiveMessage(message); |
| 22 | 74 | | } |
| 1 | 75 | | catch (Exception ex) |
| 1 | 76 | | { |
| 1 | 77 | | ActivityNew = activityId; |
| 1 | 78 | | throw new TraceException(ex, activityId); |
| | 79 | | } |
| 22 | 80 | | } |
| 22 | 81 | | return; |
| | 82 | |
|
| | 83 | | default: |
| 38 | 84 | | base.ReceiveMessage(message); |
| 38 | 85 | | return; |
| | 86 | | } |
| 62 | 87 | | } |
| | 88 | | } |
| | 89 | | } |