Skip to content

Wire log_byte client outcome reporting alongside log_item#8185

Open
tsushanth wants to merge 1 commit into
getsentry:mainfrom
tsushanth:fix/wire-log-byte-client-outcome
Open

Wire log_byte client outcome reporting alongside log_item#8185
tsushanth wants to merge 1 commit into
getsentry:mainfrom
tsushanth:fix/wire-log-byte-client-outcome

Conversation

@tsushanth

Copy link
Copy Markdown
Contributor

Fixes #8175

The log_byte data category is defined in the client reports spec but was not tracked in sentry-cocoa. Only log_item (the item count) was reported; the byte-count counterpart was missing.

What this changes

SentryDataCategory.h — adds kSentryDataCategoryLogByte = 15 and shifts kSentryDataCategoryUnknown to 16 to preserve the existing integer sequence.

SentryDataCategoryMapper.h / .m — adds the kSentryDataCategoryNameLogByte constant ("log_byte") and wires it into sentryDataCategoryForString and nameForSentryDataCategory. The envelope-item-type mapper does not need a new branch because log_byte is a client-report category, not an envelope item type — the log item type already maps to log_item.

SentryHttpTransport.m — adds a private helper recordLostLogBytes:reason: that records item.data.length bytes under kSentryDataCategoryLogByte when a log envelope item is dropped. The helper is called alongside the existing recordLostEvent / recordLostSpans calls in three drop sites:

  • envelopeItemDropped:withCategory: (rate-limit backoff)
  • envelopeItemDeleted:withCategory: (cache overflow)
  • recordLostEventFor: (send error)

The beforeSendLog path in SentryClient.m already records log_item for the rejected log record. No byte count is available there because the log has not been serialized yet, so log_byte is intentionally not recorded at that site — this matches the behaviour of other SDKs that only count bytes at the transport boundary.

SentryDataCategoryMapperTests.swift — extends the integer, string-to-category, and name-to-category round-trip tests to cover the new logByte case and updates the .unknown integer assertion from 15 to 16.

Adds kSentryDataCategoryLogByte (= "log_byte") as a new data category
alongside the existing kSentryDataCategoryLogItem. The byte category
tracks how many bytes of log envelope data were discarded, matching the
client-reports spec at https://develop.sentry.dev/sdk/telemetry/client-reports/

Changes:
- SentryDataCategory.h: add kSentryDataCategoryLogByte = 15; shift
  kSentryDataCategoryUnknown to 16
- SentryDataCategoryMapper.h/m: add kSentryDataCategoryNameLogByte
  constant ("log_byte") and wire it through sentryDataCategoryForString,
  nameForSentryDataCategory
- SentryHttpTransport.m: add recordLostLogBytes:reason: helper that
  records item.data.length bytes under kSentryDataCategoryLogByte; call
  it alongside the existing recordLostEvent / recordLostSpans calls in
  envelopeItemDropped:withCategory:, envelopeItemDeleted:withCategory:,
  and recordLostEventFor:
- SentryDataCategoryMapperTests.swift: extend integer, string, and
  name-to-category round-trip tests to cover kSentryDataCategoryLogByte
@NinjaLikesCheez

Copy link
Copy Markdown
Member

Thanks for the contribution! Allowing the CI to run - the code looks good to me, but I'd like a team member with more context on this type of change to weigh in and approve first.

@philprime philprime left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Almost LGTM, we have one topic to discuss

- (void)recordLostLogBytes:(SentryEnvelopeItem *)envelopeItem reason:(SentryDiscardReason)reason
{
if ([SentryEnvelopeItemTypes.log isEqualToString:envelopeItem.type]) {
NSUInteger byteCount = envelopeItem.data.length;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: We need to double-check if the data property is the actual size of the log item as we would count it server-side, or if it also contains boilerplate bytes like JSON encoding, attributes etc. For transaction we are actually decoding the data first.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The byte size does not need to be exact. SDKs MAY use an approximation, such as the in-memory size of the log object or the size of its serialized representation. The goal is to provide a useful signal, not a precise measurement. - develop docs

@philprime I think that answers it. Python also just gets the bytes from the log envelope item seehttps://github.com/getsentry/sentry-python/blob/7787e6dcb7212b65aaabb1808795f1fed5abc7e9/sentry_sdk/transport.py#L307-L310

@philprime

Copy link
Copy Markdown
Member

We also need a proper changelog entry for this

@philipphofmann philipphofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot for tackling this @tsushanth.

- (void)recordLostLogBytes:(SentryEnvelopeItem *)envelopeItem reason:(SentryDiscardReason)reason
{
if ([SentryEnvelopeItemTypes.log isEqualToString:envelopeItem.type]) {
NSUInteger byteCount = envelopeItem.data.length;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The byte size does not need to be exact. SDKs MAY use an approximation, such as the in-memory size of the log object or the size of its serialized representation. The goal is to provide a useful signal, not a precise measurement. - develop docs

@philprime I think that answers it. Python also just gets the bytes from the log envelope item seehttps://github.com/getsentry/sentry-python/blob/7787e6dcb7212b65aaabb1808795f1fed5abc7e9/sentry_sdk/transport.py#L307-L310

{
if ([SentryEnvelopeItemTypes.log isEqualToString:envelopeItem.type]) {
NSUInteger byteCount = envelopeItem.data.length;
if (byteCount > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

l: No need for this check. Even if the byte count is 0 for whatever reason, we should still record it — that surfaces in Sentry that we dropped a log envelope item with no data, which normally shouldn't happen. This also matches the Python SDK, which records the byte count unconditionally.

}
}

- (void)recordLostLogBytes:(SentryEnvelopeItem *)envelopeItem reason:(SentryDiscardReason)reason

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

h: We must add unit tests also for this new logic, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Use this label to trigger all PR workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wire log_byte client outcome reporting

4 participants