Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Spans for opentelemetry #902

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dotnet/DotNetStandardClasses.sln
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreChunkedTest", "te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetPDFUnitTest", "test\DotNetPdfTest\DotNetPDFUnitTest.csproj", "{0FCFB078-5584-469F-92CC-61B0A6216D0D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GXOtel.Diagnostics", "src\dotnetcore\Providers\OpenTelemetry\Diagnostics\GXOtel.Diagnostics\GXOtel.Diagnostics.csproj", "{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreCmdTest", "test\DotNetCoreCmdTest\DotNetCoreCmdTest.csproj", "{956402BD-AC8C-426E-961B-B77B3F3EDAEB}"
EndProject
Global
Expand Down Expand Up @@ -617,10 +619,15 @@ Global
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FCFB078-5584-469F-92CC-61B0A6216D0D}.Release|Any CPU.Build.0 = Release|Any CPU
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A}.Release|Any CPU.Build.0 = Release|Any CPU
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{956402BD-AC8C-426E-961B-B77B3F3EDAEB}.Release|Any CPU.Build.0 = Release|Any CPU

EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -742,7 +749,9 @@ Global
{2D615969-53E2-4B77-9A9A-75C33865CF76} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{5D2B1299-479F-430A-8D72-34D44FB299FD} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{0FCFB078-5584-469F-92CC-61B0A6216D0D} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{A42066E8-DDB9-4767-AEFA-E6D13EFF051A} = {BBE020D4-C0FF-41A9-9EB1-D1EE12CC4BB8}
{956402BD-AC8C-426E-961B-B77B3F3EDAEB} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}

EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}
Expand Down
3 changes: 1 addition & 2 deletions dotnet/src/dotnetcore/GxClasses/GxClasses.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Diagnostics\" />
<Folder Include="Helpers\Cryptography\Hashing\" />
<Folder Include="Domain\SD\" />
<Folder Include="Attributes\" />
Expand Down Expand Up @@ -163,7 +162,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" PrivateAssets="ALL" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.13" NoWarn="NU5104" PrivateAssets="All" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" PrivateAssets="All" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
<PackageReference Include="MySqlConnector" Version="2.2.3" />
<PackageReference Include="NetTopologySuite" Version="2.0.0" />
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/dotnetcore/GxClasses/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
[assembly: InternalsVisibleTo("GeneXus.Deploy.AzureFunctions.Handlers")]
[assembly: InternalsVisibleTo("AzureFunctionsTest")]
[assembly: InternalsVisibleTo("GXMessageBroker")]
[assembly: InternalsVisibleTo("DotNetCoreChunkedTest")]
[assembly: InternalsVisibleTo("DotNetCoreChunkedTest")]
[assembly: InternalsVisibleTo("GeneXus.OpenTelemetry.Diagnostics")]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.7" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />

<PackageReference Include="Azure.Identity" Version="1.10.3" PrivateAssets ="All"/>
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.13" NoWarn="NU5104" PrivateAssets ="All"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DefineConstants>NETCORE;</DefineConstants>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SignAssembly>false</SignAssembly>
<AssemblyName>GeneXus.OpenTelemetry.Diagnostics</AssemblyName>
<PackageTags>OpenTelemetry Diagnostics</PackageTags>
<PackageId>GeneXus.OpenTelemetry.Diagnostics</PackageId>
<NoWarn>NU1605</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\GxClasses\GxClasses.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Diagnostics;
using GeneXus.Application;
using OpenTelemetry.Trace;
using static GeneXus.OpenTelemetry.Diagnostics.OtelTracer;

