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

OTel ActorContextDecorator should support all ReenterAfter overloads #2138

Merged
merged 1 commit into from
Oct 12, 2024
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
6 changes: 3 additions & 3 deletions src/Proto.Actor/Context/ActorContextDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public virtual void ReenterAfter<T>(Task<T> target, Func<Task<T>, Task> action)

public virtual void ReenterAfter(Task target, Action action) => _context.ReenterAfter(target, action);

public void ReenterAfter(Task target, Action<Task> action) => _context.ReenterAfter(target, action);
public virtual void ReenterAfter(Task target, Action<Task> action) => _context.ReenterAfter(target, action);

public void ReenterAfter<T>(Task<T> target, Action<Task<T>> action) => _context.ReenterAfter(target, action);
public virtual void ReenterAfter<T>(Task<T> target, Action<Task<T>> action) => _context.ReenterAfter(target, action);

public void ReenterAfter(Task target, Func<Task, Task> action) => _context.ReenterAfter(target, action);
public virtual void ReenterAfter(Task target, Func<Task, Task> action) => _context.ReenterAfter(target, action);

public CapturedContext Capture() => _context.Capture();

Expand Down
42 changes: 42 additions & 0 deletions src/Proto.OpenTelemetry/OpenTelemetryActorContextDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,48 @@ public override void ReenterAfter<T>(Task<T> target, Func<Task<T>, Task> action)
base.ReenterAfter(target, a2);
}

public override void ReenterAfter(Task target, Action<Task> action)
{
var current = Activity.Current?.Context ?? default;
var message = base.Message!;
Action<Task> a2 = t =>
{
using var x = OpenTelemetryHelpers.BuildStartedActivity(current, Source, nameof(ReenterAfter), message,
_sendActivitySetup);
x?.SetTag(ProtoTags.ActionType, nameof(ReenterAfter));
action(t);
};
base.ReenterAfter(target, a2);
}

public override void ReenterAfter<T>(Task<T> target, Action<Task<T>> action)
{
var current = Activity.Current?.Context ?? default;
var message = base.Message!;
Action<Task<T>> a2 = t =>
{
using var x = OpenTelemetryHelpers.BuildStartedActivity(current, Source, nameof(ReenterAfter), message,
_sendActivitySetup);
x?.SetTag(ProtoTags.ActionType, nameof(ReenterAfter));
action(t);
};
base.ReenterAfter(target, a2);
}

public override void ReenterAfter(Task target, Func<Task, Task> action)
{
var current = Activity.Current?.Context ?? default;
var message = base.Message!;
Func<Task, Task> a2 = async t =>
{
using var x = OpenTelemetryHelpers.BuildStartedActivity(current, Source, nameof(ReenterAfter), message,
_sendActivitySetup);
x?.SetTag(ProtoTags.ActionType, nameof(ReenterAfter));
await action(t).ConfigureAwait(false);
};
base.ReenterAfter(target, a2);
}

public override PID SpawnNamed(Props props, string name, Action<IContext>? callback = null) =>
OpenTelemetryMethodsDecorators.SpawnNamed(Source,_spawnActivitySetup, () => base.SpawnNamed(props, name, callback),name, Actor.GetType().Name);
}
52 changes: 50 additions & 2 deletions tests/Proto.OpenTelemetry.Tests/OpenTelemetryTracingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ await VerifyTrace(async (rootContext, target) =>
response.Should().BeEquivalentTo(new TraceResponse(TestBaggage));
}
);

[Theory]
[InlineData(SendAs.ReEnterAfter1)]
[InlineData(SendAs.ReEnterAfter2)]
[InlineData(SendAs.ReEnterAfter3)]
[InlineData(SendAs.ReEnterAfter4)]
[InlineData(SendAs.ReEnterAfter5)]
public async Task TracesPropagateCorrectlyWithBaggageForReEnterAfter(SendAs reEnterType) =>
await VerifyTrace(async (rootContext, target) =>
{
Baggage.Current = TestBaggage;
var response = await rootContext.RequestAsync<TraceResponse>(target, new TraceMe(reEnterType));
response.Should().BeEquivalentTo(new TraceResponse(TestBaggage));
}
);

[Fact]
public async Task TracesPropagateCorrectlyWithBaggageForRequest() =>
Expand Down Expand Up @@ -240,13 +255,18 @@ public async Task ExceptionsAreRecorded()
receiveActivity.Events.Single().Tags.Where(tag => tag.Key.StartsWith("exception")).Should().NotBeEmpty();
}

private enum SendAs
public enum SendAs
{
RequestAsync,
Request,
Send,
Forward,
Invalid
Invalid,
ReEnterAfter1, // void ReenterAfter<T>(Task<T> target, Func<Task<T>, Task> action);
ReEnterAfter2, // void ReenterAfter(Task target, Action action);
ReEnterAfter3, // void ReenterAfter(Task target, Action<Task> action);
ReEnterAfter4, // void ReenterAfter<T>(Task<T> target, Action<Task<T>> action);
ReEnterAfter5, // void ReenterAfter(Task target, Func<Task, Task> action);
}

private record TraceMe(SendAs Method);
Expand Down Expand Up @@ -290,6 +310,34 @@ private async Task OnTraceMe(IContext context, TraceMe msg)
case SendAs.Forward:
context.Forward(target);

break;
case SendAs.ReEnterAfter1:
context.ReenterAfter(Task.FromResult(1), _ =>
{
context.Forward(target);
return Task.CompletedTask;
});

break;
case SendAs.ReEnterAfter2:
context.ReenterAfter(Task.CompletedTask, () => context.Forward(target));

break;
case SendAs.ReEnterAfter3:
context.ReenterAfter(Task.CompletedTask, _ => context.Forward(target));

break;
case SendAs.ReEnterAfter4:
context.ReenterAfter(Task.FromResult(1), _ => context.Forward(target));

break;
case SendAs.ReEnterAfter5:
context.ReenterAfter(Task.CompletedTask, _ =>
{
context.Forward(target);
return Task.CompletedTask;
});

break;
default: throw new ArgumentOutOfRangeException(nameof(msg.Method), msg.Method.ToString());
}
Expand Down
Loading