This commit is contained in:
Janus C. H. Knudsen 2026-01-10 20:39:17 +01:00
parent 54b057886c
commit 7fc1ae0650
204 changed files with 4345 additions and 134 deletions

View file

@ -0,0 +1,14 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
namespace PlanTempus.Core.Telemetry.Enrichers
{
public class EnrichWithMetaTelemetry(ITelemetryProcessor next) : ITelemetryProcessor
{
public void Process(ITelemetry item)
{
//nothing going on here yet :)
next.Process(item);
}
}
}

View file

@ -0,0 +1,9 @@
using System.Threading.Channels;
namespace PlanTempus.Core.Telemetry
{
public interface IMessageChannel<T> : IDisposable
{
ChannelWriter<T> Writer { get; }
ChannelReader<T> Reader { get; }
}
}

View file

@ -0,0 +1,23 @@
using Microsoft.ApplicationInsights.Channel;
using System.Threading.Channels;
namespace PlanTempus.Core.Telemetry
{
public class MessageChannel : IMessageChannel<ITelemetry>
{
private readonly Channel<ITelemetry> _channel;
public MessageChannel()
{
_channel = Channel.CreateUnbounded<ITelemetry>();
}
public ChannelWriter<ITelemetry> Writer => _channel.Writer;
public ChannelReader<ITelemetry> Reader => _channel.Reader;
public void Dispose()
{
_channel.Writer.Complete();
}
}
}

View file

@ -0,0 +1,13 @@
using System.Threading.Channels;
namespace PlanTempus.Core.Telemetry;
public class NotificationChannel : IMessageChannel<string>
{
private readonly Channel<string> _channel = Channel.CreateUnbounded<string>();
public ChannelWriter<string> Writer => _channel.Writer;
public ChannelReader<string> Reader => _channel.Reader;
public void Dispose() => _channel.Writer.Complete();
}

View file

@ -0,0 +1,38 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
namespace PlanTempus.Core.Telemetry
{
public class SeqTelemetryChannel(IMessageChannel<ITelemetry> messageChannel, TelemetryClient telemetryClient)
: InMemoryChannel, ITelemetryChannel
{
public new void Send(ITelemetry telemetry)
{
if (telemetry.Context.GlobalProperties.TryGetValue("OmitSeqTelemetryChannel", out var value))
if (value == "true")
{
base.Send(telemetry);
return;
}
try
{
var writeTask = messageChannel.Writer.WriteAsync(telemetry).AsTask();
writeTask.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
catch (Exception e)
{
telemetryClient.TrackException(e,
new Dictionary<string, string> { { "OmitSeqTelemetryChannel", "true" } });
}
base.Send(telemetry);
}
}
}

View file

@ -0,0 +1,25 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace PlanTempus.Core.Telemetry;
/// <summary>
/// Signal telemetry der bruges til at stoppe SeqBackgroundService gracefully.
/// Når denne læses fra channel, stopper servicen efter at have processeret alle tidligere beskeder.
/// </summary>
public class StopTelemetry : ITelemetry
{
public DateTimeOffset Timestamp { get; set; }
public string Sequence { get; set; }
public TelemetryContext Context { get; } = new TelemetryContext();
public IExtension Extension { get; set; }
public ITelemetry DeepClone() => new StopTelemetry();
public void Sanitize() { }
public void SerializeData(ISerializationWriter serializationWriter) { }
}

View file

@ -0,0 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PlanTempus.Core.Telemetry;
public static class TelemetryExtensions
{
public static Dictionary<string, string> Format(this object obj)
{
return new Dictionary<string, string> { { "Object", JObject.FromObject(obj).ToString() } };
}
}