Skip to content

Conversation

@buenaflor
Copy link
Contributor

@buenaflor buenaflor commented Dec 16, 2025

📜 Description

Add basic APIs for processing telemetry types e.g log, span

#skip-changelog

💡 Motivation and Context

Part of #3333

💚 How did you test it?

Unit test

📝 Checklist

  • I reviewed submitted code
  • I added tests to verify changes
  • No new PII added or SDK only sends newly added PII if sendDefaultPii is enabled
  • I updated the docs if needed
  • All tests passing
  • No breaking changes

🔮 Next steps

…metry processing classes

- Added `TelemetryItem` interface for consistent serialization of telemetry data.
- Implemented `TelemetryBuffer` and `TelemetryProcessor` for managing telemetry items.
- Enhanced `Span` and `SentryLog` classes to implement `TelemetryItem`.
- Updated `SimpleSpan` and `NoOpSpan` to support new telemetry features.
- Added tests for segment span behavior and trace ID propagation in spans.
…n and mock implementation

- Updated `DefaultTelemetryProcessor` to include a method for registering telemetry buffers.
- Refactored the constructor to accept a logger callback instead of Sentry options.
- Introduced `MockTelemetryBuffer` for testing purposes, allowing simulation of buffer behavior.
- Added comprehensive tests for the `DefaultTelemetryProcessor`, covering initialization, item addition, and buffer flushing.
…ocessor tests

- Deleted the initialization test group in `telemetry_processor_test.dart` as the tests were marked with TODOs and not implemented. This cleanup improves the clarity and focus of the test suite.
@buenaflor buenaflor requested a review from denrase as a code owner December 16, 2025 17:31
- Added a `telemetryProcessor` field to `SentryOptions`, initialized with a `NoOpTelemetryProcessor`.
- Enhanced `DefaultTelemetryProcessor` to manage span and log buffers based on Sentry options.
- Implemented methods for adding spans and logs, along with flushing buffers for telemetry data transmission.
- Introduced a `NoOpTelemetryProcessor` for scenarios where telemetry is not enabled.
…emetryProcessor

- Removed unnecessary comments regarding buffer creation based on options.
- Updated tests to reflect changes in method names for adding spans and logs.
- Enhanced test coverage for handling cases where buffers may not be registered, ensuring no exceptions are thrown.
- Introduced a test subclass to facilitate mock buffer creation for testing purposes.
…g processing

- Introduced `InMemoryTelemetryBuffer` class for managing telemetry items with time and size-based flushing.
- Updated `DefaultTelemetryProcessor` to utilize `InMemoryTelemetryBuffer` for span and log buffering.
- Removed obsolete buffer creation methods to streamline buffer initialization.
- Adjusted tests to instantiate `DefaultTelemetryProcessor` directly, enhancing clarity and consistency.
- Updated the SimpleSpan constructor to inherit traceId from the parent span if available, ensuring consistent trace propagation.
- Removed the condition that set 'is_segment' to true when there is no parent span, simplifying the span representation.
- Added a test to verify that child spans correctly inherit the traceId from their parent, even after resetting the propagation context.
buenaflor and others added 3 commits December 16, 2025 21:45
…ent instance

- Updated the segmentSpan getter in NoOpSpan to return the current instance instead of a new NoOpSpan, ensuring proper span behavior and consistency in span hierarchy.
…buffer factories

- Introduced `TelemetryBufferFactory` type for creating telemetry buffers, allowing for custom buffer implementations.
- Updated `DefaultTelemetryProcessor` constructor to accept optional buffer factory parameters for spans and logs, defaulting to `InMemoryTelemetryBuffer`.
- Modified tests to utilize mock buffers, improving test flexibility and clarity in buffer handling.
…tamps

- Changed the initialization of _startTimestamp to use a configurable clock from the Hub options instead of the current UTC time.
- Updated the _endTimestamp assignment to also utilize the Hub's clock, ensuring consistent time handling across spans.
…uffer factory parameters

- Removed the optional buffer factory parameters from the DefaultTelemetryProcessor constructor, directly accepting span and log buffers instead.
- Streamlined the addSpan and addLog methods to utilize a common _add method for handling telemetry items.
- Updated tests to reflect changes in the DefaultTelemetryProcessor instantiation, enhancing clarity in buffer management.
- Eliminated the import of `telemetry_item.dart` in `noop_span.dart` as it is no longer needed.
- Streamlined the `NoOpSpan` class to focus on its core functionality without unnecessary dependencies.
…face

- Updated SentryLog and Span classes to implement the new SentryEncodable interface instead of the deprecated TelemetryItem.
- Refactored TelemetryBuffer and related classes to work with SentryEncodable, enhancing type safety and consistency across telemetry processing.
- Introduced SentryEncodable interface to define a common method for JSON serialization, streamlining the encoding process for telemetry items.
…uffer

- Introduced a description for the SentryEncodable interface, clarifying its purpose for JSON serialization.
- Added documentation for the TelemetryBuffer class, explaining its functionality in batching telemetry items for efficient transmission to Sentry.
…ructor

