From b85a22a711df4c897a9fa8a74f42ac6146542105 Mon Sep 17 00:00:00 2001 From: Ian Davis Date: Tue, 5 Nov 2024 13:37:20 -0800 Subject: [PATCH] Add eval and cell events (#2004) --- pip/qsharp/_ipython.py | 12 +++++++++++- pip/qsharp/_qsharp.py | 13 +++++++++++-- pip/qsharp/telemetry_events.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/pip/qsharp/_ipython.py b/pip/qsharp/_ipython.py index 1879e48285..2ca3a9b6c3 100644 --- a/pip/qsharp/_ipython.py +++ b/pip/qsharp/_ipython.py @@ -8,10 +8,12 @@ execution within Jupyter notebooks. """ +from time import monotonic from IPython.display import display, Javascript, clear_output from IPython.core.magic import register_cell_magic from ._native import QSharpError from ._qsharp import get_interpreter +from . import telemetry_events import pathlib @@ -30,8 +32,16 @@ def callback(output): # is finished executing. display(display_id=True) + telemetry_events.on_run_cell() + start_time = monotonic() + try: - return get_interpreter().interpret(cell, callback) + results = get_interpreter().interpret(cell, callback) + + durationMs = (monotonic() - start_time) * 1000 + telemetry_events.on_run_cell_end(durationMs) + + return results except QSharpError as e: # pylint: disable=raise-missing-from raise QSharpCellError(str(e)) diff --git a/pip/qsharp/_qsharp.py b/pip/qsharp/_qsharp.py index d901621960..6365e9c40e 100644 --- a/pip/qsharp/_qsharp.py +++ b/pip/qsharp/_qsharp.py @@ -205,7 +205,7 @@ def init( _config = Config(target_profile, language_features, manifest_contents, project_root) # Return the configuration information to provide a hint to the # language service through the cell output. - return _config + return _config def get_interpreter() -> Interpreter: @@ -243,7 +243,15 @@ def callback(output: Output) -> None: pass print(output, flush=True) - return get_interpreter().interpret(source, callback) + telemetry_events.on_eval() + start_time = monotonic() + + results = get_interpreter().interpret(source, callback) + + durationMs = (monotonic() - start_time) * 1000 + telemetry_events.on_eval_end(durationMs) + + return results class ShotResult(TypedDict): @@ -387,6 +395,7 @@ def compile(entry_expr: str) -> QirInputData: telemetry_events.on_compile_end(durationMs, target_profile) return res + def circuit( entry_expr: Optional[str] = None, *, operation: Optional[str] = None ) -> Circuit: diff --git a/pip/qsharp/telemetry_events.py b/pip/qsharp/telemetry_events.py index f4865229db..d31666c55f 100644 --- a/pip/qsharp/telemetry_events.py +++ b/pip/qsharp/telemetry_events.py @@ -58,6 +58,36 @@ def on_run_end(durationMs: float, shots: int) -> None: ) +def on_eval() -> None: + log_telemetry( + "qsharp.eval", + 1, + ) + + +def on_eval_end(durationMs: float) -> None: + log_telemetry( + "qsharp.eval.durationMs", + durationMs, + type="histogram", + ) + + +def on_run_cell() -> None: + log_telemetry( + "qsharp.run.cell", + 1, + ) + + +def on_run_cell_end(durationMs: float) -> None: + log_telemetry( + "qsharp.run.cell.durationMs", + durationMs, + type="histogram", + ) + + def on_compile(profile: str) -> None: log_telemetry("qsharp.compile", 1, properties={"profile": profile})