namespace GeneXus.OpenTelemetry.Diagnostics
{
public class OtelSpan
{
private Activity _activity;

public enum SpanStatusCode
{
Unset,
Ok,
Error
}
public string Id
{ get => _activity?.Id; }

public bool IsAllDataRequested
{
get
{
if (_activity != null)
return _activity.IsAllDataRequested;
return false;
}
}

public bool IsStopped
{ get
{
if (_activity != null )
return _activity.IsStopped;
return false;
}
}

public short Kind
{ get => (short)_activity?.Kind; }

public string ParentId
{ get => _activity?.ParentId; }

public string ParentSpanId
{ get => _activity?.ParentSpanId.ToString(); }

public string TraceId
{ get => _activity?.TraceId.ToString(); }

public short Status
{ get => (short)_activity?.Status; }

internal OtelSpan(Activity activity)
{
_activity = activity;
}

public OtelSpan()
{
_activity = Activity.Current;
}
public void Start()
{
_activity?.Start();
}

public void Stop()
{
_activity?.Stop();
}
public void RecordException(string message)
{
_activity.RecordException(new Exception(message));
}

public void SetTag(string property, string value)
{
_activity.SetTag(property, value);
}
public string GetTagItem(string property)
{
return _activity.GetTagItem(property).ToString();
}
public void AddBaggage(string property, string value)
{
_activity.AddBaggage(property, value);
}
public string GetBaggageItem(string property)
{
return _activity.GetBaggageItem(property).ToString();
}
public void SetStatus(SpanStatusCode spanStatusCode, string message)
{
_activity.SetStatus((ActivityStatusCode)spanStatusCode, message);
}

public SpanStatusCode GetStatus()
{
return (SpanStatusCode)_activity.GetStatus().StatusCode;
}

//ToDO
//public void AddEvent()
//{

//}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics;
using GeneXus.Application;
using GeneXus.Attributes;

namespace GeneXus.OpenTelemetry.Diagnostics
{
[GXApi]
public class OtelTracer
{
public enum SpanType
{
Client,
Consumer,
Internal,
Producer,
Server
}

public OtelSpan CreateSpan(string name, SpanType kind)
{
Activity activity = GXBaseObject.ActivitySource.StartActivity(name, (ActivityKind)kind);
return new OtelSpan(activity);
}

public OtelSpan GetCurrent()
{
return new OtelSpan(Activity.Current);
}

public bool HasListeners()
{
return GXBaseObject.ActivitySource.HasListeners();
}
}
}
42 changes: 40 additions & 2 deletions dotnet/src/dotnetframework/GxClasses/Model/GXBaseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
using GeneXus.Encryption;
using GeneXus.Http;
using GeneXus.Mock;
#if NETCORE
using GeneXus.Services.OpenTelemetry;
#endif
using GeneXus.Utils;
using Jayrock.Json;
#if NETCORE
using Microsoft.AspNetCore.Http.Extensions;
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

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

#if NETCORE
internal static ActivitySource activitySource;
#endif
private Dictionary<string, string> callTargetsByObject = new Dictionary<string, string>();
protected IGxContext _Context;
bool _isMain;
protected bool _isApi;
#if NETCORE
internal static ActivitySource ActivitySource {
get {
if (activitySource == null)
activitySource = new(OpenTelemetryService.GX_ACTIVITY_SOURCE_NAME);
return activitySource;
}
}
#endif
protected virtual bool GenOtelSpanEnabled() { return false; }

protected virtual void ExecuteEx()
{
if (GxMockProvider.Provider != null)
Expand All @@ -35,11 +53,31 @@ protected virtual void ExecuteEx()
if (GxMockProvider.Provider.Handle(_Context, this, parmInfo))
return;
}
ExecutePrivate();
ExecuteImpl();
}
protected virtual void ExecutePrivate()
{


}
#if NETCORE
private void ExecuteUsingSpanCode()
{
using (Activity activity = ActivitySource.StartActivity($"{this.GetType().FullName}.execute"))
{
ExecutePrivate();
}
}
#endif
protected virtual void ExecuteImpl()
{
#if NETCORE
if (GenOtelSpanEnabled())
ExecuteUsingSpanCode();
else
ExecutePrivate();
#else
ExecutePrivate();
#endif
}
protected virtual void ExecutePrivateCatch(object stateInfo)
{
Expand Down
Loading