-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(spans): Flush oversized segments in chunks #111820
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from collections.abc import Callable, Mapping | ||
| from concurrent.futures import Future | ||
| from functools import partial | ||
| from typing import Any | ||
|
|
||
| import orjson | ||
| import sentry_sdk | ||
|
|
@@ -35,6 +36,42 @@ | |
| type ProduceToPipe = Callable[[int, KafkaPayload, int], None] | ||
|
|
||
|
|
||
| type SpanPayload = dict[str, Any] | ||
|
|
||
|
|
||
| def _chunk_segment(span_payloads: list[SpanPayload]) -> list[list[SpanPayload]]: | ||
| """ | ||
| Split a segment into chunks of spans payloads that fit under max_segment_bytes. | ||
| If all spans of the segment fit in one chunk, returns a single-element list. | ||
| """ | ||
| max_segment_bytes = options.get("spans.buffer.max-segment-bytes") | ||
|
|
||
| sizes = [len(orjson.dumps(s)) for s in span_payloads] | ||
| total_size = sum(sizes) | ||
| if total_size <= max_segment_bytes: | ||
| return [span_payloads] | ||
|
|
||
| chunks: list[list[SpanPayload]] = [] | ||
| current_chunk: list[SpanPayload] = [] | ||
| current_size = 0 | ||
|
|
||
| for payload, payload_size in zip(span_payloads, sizes): | ||
| if current_chunk and current_size + payload_size > max_segment_bytes: | ||
| chunks.append(current_chunk) | ||
| current_chunk = [] | ||
| current_size = 0 | ||
| current_chunk.append(payload) | ||
| current_size += payload_size | ||
|
|
||
| if current_chunk: | ||
| chunks.append(current_chunk) | ||
|
|
||
| if len(chunks) > 1: | ||
| metrics.incr("spans.buffer.flusher.oversized_segments_chunked") | ||
|
|
||
| return chunks | ||
|
|
||
|
|
||
| class MultiProducer: | ||
| """ | ||
| Manages multiple Kafka producers for load balancing across brokers/topics. | ||
|
|
@@ -303,6 +340,7 @@ def produce(project_id: int, payload: KafkaPayload, dropped: int) -> None: | |
|
|
||
| first_iteration = True | ||
| while not stopped.value: | ||
| flush_oversized_segments = options.get("spans.buffer.flush-oversized-segments") | ||
| system_now = int(time.time()) | ||
| now = system_now + current_drift.value | ||
| flushed_segments = buffer.flush_segments(now=now) | ||
|
|
@@ -334,13 +372,20 @@ def produce(project_id: int, payload: KafkaPayload, dropped: int) -> None: | |
| continue | ||
|
|
||
| spans = [span.payload for span in flushed_segment.spans] | ||
| kafka_payload = KafkaPayload(None, orjson.dumps({"spans": spans}), []) | ||
| metrics.timing( | ||
| "spans.buffer.segment_size_bytes", | ||
| len(kafka_payload.value), | ||
| tags={"shard": shard_tag}, | ||
| ) | ||
| produce(flushed_segment.project_id, kafka_payload, len(spans)) | ||
|
|
||
| if flush_oversized_segments: | ||
| chunks = _chunk_segment(spans) | ||
| else: | ||
| chunks = [spans] | ||
|
|
||
| for chunk in chunks: | ||
| kafka_payload = KafkaPayload(None, orjson.dumps({"spans": chunk}), []) | ||
| metrics.timing( | ||
| "spans.buffer.segment_size_bytes", | ||
| len(kafka_payload.value), | ||
| tags={"shard": shard_tag}, | ||
| ) | ||
| produce(flushed_segment.project_id, kafka_payload, len(chunk)) | ||
|
Comment on lines
+375
to
+388
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this logic belongs here. Deciding to chunk the segment is a business logic concern not a kafka consumer concern. This logic should go into the buffer.py file. |
||
|
|
||
| with metrics.timer("spans.buffer.flusher.wait_produce", tags={"shards": shard_tag}): | ||
| for project_id, future, dropped in producer_futures: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also track a distribution on the number of chunks we have?
metrics.timing(..., len(chunks))?