Skip to content

Envelopes#544

Merged
bruno-garcia merged 35 commits intomainfrom
envelopes
Oct 15, 2020
Merged

Envelopes#544
bruno-garcia merged 35 commits intomainfrom
envelopes

Conversation

@Tyrrrz
Copy link
Contributor

@Tyrrrz Tyrrrz commented Oct 6, 2020

No description provided.

@Tyrrrz Tyrrrz added Feature New feature or request Sentry labels Oct 6, 2020
@codecov-io
Copy link

codecov-io commented Oct 7, 2020

Codecov Report

Merging #544 into main will increase coverage by 0.22%.
The diff coverage is 91.39%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
+ Coverage   85.33%   85.55%   +0.22%     
==========================================
  Files         128      135       +7     
  Lines        3170     3274     +104     
  Branches      730      732       +2     
==========================================
+ Hits         2705     2801      +96     
- Misses        268      276       +8     
  Partials      197      197              
Impacted Files Coverage Δ
...entry/Internal/Extensions/CollectionsExtensions.cs 100.00% <ø> (ø)
src/Sentry/Protocol/BaseScopeExtensions.cs 86.66% <ø> (ø)
src/Sentry/Protocol/Context/Contexts.cs 90.90% <ø> (ø)
src/Sentry/Protocol/Request.cs 92.00% <ø> (ø)
src/Sentry/Protocol/EnvelopeHeaderCollection.cs 71.42% <71.42%> (ø)
src/Sentry/SentryClient.cs 80.89% <80.00%> (-1.66%) ⬇️
src/Sentry/Protocol/EnvelopePayload.cs 83.33% <83.33%> (ø)
src/Sentry/Protocol/EnvelopeItemCollection.cs 85.71% <85.71%> (ø)
src/Sentry/Internal/Http/HttpTransport.cs 89.47% <87.50%> (+2.80%) ⬆️
src/Sentry/Protocol/Envelope.cs 89.47% <89.47%> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42b0f92...2d9c6aa. Read the comment docs.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 9, 2020

Took your approach @bruno-garcia and made a very hacky implementation.
It works:

image

I first started out with the concept of returning a Stream from Envelope but later realized that implementing a custom HttpContent is way easier.

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

Nice! This is going on a great direction!

@bruno-garcia
Copy link
Member

Where's is the hacky code? :shipit: !

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 9, 2020

Where's is the hacky code? :shipit: !

For example where I do stream.WriteByte((byte) '\n') :D

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 12, 2020

@bruno-garcia went through a couple of iterations, I think this one is okay in regards to streaming events.
I also replaced builders with factory methods as I concluded that it's very unlikely that we will be building "free-form" envelopes (as I assumed originally), but rather generate them from existing structures.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 12, 2020

If this is okay feature-wise, maybe we can make a cut off, fix tests, refactor & merge this? Looking at the plan, it seems that this makes up a rather significant and self-contained portion of the feature set we're aiming for.

(green is done)

image

This way we can keep PRs smaller and break fewer things in between.

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

Nice! we're almost ready to remove the Draft flag it seems?
I left a few notes, and I used the LOGAF scale. Basically L, M, H as in low medium and high.

@codecov-commenter
Copy link

Codecov Report

Merging #544 into main will increase coverage by 0.22%.
The diff coverage is 91.39%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
+ Coverage   85.33%   85.55%   +0.22%     
==========================================
  Files         128      135       +7     
  Lines        3170     3274     +104     
  Branches      730      732       +2     
==========================================
+ Hits         2705     2801      +96     
- Misses        268      276       +8     
  Partials      197      197              
Impacted Files Coverage Δ
...entry/Internal/Extensions/CollectionsExtensions.cs 100.00% <ø> (ø)
src/Sentry/Protocol/BaseScopeExtensions.cs 86.66% <ø> (ø)
src/Sentry/Protocol/Context/Contexts.cs 90.90% <ø> (ø)
src/Sentry/Protocol/Request.cs 92.00% <ø> (ø)
src/Sentry/Protocol/EnvelopeHeaderCollection.cs 71.42% <71.42%> (ø)
src/Sentry/SentryClient.cs 80.89% <80.00%> (-1.66%) ⬇️
src/Sentry/Protocol/EnvelopePayload.cs 83.33% <83.33%> (ø)
src/Sentry/Protocol/EnvelopeItemCollection.cs 85.71% <85.71%> (ø)
src/Sentry/Internal/Http/HttpTransport.cs 89.47% <87.50%> (+2.80%) ⬆️
src/Sentry/Protocol/Envelope.cs 89.47% <89.47%> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42b0f92...87b0f2a. Read the comment docs.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 13, 2020

@bruno-garcia implemented dynamic length calculation, moving on to tests now probably.

