Skip to content

Commit

Permalink
Added support for tracking processes
Browse files Browse the repository at this point in the history
Added support for sending function name across to TelemetryHelper even when not in a function.
  • Loading branch information
cdmdotnet committed Dec 10, 2023
1 parent bab6917 commit f2dcb15
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using Cqrs.Services;
using Cqrs.DependencyInjection.Modules;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

#if NET6_0
using Microsoft.Extensions.Configuration;
Expand All @@ -38,10 +40,10 @@ public class IsolatedFunctionHostModule : ResolvableModule
/// </summary>
public override void Load(IServiceCollection services)
{
RegisterBasicHelpers(services);
RegisterAzureConfigurations(services);
RegisterBasicServices(services);
RegisterWebBit(services);
RegisterBasicHelpers(services);
}

#endregion
Expand Down Expand Up @@ -87,21 +89,52 @@ protected virtual void RegisterBasicHelpers(IServiceCollection services)
{
RegisterContextItemCollectionFactory(services);

bool isTelemetryClientBound = IsRegistered<TelemetryClient>(services);
if (!isTelemetryClientBound)
{
services.AddSingleton
(
new TelemetryClient(new TelemetryConfiguration()
{
ConnectionString = DependencyResolver.ConfigurationManager.GetConnectionString("Cqrs.Hosts.ApplicationInsights.ConnectionString")
})
);
}

bool isTelemetryHelperBound = IsRegistered<ITelemetryHelper>(services);
TelemetryHelper helper = null;
if (!isTelemetryHelperBound)
{
services.AddSingleton<ITelemetryHelper, TelemetryHelper>();
services.AddSingleton<ITelemetryHelper>(p => {
var telemetryHelper = new TelemetryHelper
(
Resolve<TelemetryClient>(services),
Resolve<ICorrelationIdHelper>(services),
Resolve<ILoggerSettings>(services)
);
if (DependencyResolver.ConfigurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.CloudRoleName", out string cloudRoleName) && !string.IsNullOrWhiteSpace(cloudRoleName))
telemetryHelper.GetCloudRoleName = () => { return cloudRoleName; };
if (DependencyResolver.ConfigurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.OperationName", out string operationName) && !string.IsNullOrWhiteSpace(operationName))
telemetryHelper.GetOperationName = () => { return operationName; };
return telemetryHelper;
});
}
else
{
var obj = Resolve<ITelemetryHelper>(services);
if (!(obj is TelemetryHelper))
helper = obj as TelemetryHelper;
if (helper == null)
{
Unbind<ITelemetryHelper>(services);
services.AddSingleton<ITelemetryHelper, TelemetryHelper>();
obj = Resolve<ITelemetryHelper>(services);
helper = obj as TelemetryHelper;
}
}
}

/// <summary>
/// Registers the <see cref="IContextItemCollectionFactory"/> required.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chinchilla.Logging.Azure" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging.Azure.ApplicationInsights" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging.Azure" Version="5.1.39" />
<PackageReference Include="Chinchilla.Logging.Azure.ApplicationInsights" Version="5.1.39" />

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
using Cqrs.Services;
using Cqrs.DependencyInjection.Modules;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;


#if NET6_0
using Microsoft.Extensions.Configuration;
Expand All @@ -38,10 +41,10 @@ public class FunctionHostModule : ResolvableModule
/// </summary>
public override void Load(IServiceCollection services)
{
RegisterBasicHelpers(services);
RegisterAzureConfigurations(services);
RegisterBasicServices(services);
RegisterWebBit(services);
RegisterBasicHelpers(services);
}

#endregion
Expand Down Expand Up @@ -87,21 +90,52 @@ protected virtual void RegisterBasicHelpers(IServiceCollection services)
{
RegisterContextItemCollectionFactory(services);

bool isTelemetryClientBound = IsRegistered<TelemetryClient>(services);
if (!isTelemetryClientBound)
{
services.AddSingleton
(
new TelemetryClient(new TelemetryConfiguration()
{
ConnectionString = DependencyResolver.ConfigurationManager.GetConnectionString("Cqrs.Hosts.ApplicationInsights.ConnectionString")
})
);
}

bool isTelemetryHelperBound = IsRegistered<ITelemetryHelper>(services);
TelemetryHelper helper = null;
if (!isTelemetryHelperBound)
{
services.AddSingleton<ITelemetryHelper, TelemetryHelper>();
services.AddSingleton<ITelemetryHelper>(p => {
var telemetryHelper = new TelemetryHelper
(
Resolve<TelemetryClient>(services),
Resolve<ICorrelationIdHelper>(services),
Resolve<ILoggerSettings>(services)
);
if (DependencyResolver.ConfigurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.CloudRoleName", out string cloudRoleName) && !string.IsNullOrWhiteSpace(cloudRoleName))
telemetryHelper.GetCloudRoleName = () => { return cloudRoleName; };
if (DependencyResolver.ConfigurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.OperationName", out string operationName) && !string.IsNullOrWhiteSpace(operationName))
telemetryHelper.GetOperationName = () => { return operationName; };
return telemetryHelper;
});
}
else
{
var obj = Resolve<ITelemetryHelper>(services);
if (!(obj is TelemetryHelper))
helper = obj as TelemetryHelper;
if (helper == null)
{
Unbind<ITelemetryHelper>(services);
services.AddSingleton<ITelemetryHelper, TelemetryHelper>();
obj = Resolve<ITelemetryHelper>(services);
helper = obj as TelemetryHelper;
}
}
}

/// <summary>
/// Registers the <see cref="IContextItemCollectionFactory"/> required.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chinchilla.Logging.Azure" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging.Azure.ApplicationInsights" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging.Azure" Version="5.1.39" />
<PackageReference Include="Chinchilla.Logging.Azure.ApplicationInsights" Version="5.1.39" />

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
Expand Down
22 changes: 20 additions & 2 deletions Framework/Azure/Cqrs.Azure.ServiceBus/AzureCommandBusReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ void ReceiveCommand(IMessageReceiver
Guid? guidAuthenticationToken = null;
string stringAuthenticationToken = null;
int? intAuthenticationToken = null;
string commandType = null;

IDictionary<string, string> telemetryProperties = ExtractTelemetryProperties(message, "Azure/Servicebus");
TelemetryHelper.TrackMetric("Cqrs/Handle/Command", CurrentHandles++, telemetryProperties);
Expand Down Expand Up @@ -498,7 +499,7 @@ await AzureBusHelper.ReceiveCommandAsync(
#endif
() =>
{
wasSuccessfull = null;
wasSuccessfull = null;
telemetryName = $"Cqrs/Handle/Command/Skipped/{message.MessageId}";
responseCode = "204";
// Remove message from queue
Expand Down Expand Up @@ -560,7 +561,8 @@ await AzureBusHelper.RefreshLockAsync(serviceBusReceiver,
{
if (command != null)
{
telemetryName = $"{command.GetType().FullName}/{command.Id}";
commandType = command.GetType().FullName;
telemetryName = $"{commandType}/{command.Id}";
authenticationToken = command.AuthenticationToken as ISingleSignOnToken;
if (AuthenticationTokenIsGuid)
guidAuthenticationToken = command.AuthenticationToken as Guid?;
Expand Down Expand Up @@ -750,6 +752,22 @@ await AzureBusHelper.RefreshLockAsync(serviceBusReceiver,
TelemetryHelper.TrackMetric("Cqrs/Handle/Command", CurrentHandles--, telemetryProperties);

mainStopWatch.Stop();

TelemetryHelper.TrackProcessFlow
(
telemetryName,
new Uri(string.Format("cqrs://{0}", telemetryName)),
"Command",
commandType,
GetType().Name,
null,
mainStopWatch.Elapsed,
responseCode,
wasSuccessfull == null || wasSuccessfull.Value,
null,
telemetryProperties
);

if (guidAuthenticationToken != null)
TelemetryHelper.TrackRequest
(
Expand Down
20 changes: 19 additions & 1 deletion Framework/Azure/Cqrs.Azure.ServiceBus/AzureEventBusReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ void ReceiveEvent(IMessageReceiver
Guid? guidAuthenticationToken = null;
string stringAuthenticationToken = null;
int? intAuthenticationToken = null;
string eventType = null;

IDictionary<string, string> telemetryProperties = ExtractTelemetryProperties(message, "Azure/Servicebus");
TelemetryHelper.TrackMetric("Cqrs/Handle/Event", CurrentHandles++, telemetryProperties);
Expand Down Expand Up @@ -572,7 +573,8 @@ await AzureBusHelper.RefreshLockAsync(serviceBusReceiver,
{
if (@event != null)
{
telemetryName = $"{@event.GetType().FullName}/{@event.GetIdentity()}/{@event.Id}";
eventType = @event.GetType().FullName;
telemetryName = $"{eventType}/{@event.GetIdentity()}/{@event.Id}";
authenticationToken = @event.AuthenticationToken as ISingleSignOnToken;
if (AuthenticationTokenIsGuid)
guidAuthenticationToken = @event.AuthenticationToken as Guid?;
Expand Down Expand Up @@ -766,6 +768,22 @@ await AzureBusHelper.RefreshLockAsync(serviceBusReceiver,
TelemetryHelper.TrackMetric("Cqrs/Handle/Event", CurrentHandles--, telemetryProperties);

mainStopWatch.Stop();

TelemetryHelper.TrackProcessFlow
(
telemetryName,
new Uri(string.Format("cqrs://{0}", telemetryName)),
"Event",
eventType,
GetType().Name,
null,
mainStopWatch.Elapsed,
responseCode,
wasSuccessfull == null || wasSuccessfull.Value,
null,
telemetryProperties
);

if (guidAuthenticationToken != null)
TelemetryHelper.TrackRequest
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
</ItemGroup>

Expand Down
54 changes: 43 additions & 11 deletions Framework/Cqrs/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#endregion

using System;
using Chinchilla.Logging;
using System.IO;
using System.Reflection;
using Chinchilla.Logging;
using Chinchilla.Logging.Configuration;
using Chinchilla.StateManagement;

namespace Cqrs.Configuration
{
Expand Down Expand Up @@ -45,23 +47,53 @@ public static ITelemetryHelper CreateTelemetryHelper(this IConfigurationManager

if (useApplicationInsightTelemetryHelper)
{
ITelemetryHelper helper;
try
{
string assemblyFile = Path.Combine(GetExecutionPath(), "Chinchilla.Logging.Azure.ApplicationInsights.dll");
ITelemetryHelper helper = null;
Action action = () => {
try
{
#if NETSTANDARD2_0
helper = (ITelemetryHelper)DotNetStandard2Helper.CreateInstanceFrom(string.Format("{0}\\Chinchilla.Logging.Azure.ApplicationInsights.dll", GetExecutionPath()), "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, DependencyResolver.Current.Resolve<ILoggerSettings>() }, null, null);
helper = (ITelemetryHelper)DotNetStandard2Helper.CreateInstanceFrom(assemblyFile, "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, DependencyResolver.Current.Resolve<ILoggerSettings>(), DependencyResolver.Current.Resolve<IContextItemCollectionFactory>(), false }, null, null);
#else
helper = (ITelemetryHelper)Activator.CreateInstanceFrom(string.Format("{0}\\Chinchilla.Logging.Azure.ApplicationInsights.dll", GetExecutionPath()), "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, DependencyResolver.Current.Resolve<ILoggerSettings>() }, null, null).Unwrap();
helper = (ITelemetryHelper)Activator.CreateInstanceFrom(assemblyFile, "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, DependencyResolver.Current.Resolve<ILoggerSettings>(), DependencyResolver.Current.Resolve<IContextItemCollectionFactory>(), false }, null, null).Unwrap();
#endif
}
catch
{
}
catch (FileNotFoundException)
{
throw;
}
catch
{
#if NETSTANDARD2_0
helper = (ITelemetryHelper)DotNetStandard2Helper.CreateInstanceFrom(string.Format("{0}\\Chinchilla.Logging.Azure.ApplicationInsights.dll", GetExecutionPath()), "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper }, null, null);
helper = (ITelemetryHelper)DotNetStandard2Helper.CreateInstanceFrom(assemblyFile, "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, null, DependencyResolver.Current.Resolve<IContextItemCollectionFactory>(), false }, null, null);
#else
helper = (ITelemetryHelper)Activator.CreateInstanceFrom(string.Format("{0}\\Chinchilla.Logging.Azure.ApplicationInsights.dll", GetExecutionPath()), "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper }, null, null).Unwrap();
helper = (ITelemetryHelper)Activator.CreateInstanceFrom(assemblyFile, "Chinchilla.Logging.Azure.ApplicationInsights.TelemetryHelper", false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, new object[] { correlationIdHelper, null, DependencyResolver.Current.Resolve<IContextItemCollectionFactory>(), false }, null, null).Unwrap();
#endif
}
};
try
{
action();
}
catch (FileNotFoundException)
{
assemblyFile = Path.Combine(GetExecutionPath(), "..", "Chinchilla.Logging.Azure.ApplicationInsights.dll");
action();
}

if (configurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.CloudRoleName", out string cloudRoleName) && !string.IsNullOrWhiteSpace(cloudRoleName))
{
PropertyInfo getCloudRoleNameProperty = helper.GetType().GetProperty("GetCloudRoleName", BindingFlags.Instance | BindingFlags.Public);
if (getCloudRoleNameProperty != null)
getCloudRoleNameProperty.SetValue(helper, (Func<string>)(() => { return cloudRoleName; }), null);
}
if (configurationManager.TryGetSetting("Cqrs.Hosts.ApplicationInsights.OperationName", out string operationName) && !string.IsNullOrWhiteSpace(operationName))
{
PropertyInfo getOperationNameProperty = helper.GetType().GetProperty("GetOperationName", BindingFlags.Instance | BindingFlags.Public);
if (getOperationNameProperty != null)
getOperationNameProperty.SetValue(helper, (Func<string>)(() => { return operationName; }), null);
}

return helper;
}
return new NullTelemetryHelper();
Expand Down
4 changes: 2 additions & 2 deletions Framework/Cqrs/Cqrs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@

<ItemGroup>
<PackageReference Include="Chinchilla.StateManagement" Version="4.2.11" />
<PackageReference Include="Chinchilla.Logging" Version="5.0.36" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Chinchilla.Logging" Version="5.1.39" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit f2dcb15

Please sign in to comment.