Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
suraciii committed Aug 31, 2023
1 parent 9b23ff2 commit 1453269
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "7.0.306"
"version": "7.0.400"
}
}
16 changes: 16 additions & 0 deletions src/DCA.Extensions.BackgroundTask/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace System.Runtime.CompilerServices
{
public class RequiredMemberAttribute : Attribute { }
public class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string name) { }
}
}

namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
public sealed class SetsRequiredMembersAttribute : Attribute
{
}
}
22 changes: 10 additions & 12 deletions src/DCA.Extensions.BackgroundTask/BackgroundTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ ActivityContext activityContext

void IThreadPoolWorkItem.Execute() => Start();

[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly", Justification = "<Pending>")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly", Justification = "<Pending>")]
public void Start()
{
if (Interlocked.CompareExchange(ref _started, 1, 0) == 0)
{
{
_ = ExecuteAsync();
}
else
Expand All @@ -44,22 +44,20 @@ public void Start()
}

public ValueTask WaitToCompleteAsync()
{
return _waiter.Task;
}
=> _waiter.Task;

internal async ValueTask ExecuteAsync()
{
Activity? activity = null;
if (activityContext != default)
{
activity = Tracing.ActivitySource.StartActivity(
"Execute Background Task",
ActivityKind.Internal,
if (activityContext != default)
{
activity = Tracing.ActivitySource.StartActivity(
"Execute Background Task",
ActivityKind.Internal,
activityContext);
}
if (id != null)
{
if (id != null)
{
activity?.SetTag("task.id", id);
}
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ private async Task ReadLoop()
{ "channel", key }
};
Metrics.CounterInflightBackgroundTasks.Add(-1, tagList);
Metrics.CounterProcessedTasks.Add(1);
Metrics.CounterProcessedTasks.Add(1, tagList);
if (!task.Started)
{
Logs.TaskStarting(logger, key);
task.Start();
}
var vt = task.WaitToCompleteAsync();
if (!vt.IsCompletedSuccessfully)
{
Logs.TaskWaitingToComplete(logger, key);
await vt.ConfigureAwait(false);
}
Checkpoint = task;
Expand All @@ -59,6 +61,7 @@ private async Task ReadLoop()
}
else
{
Logs.WaitingNextTask(logger, key);
await channelReader.WaitToReadAsync(stopToken).ConfigureAwait(false);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/DCA.Extensions.BackgroundTask/BackgroundTaskOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace DCA.Extensions.BackgroundTask;

public class BackgroundTaskOptions
{
/// <summary>
/// Configure channels
/// <summary>
/// Configure channels
/// </summary>
public List<BackgroundTaskChannelOptions> Channels { get; set; } = new()
{
Expand All @@ -14,18 +14,18 @@ public class BackgroundTaskOptions

public class BackgroundTaskChannelOptions
{
/// <summary>
/// The key of the channel
/// <summary>
/// The key of the channel
/// </summary>
public string Key { get; set; } = default!;
public required string Key { get; init; }

/// <summary>
/// Max number of inflight tasks in the channel, A negetive number means unlimited
/// <summary>
/// Max number of inflight tasks in the channel, A negetive number means unlimited
/// </summary>
public int Capacity { get; set; } = 32;

/// <summary>
/// Number of workers for the channel, it should be 1 if all tasks in the channel are started immediately when dispatching
/// <summary>
/// Number of workers for the channel, it should be 1 if all tasks in the channel are started immediately when dispatching
/// </summary>
public int WorkerCount { get; set; } = 1;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
Expand Down
18 changes: 18 additions & 0 deletions src/DCA.Extensions.BackgroundTask/Telemetry/Logs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ internal static partial class Logs
Message = "[{channel}] Reader error"
)]
public static partial void ReaderError(ILogger logger, Exception ex, string channel);

[LoggerMessage(
Level = LogLevel.Debug,
Message = "[{channel}] Task not started, starting"
)]
public static partial void TaskStarting(ILogger logger, string channel);

[LoggerMessage(
Level = LogLevel.Debug,
Message = "[{channel}] No task in channel, waiting for next"
)]
public static partial void TaskWaitingToComplete(ILogger logger, string channel);

[LoggerMessage(
Level = LogLevel.Debug,
Message = "[{channel}] No task in channel, waiting for next"
)]
public static partial void WaitingNextTask(ILogger logger, string channel);
}
1 change: 1 addition & 0 deletions src/DCA.Extensions.BackgroundTask/Telemetry/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal static class Metrics

public static Counter<int> CounterDispatchedTasks { get; }
= s_meter.CreateCounter<int>("background_tasks_dispatched");

public static Counter<int> CounterProcessedTasks { get; }
= s_meter.CreateCounter<int>("background_tasks_processed");
}

0 comments on commit 1453269

Please sign in to comment.