@Tyrrrz Tyrrrz marked this pull request as ready for review October 14, 2020 13:43
@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 14, 2020

One more question I realized: should it be Envelope/EnvelopeItem or SentryEnvelope/SentryEnvelopeItem for consistency?

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 14, 2020

Interestingly enough, Arg<T>.Do doesn't seem to actually trigger a callback so tests that rely on it seems to pass regardless of the result.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 14, 2020

Newtonsoft.Json doesn't actually support async serialization. Our best bet is:

public static async Task SerializeToStreamAsync(
            object obj,
            Stream stream,
            CancellationToken cancellationToken = default)
        {
            using var writer = CreateWriter(stream);
            Serializer.Serialize(writer, obj);
            await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
        }

Notably, it's probably going to be the same with System.Text.Json, unless we serialize manually.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 14, 2020

@bruno-garcia some mocking magic is failing, I'm guessing it's because Arg.Is<T>(...) uses an expression for predicate, so it can't handle more involved scenarios. In any case, the test errors don't give any clues:

Error: NotSentry.Tests.HubTests.CaptureMessage_AttachStacktraceTrue_IncludesStackTrace: NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
  Failed NotSentry.Tests.HubTests.CaptureMessage_AttachStacktraceTrue_IncludesStackTrace [20 ms]
  Error Message:
   NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
	EnqueueEnvelope(e => (e.Items.Select(i => i.Payload).OfType().Single().Exception.StackTrace != null))
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
	EnqueueEnvelope(*Envelope*)

  Stack Trace:
     at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable`1 matchingCalls, IEnumerable`1 nonMatchingCalls, Quantity requiredQuantity)
   at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at NSubstitute.Routing.Route.Handle(ICall call)
   at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IBackgroundWorkerProxy_1.EnqueueEnvelope(Envelope envelope)
   at NotSentry.Tests.HubTests.CaptureMessage_AttachStacktraceTrue_IncludesStackTrace() in /home/runner/work/sentry-dotnet/sentry-dotnet/test/Sentry.Tests/HubTests.cs:line 77

@codecov-io
Copy link

codecov-io commented Oct 15, 2020

Codecov Report

Merging #544 into main will decrease coverage by 0.16%.
The diff coverage is 84.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
- Coverage   85.33%   85.16%   -0.17%     
==========================================
  Files         128      137       +9     
  Lines        3170     3317     +147     
  Branches      730      749      +19     
==========================================
+ Hits         2705     2825     +120     
- Misses        268      294      +26     
- Partials      197      198       +1     
Impacted Files Coverage Δ
src/Sentry/Protocol/BaseScopeExtensions.cs 86.66% <ø> (ø)
src/Sentry/Protocol/Context/Contexts.cs 90.90% <ø> (ø)
src/Sentry/Protocol/Request.cs 92.00% <ø> (ø)
src/Sentry/Internal/Polyfills.cs 50.00% <50.00%> (ø)
src/Sentry/Protocol/EnvelopeItem.cs 60.00% <60.00%> (ø)
src/Sentry/Protocol/StreamSerializable.cs 66.66% <66.66%> (ø)
src/Sentry/SentryClient.cs 80.45% <66.66%> (-2.10%) ⬇️
src/Sentry/Internal/Http/HttpTransport.cs 89.47% <88.88%> (+2.80%) ⬆️
src/Sentry/Internal/BackgroundWorker.cs 84.28% <93.18%> (+0.82%) ⬆️
src/Sentry/Protocol/Envelope.cs 96.15% <96.15%> (ø)
... and 22 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42b0f92...88c46e0. Read the comment docs.

@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Oct 15, 2020

This test seems to never have actually worked:

[Fact]
public void CaptureMessage_AttachStacktraceTrue_IncludesStackTrace()
{
_fixture.SentryOptions.AttachStacktrace = true;
var sut = _fixture.GetSut();
_ = sut.CaptureMessage("test");
_ = _fixture.Worker.Received(1)
.EnqueueEvent(Arg.Do<SentryEvent>(
e => Assert.DoesNotContain(e.SentryExceptions.Single().Stacktrace.Frames,
p => p.Function == nameof(CaptureMessage_AttachStacktraceTrue_IncludesStackTrace))));
}

The Arg.Do<> part was just ignored. I couldn't find any logic in code that actually does anything with AttachStackTrace. On top of that, the name of the test seems to mismatch the assertion. So I decided to remove it because it doesn't make sense. Will probably need to add another, proper test.

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

Elegant! Thanks for this @Tyrrrz , great stuff!

@bruno-garcia bruno-garcia merged commit 5dada5b into main Oct 15, 2020
@bruno-garcia bruno-garcia deleted the envelopes branch October 15, 2020 23:50
This was referenced Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants