Skip to content

Commit 5744312

Browse files
committed
Add classes for manual instrumentation
1 parent 7df47d6 commit 5744312

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Diagnostics;
3+
using OpenTelemetry.Trace;
4+
5+
namespace GxClasses.Diagnostics.Opentelemetry
6+
{
7+
public class OtelSpan
8+
{
9+
private Activity _activity;
10+
public string Id
11+
{ get => _activity?.Id; }
12+
13+
public bool IsAllDataRequested
14+
{
15+
get
16+
{
17+
if (_activity != null)
18+
return _activity.IsAllDataRequested;
19+
return false;
20+
}
21+
}
22+
23+
public bool IsStopped
24+
{ get
25+
{
26+
if (_activity != null )
27+
return _activity.IsStopped;
28+
return false;
29+
}
30+
}
31+
32+
public SpanKind Kind
33+
{ get => (SpanKind)_activity?.Kind; }
34+
35+
public string ParentId
36+
{ get => _activity?.ParentId; }
37+
38+
public string ParentSpanId
39+
{ get => _activity?.ParentSpanId.ToString(); }
40+
41+
public string TraceId
42+
{ get => _activity?.TraceId.ToString(); }
43+
44+
public SpanStatusCode Status
45+
{ get => (SpanStatusCode)_activity?.Status; }
46+
47+
public enum SpanStatusCode
48+
{
49+
Unset,
50+
Ok,
51+
Error
52+
}
53+
54+
internal OtelSpan(Activity activity)
55+
{
56+
_activity = activity;
57+
}
58+
59+
public void Start()
60+
{
61+
_activity?.Start();
62+
}
63+
64+
public void Stop()
65+
{
66+
_activity?.Stop();
67+
}
68+
public void RecordException(string message)
69+
{
70+
_activity.RecordException(new Exception(message));
71+
}
72+
73+
public void SetTag(string property, string value)
74+
{
75+
_activity.SetTag(property, value);
76+
}
77+
public string GetTagItem(string property)
78+
{
79+
return _activity.GetTagItem(property).ToString();
80+
}
81+
public void AddBaggage(string property, string value)
82+
{
83+
_activity.AddBaggage(property, value);
84+
}
85+
public string GetBaggageItem(string property)
86+
{
87+
return _activity.GetBaggageItem(property).ToString();
88+
}
89+
public void SetStatus(SpanStatusCode spanStatusCode, string message)
90+
{
91+
_activity.SetStatus((ActivityStatusCode)spanStatusCode, message);
92+
}
93+
94+
public SpanStatusCode GetStatus()
95+
{
96+
return (SpanStatusCode)_activity.GetStatus().StatusCode;
97+
}
98+
99+
//ToDO
100+
//public void AddEvent()
101+
//{
102+
103+
//}
104+
105+
}
106+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Diagnostics;
2+
using GeneXus.Application;
3+
using GeneXus.Attributes;
4+
5+
namespace GxClasses.Diagnostics.Opentelemetry
6+
{
7+
[GXApi]
8+
public class OtelTracer
9+
{
10+
public enum SpanKind
11+
{
12+
Client,
13+
Consumer,
14+
Internal,
15+
Producer,
16+
Server
17+
}
18+
19+
public static OtelSpan CreateSpan(string name, SpanKind kind)
20+
{
21+
Activity activity = GXBaseObject.ActivitySource.StartActivity(name, (ActivityKind)kind);
22+
return new OtelSpan(activity);
23+
}
24+
25+
public static OtelSpan GetCurrent()
26+
{
27+
return new OtelSpan(Activity.Current);
28+
}
29+
30+
public static bool HasListeners()
31+
{
32+
return GXBaseObject.ActivitySource.HasListeners();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)