Skip to content

Commit 1419a5c

Browse files
authored
Support Spans for opentelemetry (#902)
1 parent cef7237 commit 1419a5c

File tree

10 files changed

+322
-17
lines changed

10 files changed

+322
-17
lines changed

dotnet/DotNetStandardClasses.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreChunkedTest", "te
253253
EndProject
254254
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetPDFUnitTest", "test\DotNetPdfTest\DotNetPDFUnitTest.csproj", "{0FCFB078-5584-469F-92CC-61B0A6216D0D}"
255255
EndProject
256+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GXOtel.Diagnostics", "src\dotnetcore\Providers\OpenTelemetry\Diagnostics\GXOtel.Diagnostics\GXOtel.Diagnostics.csproj", "{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}"
257+
EndProject
256258
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreCmdTest", "test\DotNetCoreCmdTest\DotNetCoreCmdTest.csproj", "{956402BD-AC8C-426E-961B-B77B3F3EDAEB}"
257259
EndProject
258260
Global
@@ -617,10 +619,15 @@ Global
617619
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
618620
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
619621
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.Build.0 = Release|Any CPU
622+
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
623+
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Debug|Any CPU.Build.0 = Debug|Any CPU
624+
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Release|Any CPU.ActiveCfg = Release|Any CPU
625+
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Release|Any CPU.Build.0 = Release|Any CPU
620626
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
621627
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
622628
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
623629
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Release|Any CPU.Build.0 = Release|Any CPU
630+
624631
EndGlobalSection
625632
GlobalSection(SolutionProperties) = preSolution
626633
HideSolutionNode = FALSE
@@ -742,7 +749,9 @@ Global
742749
{2D615969-53E2-4B77-9A9A-75C33865CF76} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
743750
{5D2B1299-479F-430A-8D72-34D44FB299FD} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
744751
{0FCFB078-5584-469F-92CC-61B0A6216D0D} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
752+
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A} = {BBE020D4-C0FF-41A9-9EB1-D1EE12CC4BB8}
745753
{956402BD-AC8C-426E-961B-B77B3F3EDAEB} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
754+
746755
EndGlobalSection
747756
GlobalSection(ExtensibilityGlobals) = postSolution
748757
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}

dotnet/src/dotnetcore/GxClasses/GxClasses.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
</ItemGroup>
121121

122122
<ItemGroup>
123-
<Folder Include="Diagnostics\" />
124123
<Folder Include="Helpers\Cryptography\Hashing\" />
125124
<Folder Include="Domain\SD\" />
126125
<Folder Include="Attributes\" />
@@ -163,7 +162,7 @@
163162
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" PrivateAssets="ALL" />
164163
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.13" NoWarn="NU5104" PrivateAssets="All" />
165164
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" PrivateAssets="All" />
166-
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
165+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
167166
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
168167
<PackageReference Include="MySqlConnector" Version="2.2.3" />
169168
<PackageReference Include="NetTopologySuite" Version="2.0.0" />

dotnet/src/dotnetcore/GxClasses/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
[assembly: InternalsVisibleTo("GeneXus.Deploy.AzureFunctions.Handlers")]
1515
[assembly: InternalsVisibleTo("AzureFunctionsTest")]
1616
[assembly: InternalsVisibleTo("GXMessageBroker")]
17-
[assembly: InternalsVisibleTo("DotNetCoreChunkedTest")]
17+
[assembly: InternalsVisibleTo("DotNetCoreChunkedTest")]
18+
[assembly: InternalsVisibleTo("GeneXus.OpenTelemetry.Diagnostics")]

dotnet/src/dotnetcore/GxNetCoreStartup/GxNetCoreStartup.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="3.1.3" />
1717
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.7" />
1818
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
19-
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
19+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
2020