- Updated the SimpleSpan constructor to conditionally inherit traceId from the parent span only if it is not empty, enhancing trace propagation accuracy.
- Simplified the logic for determining the traceId, ensuring clearer and more reliable span behavior.
- Updated the assignment of _endTimestamp in SimpleSpan to convert endTimestamp to UTC if provided, ensuring consistent timestamp handling across spans.
…he buffer

- Updated the addSpan method in DefaultTelemetryProcessor to ignore NoOpSpan and UnsetSpan instances, ensuring they are not added to the telemetry buffer.
- Added unit tests to verify that NoOpSpan and UnsetSpan cannot be added to the buffer, enhancing the robustness of span handling.
- Added documentation comments for the addSpan, addLog, and flush methods in the TelemetryProcessor interface, clarifying their functionality and usage.
- Improved code readability and maintainability by providing clear descriptions of method behaviors and expected outcomes.
…tTelemetryProcessor

- Deleted the documentation comments for the flush method in DefaultTelemetryProcessor, as they were no longer relevant.
- This change helps maintain clarity and accuracy in the codebase by ensuring that documentation reflects the current implementation.
…TelemetryProcessor

- Deleted the documentation comment for the constructor in DefaultTelemetryProcessor, as it was no longer relevant.
- This change helps maintain clarity and accuracy in the codebase by ensuring that documentation reflects the current implementation.
…lated classes

- Eliminated the SentryEncodable interface, removing its implementation from SentryLog and Span classes.
- Updated TelemetryBuffer and its subclasses to no longer depend on SentryEncodable, enhancing type flexibility.
- Adjusted related methods to accommodate the removal of the interface, streamlining the telemetry processing codebase.
…lated types

- Renamed BufferedItem class to EncodedItem to better reflect its purpose of pairing items with their encoded bytes for size tracking and transmission.
- Updated InMemoryTelemetryBuffer to use an ItemEncoder type instead of a serializer function, enhancing clarity in item encoding.
- Adjusted the add method to utilize the new encoder, streamlining the telemetry processing workflow.
- Updated the traceId assignment logic in the SimpleSpan constructor to enhance clarity by removing unnecessary null checks.
- Added an override for the toJson method, ensuring proper JSON serialization of the SimpleSpan instance.
- Added telemetry processing capabilities to SentryClient by introducing DefaultTelemetryProcessor and InMemoryTelemetryBuffer for log and span handling.
- Updated the constructor to initialize the telemetry processor, preparing for future removal of the log batcher.
- Renamed EncodedItem to BufferedItem to better represent its role in telemetry data management.
…classes

- Introduced JsonEncodable interface to standardize JSON serialization across telemetry-related classes.
- Updated SentryLog and Span classes to implement the new interface, ensuring consistent toJson method implementation.
- Simplified InMemoryTelemetryBuffer by removing the encoder parameter, directly utilizing the toJson method for item encoding.
…ce SentryClient tests

- Removed unused import from sentry_client.dart to clean up the code.
- Updated InMemoryTelemetryBuffer's add method to use a temporary variable for BufferedItem, improving clarity.
- Enhanced SentryClient tests by adding a new group to verify the default telemetry processor initialization, ensuring proper setup during client instantiation.
void add(T item);

/// Manually sends all buffered items to Sentry and clears the buffer.
FutureOr<void> flush();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't check in the other PR, but in Cocoa we had the discussion if this should be called clear (or similar) instead of flush, up to you if you want to change this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change it, don't have a strong opinion on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


void _add(dynamic item) {
final buffer = switch (item) {
Span() => spanBuffer,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can check the type with an instance Span() ? TIL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

afaik it's not actually creating a new instance, it's just pattern (Dart 3+ feature) since you could also do

e.g Span(length: int) => length + 1

I updated it to

final buffer = switch (item) {
  Span _ => spanBuffer,
  SentryLog _ => logBuffer,
  _ => null,
};

- Updated TelemetryBuffer and its implementations to rename the flush method to clear, reflecting its functionality of sending and clearing buffered items.
- Adjusted SentryClient to include a TODO for future telemetry processor integration.
- Enhanced documentation to clarify the purpose of the clear method in telemetry processing.
- Updated MockTelemetryBuffer to rename the flush method to clear, aligning with its intended functionality of clearing buffered items.
- Adjusted related test cases to reflect the method name change, ensuring consistency across the telemetry processing tests.
- Changed the type of futures in DefaultTelemetryProcessor from Future<void> to Future, allowing for more flexibility in handling future results.
- This adjustment improves the clarity and usability of the telemetry processing logic.
…Buffer

- Updated the MockTelemetryBuffer class to rename the asyncFlush parameter to asyncClear, aligning with the method's functionality of clearing items.
- Adjusted related test cases to reflect this change, ensuring consistency in the telemetry processing tests.
@buenaflor buenaflor merged commit bb21a6f into feat/span-first Dec 17, 2025
94 of 133 checks passed
@buenaflor buenaflor deleted the feat/buffer/pr-1-telemetry-item-interface branch December 17, 2025 17:09
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