Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(batch-exports): add tracing for S3 consumer (#68256)

Ross1e0ea7251f6e
diff --git a/products/batch_exports/backend/temporal/destinations/s3_batch_export.py b/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
index 021b1556dbae..5cdf173abbd7 100644
--- a/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
@@ -9,6 +9,7 @@ import aioboto3
import botocore.exceptions
from aiobotocore.config import AioConfig
from aiobotocore.session import ClientCreatorContext
+from opentelemetry import trace
if typing.TYPE_CHECKING:
from types_aiobotocore_s3.client import S3Client
@@ -47,7 +48,7 @@ from products.batch_exports.backend.temporal.destinations.constants import (
S3_SUPPORTED_COMPRESSIONS as SUPPORTED_COMPRESSIONS,
)
from products.batch_exports.backend.temporal.destinations.utils import get_manifest_key, get_object_key
-from products.batch_exports.backend.temporal.metrics import ExecutionTimeRecorder
+from products.batch_exports.backend.temporal.metrics import Attributes, CumulativeTimer, ExecutionTimeRecorder
from products.batch_exports.backend.temporal.pipeline.consumer import Consumer, run_consumer_from_stage
from products.batch_exports.backend.temporal.pipeline.entrypoint import execute_batch_export_using_internal_stage
from products.batch_exports.backend.temporal.pipeline.producer import Producer as ProducerFromInternalStage
@@ -99,6 +100,7 @@ COMPRESSION_EXTENSIONS = {
LOGGER = get_write_only_logger(__name__)
EXTERNAL_LOGGER = get_logger("EXTERNAL")
+TRACER = trace.get_tracer(__name__)
class UnsupportedFileFormatError(Exception):
@@ -531,6 +533,10 @@ class ConcurrentS3Consumer(Consumer):
self.part_size = part_size
self.max_concurrent_uploads = max_concurrent_uploads
self.upload_semaphore = asyncio.Semaphore(max_concurrent_uploads)
+ # Time spent blocked waiting for an upload slot (i.e. `max_concurrent_uploads` parts are
+ # already in flight), reported as a span attribute. When this dominates the consumer's
+ # consume time, the bottleneck is upload throughput to the destination.
+ self._upload_slot_wait_timer = CumulativeTimer()
self._session = aioboto3.Session()
self._s3_client: S3Client | None = None # Shared S3 client
@@ -635,6 +641,16 @@ class ConcurrentS3Consumer(Consumer):
# Ensure that we give pending tasks a chance to run.
await asyncio.sleep(0)
+ def reset_tracking(self) -> None:
+ super().reset_tracking()
+ self._upload_slot_wait_timer = CumulativeTimer()
+
+ def get_destination_span_attributes(self) -> Attributes:
+ return {
+ "batch_export.s3.files_uploaded": len(self.files_uploaded),
+ "batch_export.s3.total_upload_slot_wait_seconds": self._upload_slot_wait_timer.total_seconds,
+ }
+
async def _upload_next_part(self, final: bool = False):
"""Extract a part from buffer and upload it"""
if not len(self.current_buffer):
@@ -658,7 +674,8 @@ class ConcurrentS3Consumer(Consumer):
self.part_counter += 1
# Acquire upload semaphore (blocks if too many uploads in flight)
- await self.upload_semaphore.acquire()
+ with self._upload_slot_wait_timer.time():
+ await self.upload_semaphore.acquire()
# Create upload task
upload_task = asyncio.create_task(self._upload_part_with_cleanup(part_data, part_number))
@@ -707,60 +724,75 @@ class ConcurrentS3Consumer(Consumer):
response: UploadPartOutputTypeDef | None = None
attempt = 0
- with ExecutionTimeRecorder(
- "s3_batch_export_upload_part_duration",
- description="Total duration of the upload of a part of a multi-part upload",
- log_message=(
- "Finished uploading file number %(file_number)d part %(part_number)d"
- " with upload id '%(upload_id)s' with status '%(status)s'."
- " File size: %(mb_processed).2f MB, upload time: %(duration_seconds)d"
- " seconds, speed: %(mb_per_second).2f MB/s"
- ),
- log_attributes={
- "file_number": self.current_file_index,
- "upload_id": self.upload_id,
- "part_number": part_number,
- },
- ) as recorder:
+ with (
+ TRACER.start_as_current_span(
+ "batch_export.s3.upload_part",
+ attributes={
+ "batch_export.s3.file_number": self.current_file_index,
+ "batch_export.s3.part_number": part_number,
+ "batch_export.s3.part_bytes": len(data),
+ },
+ ) as span,
+ ExecutionTimeRecorder(
+ "s3_batch_export_upload_part_duration",
+ description="Total duration of the upload of a part of a multi-part upload",
+ log_message=(
+ "Finished uploading file number %(file_number)d part %(part_number)d"
+ " with upload id '%(upload_id)s' with status '%(status)s'."
+ " File size: %(mb_processed).2f MB, upload time: %(duration_seconds)d"
+ " seconds, speed: %(mb_per_second).2f MB/s"
+ ),
+ log_attributes={
+ "file_number": self.current_file_index,
+ "upload_id": self.upload_id,
+ "part_number": part_number,
+ },
+ ) as recorder,
+ ):
recorder.add_bytes_processed(len(data))
- while response is None:
- try:
- response = await client.upload_part(
- Bucket=self.bucket,
- Key=current_key,
- PartNumber=part_number,
- UploadId=self.upload_id,
- Body=data,
- **optional_kwargs, # type: ignore
- )
-
- except botocore.exceptions.ClientError as err:
- error_code = err.response.get("Error", {}).get("Code", None)
+ try:
+ while response is None:
attempt += 1
+ try:
+ response = await client.upload_part(
+ Bucket=self.bucket,
+ Key=current_key,
+ PartNumber=part_number,
+ UploadId=self.upload_id,
+ Body=data,
+ **optional_kwargs, # type: ignore
+ )
+
+ except botocore.exceptions.ClientError as err:
+ error_code = err.response.get("Error", {}).get("Code", None)
- self.logger.warning(
- "Caught ClientError while uploading file %s part %s: %s (attempt %s/%s)",
- self.current_file_index,
- part_number,
- error_code,
- attempt,
- self.UPLOAD_PART_MAX_ATTEMPTS,
- )
-
- if error_code is not None and error_code == "RequestTimeout":
- if attempt >= self.UPLOAD_PART_MAX_ATTEMPTS:
- raise IntermittentUploadPartTimeoutError(part_number=part_number) from err
-
- retry_delay = min(
- self.MAX_RETRY_DELAY,
- self.INITIAL_RETRY_DELAY * (attempt**self.EXPONENTIAL_BACKOFF_COEFFICIENT),
+ self.logger.warning(
+ "Caught ClientError while uploading file %s part %s: %s (attempt %s/%s)",
+ self.current_file_index,
+ part_number,
+ error_code,
+ attempt,
+ self.UPLOAD_PART_MAX_ATTEMPTS,
)
- self.logger.warning("Retrying part %s upload in %s seconds", part_number, retry_delay)
- await asyncio.sleep(retry_delay)
- continue
- else:
- raise
+
+ if error_code is not None and error_code == "RequestTimeout":
+ if attempt >= self.UPLOAD_PART_MAX_ATTEMPTS:
+ raise IntermittentUploadPartTimeoutError(part_number=part_number) from err
+
+ retry_delay = min(
+ self.MAX_RETRY_DELAY,
+ self.INITIAL_RETRY_DELAY * (attempt**self.EXPONENTIAL_BACKOFF_COEFFICIENT),
+ )
+ self.logger.warning("Retrying part %s upload in %s seconds", part_number, retry_delay)
+ await asyncio.sleep(retry_delay)
+ continue
+ else:
+ raise
+ finally:
+ # Backoff sleeps happen inside this span, so the count also explains durations
+ # inflated by retries.
+ span.set_attribute("batch_export.s3.upload_attempts", attempt)
part_info: CompletedPartTypeDef = {
"ETag": response["ETag"],
@@ -872,11 +904,15 @@ class ConcurrentS3Consumer(Consumer):
current_key = self._get_current_key()
client = await self._get_s3_client()
- response = await client.create_multipart_upload(
- Bucket=self.bucket,
- Key=current_key,
- **optional_kwargs, # type: ignore
- )
+ with TRACER.start_as_current_span(
+ "batch_export.s3.create_multipart_upload",
+ attributes={"batch_export.s3.file_number": self.current_file_index},
+ ):
+ response = await client.create_multipart_upload(
+ Bucket=self.bucket,
+ Key=current_key,
+ **optional_kwargs, # type: ignore
+ )
self.upload_id = response["UploadId"]
self.logger.debug("Initialized multipart upload for key %s with upload id %s", current_key, self.upload_id)
@@ -927,12 +963,13 @@ class ConcurrentS3Consumer(Consumer):
if self.endpoint_url is None:
optional_kwargs["ChecksumAlgorithm"] = "CRC64NVME"
- await client.put_object(
- Bucket=self.bucket,
- Key=manifest_key,
- Body=json.dumps({"files": files_uploaded}),
- **optional_kwargs, # type: ignore
- )
+ with TRACER.start_as_current_span("batch_export.s3.upload_manifest"):
+ await client.put_object(
+ Bucket=self.bucket,
+ Key=manifest_key,
+ Body=json.dumps({"files": files_uploaded}),
+ **optional_kwargs, # type: ignore
+ )
if self._s3_client is not None and self._s3_client_ctx is not None:
await self._s3_client_ctx.__aexit__(None, None, None)
@@ -959,12 +996,19 @@ class ConcurrentS3Consumer(Consumer):
current_key = self._get_current_key()
client = await self._get_s3_client()
- await client.complete_multipart_upload(
- Bucket=self.bucket,
- Key=current_key,
- UploadId=self.upload_id,
- MultipartUpload={"Parts": sorted_parts},
- )
+ with TRACER.start_as_current_span(
+ "batch_export.s3.complete_multipart_upload",
+ attributes={
+ "batch_export.s3.file_number": self.current_file_index,
+ "batch_export.s3.num_parts": len(sorted_parts),
+ },
+ ):
+ await client.complete_multipart_upload(
+ Bucket=self.bucket,
+ Key=current_key,
+ UploadId=self.upload_id,
+ MultipartUpload={"Parts": sorted_parts},
+ )
async def _abort(self):
"""Abort this S3 multi-part upload and cancel any in-flight part uploads."""
diff --git a/products/batch_exports/backend/temporal/pipeline/consumer.py b/products/batch_exports/backend/temporal/pipeline/consumer.py
index 9ad383cda614..85380373e279 100644
--- a/products/batch_exports/backend/temporal/pipeline/consumer.py
+++ b/products/batch_exports/backend/temporal/pipeline/consumer.py
@@ -12,6 +12,7 @@ from opentelemetry import trace
from posthog.temporal.common.logger import get_logger, get_write_only_logger
from products.batch_exports.backend.temporal.metrics import (
+ Attributes,
CumulativeTimer,
get_bytes_exported_metric,
get_rows_exported_metric,
@@ -174,9 +175,18 @@ class Consumer:
"batch_export.consumer.total_queue_get_wait_seconds": queue_get_wait_seconds,
"batch_export.consumer.total_consume_seconds": consume_seconds,
"batch_export.consumer.total_transform_seconds": transform_seconds,
+ **self.get_destination_span_attributes(),
}
)
+ def get_destination_span_attributes(self) -> Attributes:
+ """Destination-specific attributes to report on the consumer span.
+
+ Subclasses can override this to break down where their consume time goes
+ (e.g. time blocked on destination upload capacity).
+ """
+ return {}
+
def collect_result(self) -> BatchExportResult:
"""Collect the result of the consumer.