-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent_recorder.py
More file actions
35 lines (29 loc) · 1.29 KB
/
agent_recorder.py
File metadata and controls
35 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import grpc
import logging
from haystack.recorder import SpanRecorder
from haystack.agent import spanAgent_pb2_grpc
from haystack.util import span_to_proto
logger = logging.getLogger(__name__)
class HaystackAgentRecorder(SpanRecorder):
"""
HaystackAgentRecorder is to be used with the haystack-agent described
"""
def __init__(self, agent_host="haystack-agent", agent_port=35000):
logger.info("Initializing the remote grpc agent recorder, connecting "
f"at {agent_host}:{agent_port}")
channel = grpc.insecure_channel(f"{agent_host}:{agent_port}")
self._stub = spanAgent_pb2_grpc.SpanAgentStub(channel)
@staticmethod
def process_response(future):
try:
grpc_response = future.result()
if grpc_response.code != 0:
logger.error(f"Dispatch failed with {grpc_response.code} due "
f"to {grpc_response.error_message}")
else:
logger.debug("Successfully submitted span to haystack-agent")
except grpc.RpcError:
logger.exception("Dispatch failed due to RPC error")
def record_span(self, span):
future = self._stub.dispatch.future(span_to_proto(span))
future.add_done_callback(HaystackAgentRecorder.process_response)