44import logging
55from pathlib import Path
66import json
7- import yaml
7+ import datetime
88
9+ import yaml
910import posthog
1011
1112from haystack .environment import collect_static_system_specs , collect_dynamic_system_specs
@@ -103,7 +104,8 @@ def send_event(self, event_name: str, event_properties: Optional[Dict[str, Any]]
103104
104105
105106def send_pipeline_run_event ( # type: ignore
106- event_name : str ,
107+ classname : str ,
108+ function_name : str ,
107109 pipeline : "Pipeline" , # type: ignore
108110 query : Optional [str ] = None ,
109111 queries : Optional [List [str ]] = None ,
@@ -117,7 +119,8 @@ def send_pipeline_run_event( # type: ignore
117119 """
118120 Sends a telemetry event about the execution of a pipeline, if telemetry is enabled.
119121
120- :param event_name: The name of the event to show in PostHog.
122+ :param classname: The name of the Pipeline class (Pipeline, RayPipeline, ...)
123+ :param function_name: The name of the function that was invoked (run, run_batch, async_run, ...).
121124 :param pipeline: the pipeline that is running
122125 :param query: the value of the `query` input of the pipeline, if any
123126 :param queries: the value of the `queries` input of the pipeline, if any
@@ -130,15 +133,18 @@ def send_pipeline_run_event( # type: ignore
130133 """
131134 try :
132135 if telemetry :
133- event_properties : Dict [str , Optional [Union [str , bool , int , Dict [str , Any ]]]] = {}
136+ event_properties : Dict [str , Optional [Union [str , bool , int , Dict [str , Any ]]]] = {
137+ "class" : classname ,
138+ "function_name" : function_name ,
139+ }
134140
135141 # Check if it's the public demo
136142 exec_context = os .environ .get (HAYSTACK_EXECUTION_CONTEXT , "" )
137143 if exec_context == "public_demo" :
138144 event_properties ["pipeline.is_public_demo" ] = True
139145 event_properties ["pipeline.run_parameters.query" ] = query
140146 event_properties ["pipeline.run_parameters.params" ] = params
141- telemetry .send_event (event_name = event_name , event_properties = event_properties )
147+ telemetry .send_event (event_name = function_name , event_properties = event_properties )
142148 return
143149
144150 # Collect pipeline profile
@@ -185,26 +191,35 @@ def send_pipeline_run_event( # type: ignore
185191 event_properties ["pipeline.run_parameters.params" ] = bool (params )
186192 event_properties ["pipeline.run_parameters.debug" ] = bool (debug )
187193
188- telemetry .send_event (event_name = event_name , event_properties = event_properties )
194+ telemetry .send_event (event_name = "Pipeline run" , event_properties = event_properties )
189195 except Exception as e :
190196 # Never let telemetry break things
191- logger .debug ("There was an issue sending a %s telemetry event" , event_name , exc_info = e )
197+ logger .debug ("There was an issue sending a '%s' telemetry event" , function_name , exc_info = e )
192198
193199
194- def send_pipeline_event (pipeline : "Pipeline" , event_name : str ): # type: ignore
200+ def send_pipeline_event (pipeline : "Pipeline" , event_name : str , event_properties : Optional [ Dict [ str , Any ]] = None ): # type: ignore
195201 """
196202 Send a telemetry event related to a pipeline which is not a call to run(), if telemetry is enabled.
197203 """
198204 try :
199205 if telemetry :
200- telemetry .send_event (
201- event_name = event_name ,
202- event_properties = {
206+ if not event_properties :
207+ event_properties = {}
208+ event_properties .update (
209+ {
203210 "pipeline.classname" : pipeline .__class__ .__name__ ,
204211 "pipeline.fingerprint" : pipeline .fingerprint ,
205212 "pipeline.yaml_hash" : pipeline .yaml_hash ,
206- },
213+ }
207214 )
215+ now = datetime .datetime .now ()
216+ if pipeline .last_run :
217+ event_properties ["pipeline.since_last_run" ] = (now - pipeline .last_run ).total_seconds ()
218+ else :
219+ event_properties ["pipeline.since_last_run" ] = 0
220+ pipeline .last_run = now
221+
222+ telemetry .send_event (event_name = event_name , event_properties = event_properties )
208223 except Exception as e :
209224 # Never let telemetry break things
210225 logger .debug ("There was an issue sending a '%s' telemetry event" , event_name , exc_info = e )
@@ -222,16 +237,6 @@ def send_event(event_name: str, event_properties: Optional[Dict[str, Any]] = Non
222237 logger .debug ("There was an issue sending a '%s' telemetry event" , event_name , exc_info = e )
223238
224239
225- def _serializer (obj ):
226- """
227- Small function used to build pipeline fingerprints and safely serialize any object.
228- """
229- try :
230- return str (obj )
231- except :
232- return "~ non serializable object ~"
233-
234-
235240if os .environ .get ("HAYSTACK_TELEMETRY_VERSION" , "2" ) == "2" :
236241 telemetry = Telemetry ()
237242else :
0 commit comments