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

Add TelemetrySource to RateLimiterRejectedException #2346

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

peter-csala
Copy link
Contributor

@peter-csala peter-csala commented Oct 15, 2024

Pull Request

The issue or feature being addressed

#2345

Details on the issue fix or feature implementation

Done

  • Added TelemetrySource property to the RateLimiterRejectedException
    • We can't call it Source because that would shadow the inherited Source property

Confirm the following

  • I started this PR by branching from the head of the default branch
  • I have targeted the PR to merge into the default branch
  • I have included unit tests for the issue/feature
  • I have successfully run a local build

@peter-csala peter-csala force-pushed the add-telemetry-source-to-rate-limiter-rejected-exception branch from 92e3f2e to cd0b75c Compare October 16, 2024 10:53
Copy link

codecov bot commented Oct 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.43%. Comparing base (14ac109) to head (dc687af).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2346      +/-   ##
==========================================
+ Coverage   85.40%   85.43%   +0.03%     
==========================================
  Files         313      313              
  Lines        7467     7485      +18     
  Branches     1125     1128       +3     
==========================================
+ Hits         6377     6395      +18     
  Misses        745      745              
  Partials      345      345              
Flag Coverage Δ
linux 85.43% <100.00%> (+0.03%) ⬆️
macos 85.43% <100.00%> (+0.03%) ⬆️
windows 85.41% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

/// Gets BlahBlahBlah.
/// </summary>
/// <value>dummy.</value>
public ResilienceTelemetrySource TelemetrySource { get; } // Fix visibility
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I add the following line to the Polly.Core.csproj:

<InternalsVisibleToProject Include="Polly.RateLimiting" />

then I don't have to change the access modifier from internal to public.

But it causes lots of compile time error, like complaining about RateLimiterResilienceStrategy's ExecuteCore should be proctected internal not just proctected, etc..

Do you have any other idea than making this property public?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, we're currently doing this:

  1. Exposing the source;
  2. Using the source to pass to an exception;
  3. Having the exception turn the context into a string;
  4. Expose the string on the exception.

If that is the case, would this be better?

  1. Add a method to the context to turn it into a string (i.e. have it own its own logic to do that so it's not far from the type and could misalign if we changed the context in the future and forgot we were stringifying it elsewhere);
  2. Use that method to expose that string representation on the telemetry;
  3. Pass that string to the exception;
  4. Expose that string on the exception.

The above avoids the visibility problem, and makes the source string come from Polly.Core as the source-of-truth, rather than making something visible solely for rate limiting to make its own decisions about.

Copy link
Contributor Author

@peter-csala peter-csala Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this approach we end up with following error:

Type 'RateLimiterRejectedException' already defines a member called 'RateLimiterRejectedException' with the same parameter types

public RateLimiterRejectedException(string telemetrySource)
public RateLimiterRejectedException(string message)

public RateLimiterRejectedException(string message, TimeSpan retryAfter)
public RateLimiterRejectedException(string telemetrySource, TimeSpan retryAfter)
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So let's add a constructor that doesn't cause that problem then 😄 - for example, by manually specifying an exception message new RateLimiterRejectedException("The source {blah} go boom", source).

I think it would be much neater to have to manually construct a message because we've already used .ctor(string) for an explicit exception than have to expose the source just to avoid that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just expose this property?

internal ResilienceTelemetrySource TelemetrySource { get; }

The ResilienceStrategyTelemetry is already public. I would indeed suggest to make the constructor of ResilienceTelemetrySource public, there is no reason for us to hinder the testability.

@peter-csala peter-csala marked this pull request as ready for review October 17, 2024 16:09
@peter-csala peter-csala changed the title [DRAFT] Add TelemetrySource to RateLimiterRejectedException Add TelemetrySource to RateLimiterRejectedException Oct 17, 2024
/// <summary>
/// Gets the source of the strategy which has thrown the exception, if known.
/// </summary>
public string? TelemetrySource { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use telemetry source here? The strategy name is not enough to uniquely identify the strategy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added a comment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants