Skip to content
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

chore(producer): Add produce time by topic #423

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
- id: fix-encoding-pragma
args: ["--remove"]
- repo: https://github.com/pycqa/flake8
rev: 3.8.4
rev: 7.1.1
Copy link
Member Author

Choose a reason for hiding this comment

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

random change to fix my devenv

hooks:
- id: flake8
language_version: python3.12
Expand Down
31 changes: 30 additions & 1 deletion arroyo/backends/kafka/consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import time
from concurrent.futures import Future
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -45,6 +46,7 @@
)
from arroyo.types import BrokerValue, Partition, Topic
from arroyo.utils.concurrent import execute
from arroyo.utils.metrics import get_metrics
from arroyo.utils.retries import BasicRetryPolicy

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -655,6 +657,9 @@ def __init__(self, configuration: Mapping[str, Any]) -> None:
# are fired -- otherwise trying to produce "synchronously" via
# ``produce(...).result()`` could result in a deadlock.
self.__result = execute(self.__worker, daemon=True)
self.__metrics = get_metrics()
self.__last_produce_time_record = 0.0
self.__max_produce_time = 0.0

def __worker(self) -> None:
"""
Expand All @@ -668,13 +673,33 @@ def __worker(self) -> None:
self.__producer.poll(0.1)
self.__producer.flush()

def __record_produce_time(self, topic: str, start_time: float) -> None:
now = time.time()
duration = now - start_time
self.__max_produce_time = max(self.__max_produce_time, duration)

if now - self.__last_produce_time_record <= 0.1:
return

self.__metrics.timing(
"arroyo.producer.max_produce_time",
self.__max_produce_time,
tags={"topic": topic},
)

self.__max_produce_time = 0.0
self.__last_produce_time_record = now

def __delivery_callback(
self,
future: Future[BrokerValue[KafkaPayload]],
payload: KafkaPayload,
error: KafkaError,
message: ConfluentMessage,
start_produce_time: float,
) -> None:
self.__record_produce_time(message.topic(), start_produce_time)

if error is not None:
future.set_exception(TransportError(error))
else:
Expand All @@ -699,6 +724,8 @@ def produce(
destination: Union[Topic, Partition],
payload: KafkaPayload,
) -> Future[BrokerValue[KafkaPayload]]:
start_produce_time = time.time()

if self.__shutdown_requested.is_set():
raise RuntimeError("producer has been closed")

Expand All @@ -719,7 +746,9 @@ def produce(
value=payload.value,
key=payload.key,
headers=payload.headers,
on_delivery=partial(self.__delivery_callback, future, payload),
on_delivery=partial(
self.__delivery_callback, future, payload, start_produce_time
),
)
return future

Expand Down
3 changes: 3 additions & 0 deletions arroyo/utils/metric_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,7 @@
"arroyo.processing.strategies.healthcheck.touch",
# Counter: Number of messages dropped in the FilterStep strategy
"arroyo.strategies.filter.dropped_messages",
# Time: e2e time it takes to produce a message. Tagged by
# physical topic name.
"arroyo.producer.max_produce_time",
]
Loading