|
1 | 1 | using System; |
2 | 2 | using System.Diagnostics; |
3 | | -using GeneXus.Application; |
| 3 | +using GeneXus.Attributes; |
| 4 | +using OpenTelemetry; |
4 | 5 | using OpenTelemetry.Trace; |
5 | | -using static GeneXus.OpenTelemetry.Diagnostics.OtelTracer; |
6 | 6 |
|
7 | 7 | namespace GeneXus.OpenTelemetry.Diagnostics |
8 | 8 | { |
| 9 | + [GXApi] |
9 | 10 | public class OtelSpan |
10 | 11 | { |
11 | 12 | private Activity _activity; |
12 | | - |
13 | 13 | public enum SpanStatusCode |
14 | 14 | { |
15 | 15 | Unset, |
16 | 16 | Ok, |
17 | 17 | Error |
18 | 18 | } |
19 | | - public string Id |
20 | | - { get => _activity?.Id; } |
21 | 19 |
|
22 | | - public bool IsAllDataRequested |
| 20 | + internal OtelSpan(Activity activity) |
23 | 21 | { |
24 | | - get |
25 | | - { |
26 | | - if (_activity != null) |
27 | | - return _activity.IsAllDataRequested; |
28 | | - return false; |
29 | | - } |
| 22 | + _activity = activity; |
30 | 23 | } |
31 | 24 |
|
32 | | - public bool IsStopped |
33 | | - { get |
34 | | - { |
35 | | - if (_activity != null ) |
36 | | - return _activity.IsStopped; |
37 | | - return false; |
38 | | - } |
39 | | - } |
| 25 | + public OtelSpan() |
| 26 | + {} |
40 | 27 |
|
41 | | - public short Kind |
42 | | - { get => (short)_activity?.Kind; } |
| 28 | + public Activity Activity => _activity; |
43 | 29 |
|
44 | | - public string ParentId |
45 | | - { get => _activity?.ParentId; } |
| 30 | + #region EO Properties |
| 31 | + public GXSpanContext SpanContext => _activity == null ? null : new GXSpanContext(_activity.Context); |
46 | 32 |
|
47 | | - public string ParentSpanId |
48 | | - { get => _activity?.ParentSpanId.ToString(); } |
| 33 | + public GXTraceContext GetContext => _activity == null ? null : new GXTraceContext(_activity.Context); |
| 34 | + public string SpanId => _activity?.Id; |
49 | 35 |
|
50 | | - public string TraceId |
51 | | - { get => _activity?.TraceId.ToString(); } |
| 36 | + public string TraceId => _activity?.TraceId.ToHexString(); |
52 | 37 |
|
53 | | - public short Status |
54 | | - { get => (short)_activity?.Status; } |
55 | | - |
56 | | - internal OtelSpan(Activity activity) |
| 38 | + #endregion |
| 39 | + |
| 40 | + #region Methods |
| 41 | + public void Stop() |
57 | 42 | { |
58 | | - _activity = activity; |
| 43 | + _activity?.Stop(); |
| 44 | + } |
| 45 | + public void RecordException(string message) |
| 46 | + { |
| 47 | + _activity?.RecordException(new Exception(message)); |
59 | 48 | } |
60 | 49 |
|
61 | | - public OtelSpan() |
| 50 | + public void SetStringAttribute(string property, string value) |
62 | 51 | { |
63 | | - _activity = Activity.Current; |
| 52 | + _activity?.SetTag(property, value); |
| 53 | + |
64 | 54 | } |
65 | | - public void Start() |
| 55 | + public void SetLongAttribute(string property, long value) |
66 | 56 | { |
67 | | - _activity?.Start(); |
| 57 | + _activity?.SetTag(property, value); |
| 58 | + |
68 | 59 | } |
| 60 | + public void SetDoubleAttribute(string property, double value) |
| 61 | + { |
| 62 | + _activity?.SetTag(property, value); |
69 | 63 |
|
70 | | - public void Stop() |
| 64 | + } |
| 65 | + public void SetBooleanAttribute(string property, bool value) |
71 | 66 | { |
72 | | - _activity?.Stop(); |
| 67 | + _activity?.SetTag(property, value); |
| 68 | + |
73 | 69 | } |
74 | | - public void RecordException(string message) |
| 70 | + public GXTraceContext AddBaggage(string property, string value) |
| 71 | + { |
| 72 | + Baggage.SetBaggage(property, value); |
| 73 | + if (_activity != null) |
| 74 | + return new GXTraceContext(_activity.Context); |
| 75 | + else return null; |
| 76 | + } |
| 77 | + |
| 78 | + public string GetBaggageItem(string property,GXTraceContext gXTraceContext) |
| 79 | + { |
| 80 | + return Baggage.GetBaggage(property); |
| 81 | + } |
| 82 | + public void SetStatus(SpanStatusCode spanStatusCode, string message) |
75 | 83 | { |
76 | | - _activity.RecordException(new Exception(message)); |
| 84 | + _activity?.SetStatus((ActivityStatusCode)spanStatusCode, message); |
77 | 85 | } |
| 86 | + public void SetStatus(SpanStatusCode spanStatusCode) |
| 87 | + { |
| 88 | + _activity?.SetStatus((ActivityStatusCode)spanStatusCode); |
| 89 | + } |
| 90 | + |
| 91 | + #endregion |
78 | 92 |
|
79 | | - public void SetTag(string property, string value) |
| 93 | + #region Private Methods |
| 94 | + public bool IsRecording => IsSpanRecording(); |
| 95 | + |
| 96 | + private bool IsSpanRecording() |
80 | 97 | { |
81 | | - _activity.SetTag(property, value); |
| 98 | + if (_activity != null) |
| 99 | + return _activity.IsAllDataRequested; |
| 100 | + else |
| 101 | + return false; |
82 | 102 | } |
83 | | - public string GetTagItem(string property) |
| 103 | + #endregion |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + public class GXTraceContext : GXSpanContext |
| 108 | + { |
| 109 | + //Dummy class to be compatible with Java. |
| 110 | + //.NET does not requiere propagating the context explicitly in most of the cases. |
| 111 | + //https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Api/README.md#baggage-api |
| 112 | + |
| 113 | + public GXTraceContext(ActivityContext activityContext):base(activityContext) { } |
| 114 | + public GXTraceContext():base() { } |
| 115 | + } |
| 116 | + public class GXSpanContext |
| 117 | + { |
| 118 | + private ActivityContext _activityContext; |
| 119 | + public GXSpanContext() |
84 | 120 | { |
85 | | - return _activity.GetTagItem(property).ToString(); |
| 121 | + _activityContext = Activity.Current.Context; |
86 | 122 | } |
87 | | - public void AddBaggage(string property, string value) |
| 123 | + |
| 124 | + public GXSpanContext(ActivityContext activityContext) |
88 | 125 | { |
89 | | - _activity.AddBaggage(property, value); |
| 126 | + _activityContext = activityContext; |
90 | 127 | } |
91 | | - public string GetBaggageItem(string property) |
| 128 | + public ActivityContext ActivityContext { get { return _activityContext; } } |
| 129 | + |
| 130 | + public string TraceId => GetTraceId(); |
| 131 | + |
| 132 | + public string SpanId => GetSpanId(); |
| 133 | + |
| 134 | + private string GetTraceId() |
92 | 135 | { |
93 | | - return _activity.GetBaggageItem(property).ToString(); |
| 136 | + if (_activityContext != null) |
| 137 | + { |
| 138 | + ActivityTraceId activityTraceId = _activityContext.TraceId; |
| 139 | + return activityTraceId.ToHexString(); |
| 140 | + } else return null; |
| 141 | + } |
| 142 | + private string GetSpanId() |
| 143 | + { |
| 144 | + if (_activityContext != null) |
| 145 | + { |
| 146 | + ActivitySpanId activitySpanId = _activityContext.SpanId; |
| 147 | + return activitySpanId.ToHexString(); |
| 148 | + } else { return null; } |
94 | 149 | } |
95 | | - public void SetStatus(SpanStatusCode spanStatusCode, string message) |
| 150 | + private GXActivityTraceFlags TraceFlags() |
96 | 151 | { |
97 | | - _activity.SetStatus((ActivityStatusCode)spanStatusCode, message); |
| 152 | + if (_activityContext != null) { |
| 153 | + ActivityTraceFlags activityTraceFlags = _activityContext.TraceFlags; |
| 154 | + return (GXActivityTraceFlags)activityTraceFlags; |
| 155 | + } |
| 156 | + else { return GXActivityTraceFlags.None;} |
98 | 157 | } |
99 | 158 |
|
100 | | - public SpanStatusCode GetStatus() |
| 159 | + private string TraceState() |
101 | 160 | { |
102 | | - return (SpanStatusCode)_activity.GetStatus().StatusCode; |
| 161 | + if (_activityContext != null) |
| 162 | + return _activityContext.TraceState; |
| 163 | + else return null; |
103 | 164 | } |
104 | 165 |
|
105 | | - //ToDO |
106 | | - //public void AddEvent() |
107 | | - //{ |
108 | 166 |
|
109 | | - //} |
| 167 | + // |
| 168 | + // Summary: |
| 169 | + // Specifies flags defined by the W3C standard that are associated with an activity. |
110 | 170 |
|
| 171 | + internal enum GXActivityTraceFlags |
| 172 | + { |
| 173 | + // |
| 174 | + // Summary: |
| 175 | + // The activity has not been marked. |
| 176 | + None = 0, |
| 177 | + // |
| 178 | + // Summary: |
| 179 | + // The activity (or more likely its parents) has been marked as useful to record. |
| 180 | + Recorded = 1 |
| 181 | + } |
111 | 182 | } |
112 | 183 | } |
0 commit comments