2121
<PackageReference Include="Azure.Identity" Version="1.10.3" PrivateAssets ="All"/>
2222
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.13" NoWarn="NU5104" PrivateAssets ="All"/>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<DefineConstants>NETCORE;</DefineConstants>
6+
<AppDesignerFolder>Properties</AppDesignerFolder>
7+
<SignAssembly>false</SignAssembly>
8+
<AssemblyName>GeneXus.OpenTelemetry.Diagnostics</AssemblyName>
9+
<PackageTags>OpenTelemetry Diagnostics</PackageTags>
10+
<PackageId>GeneXus.OpenTelemetry.Diagnostics</PackageId>
11+
<NoWarn>NU1605</NoWarn>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
16+
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\..\..\GxClasses\GxClasses.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Diagnostics;
3+
using GeneXus.Application;
4+
using OpenTelemetry.Trace;
5+
using static GeneXus.OpenTelemetry.Diagnostics.OtelTracer;
6+
7+
namespace GeneXus.OpenTelemetry.Diagnostics
8+
{
9+
public class OtelSpan
10+
{
11+
private Activity _activity;
12+
13+
public enum SpanStatusCode
14+
{
15+
Unset,
16+
Ok,
17+
Error
18+
}
19+
public string Id
20+
{ get => _activity?.Id; }
21+
22+
public bool IsAllDataRequested
23+
{
24+
get
25+
{
26+
if (_activity != null)
27+
return _activity.IsAllDataRequested;
28+
return false;
29+
}
30+
}
31+
32+
public bool IsStopped
33+
{ get
34+
{
35+
if (_activity != null )
36+
return _activity.IsStopped;
37+
return false;
38+
}
39+
}
40+
41+
public short Kind
42+
{ get => (short)_activity?.Kind; }
43+
44+
public string ParentId
45+
{ get => _activity?.ParentId; }
46+
47+
public string ParentSpanId
48+
{ get => _activity?.ParentSpanId.ToString(); }
49+
50+
public string TraceId
51+
{ get => _activity?.TraceId.ToString(); }
52+
53+
public short Status
54+
{ get => (short)_activity?.Status; }
55+
56+
internal OtelSpan(Activity activity)
57+
{
58+
_activity = activity;
59+
}
60+
61+
public OtelSpan()
62+
{
63+
_activity = Activity.Current;
64+
}
65+
public void Start()
66+
{
67+
_activity?.Start();
68+
}
69+
70+
public void Stop()
71+
{
72+
_activity?.Stop();
73+
}
74+
public void RecordException(string message)
75+
{
76+
_activity.RecordException(new Exception(message));
77+
}
78+
79+
public void SetTag(string property, string value)
80+
{
81+
_activity.SetTag(property, value);
82+
}
83+
public string GetTagItem(string property)
84+
{
85+
return _activity.GetTagItem(property).ToString();
86+
}
87+
public void AddBaggage(string property, string value)
88+
{
89+
_activity.AddBaggage(property, value);
90+
}
91+
public string GetBaggageItem(string property)
92+
{
93+
return _activity.GetBaggageItem(property).ToString();
94+
}
95+
public void SetStatus(SpanStatusCode spanStatusCode, string message)
96+
{
97+
_activity.SetStatus((ActivityStatusCode)spanStatusCode, message);
98+
}
99+
100+
public SpanStatusCode GetStatus()
101+
{
102+
return (SpanStatusCode)_activity.GetStatus().StatusCode;
103+
}
104+
105+
//ToDO
106+
//public void AddEvent()
107+
//{
108+
109+
//}
110+
111+
}
112+
}
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 GeneXus.OpenTelemetry.Diagnostics
6+
{
7+
[GXApi]
8+
public class OtelTracer
9+
{
10+
public enum SpanType
11+
{
12+
Client,
13+
Consumer,
14+
Internal,
15+
Producer,
16+
Server
17+
}
18+
19+
public OtelSpan CreateSpan(string name, SpanType kind)
20+
{
21+
Activity activity = GXBaseObject.ActivitySource.StartActivity(name, (ActivityKind)kind);
22+
return new OtelSpan(activity);
23+
}
24+
25+
public OtelSpan GetCurrent()
26+
{
27+
return new OtelSpan(Activity.Current);
28+
}
29+
30+
public bool HasListeners()
31+
{
32+
return GXBaseObject.ActivitySource.HasListeners();
33+
}
34+
}
35+
}

dotnet/src/dotnetframework/GxClasses/Model/GXBaseObject.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
using GeneXus.Encryption;
33
using GeneXus.Http;
44
using GeneXus.Mock;
5+
#if NETCORE
6+
using GeneXus.Services.OpenTelemetry;
7+
#endif
58
using GeneXus.Utils;
69
using Jayrock.Json;
710
#if NETCORE
811
using Microsoft.AspNetCore.Http.Extensions;
912
#endif
1013
using System;
1114
using System.Collections.Generic;
15+
using System.Diagnostics;
1216
using System.Reflection;
1317
using System.Threading;
1418

@@ -23,10 +27,24 @@ public class GXBaseObject
2327
{
2428
static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXBaseObject>();
2529

30+
#if NETCORE
31+
internal static ActivitySource activitySource;
32+
#endif
2633
private Dictionary<string, string> callTargetsByObject = new Dictionary<string, string>();
2734
protected IGxContext _Context;
2835
bool _isMain;
2936
protected bool _isApi;
37+
#if NETCORE
38+
internal static ActivitySource ActivitySource {
39+
get {
40+
if (activitySource == null)
41+
activitySource = new(OpenTelemetryService.GX_ACTIVITY_SOURCE_NAME);
42+
return activitySource;
43+
}
44+
}
45+
#endif
46+
protected virtual bool GenOtelSpanEnabled() { return false; }
47+
3048
protected virtual void ExecuteEx()
3149
{
3250
if (GxMockProvider.Provider != null)
@@ -35,11 +53,31 @@ protected virtual void ExecuteEx()
3553
if (GxMockProvider.Provider.Handle(_Context, this, parmInfo))
3654
return;
3755
}
38-
ExecutePrivate();
56+
ExecuteImpl();
3957
}
4058
protected virtual void ExecutePrivate()
4159
{
42-
60+
61+
}
62+
#if NETCORE
63+
private void ExecuteUsingSpanCode()
64+
{
65+
using (Activity activity = ActivitySource.StartActivity($"{this.GetType().FullName}.execute"))
66+
{
67+
ExecutePrivate();
68+
}
69+
}
70+
#endif
71+
protected virtual void ExecuteImpl()
72+
{
73+
#if NETCORE
74+
if (GenOtelSpanEnabled())
75+
ExecuteUsingSpanCode();
76+
else
77+
ExecutePrivate();
78+
#else
79+
ExecutePrivate();
80+
#endif
4381
}
4482
protected virtual void ExecutePrivateCatch(object stateInfo)
4583
{

0 commit comments

Comments
 (0)