1+ """
2+ Copyright (c) 2024 Scale3 Labs
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ """
16+
17+ from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
18+ from opentelemetry .trace import get_tracer
19+ from wrapt import wrap_function_wrapper as _W
20+ from typing import Collection
21+ from importlib_metadata import version as v
22+ from .patch import patch_agent , patch_memory
23+
24+ class PhiDataInstrumentation (BaseInstrumentor ):
25+ def instrumentation_dependencies (self ) -> Collection [str ]:
26+ return ["phidata >= 2.7.10" ] # Adjust version as needed
27+
28+ def _instrument (self , ** kwargs ):
29+ tracer_provider = kwargs .get ("tracer_provider" )
30+ tracer = get_tracer (__name__ , "" , tracer_provider )
31+ version = v ("phidata" )
32+
33+ try :
34+ _W (
35+ "phi.agent.agent" ,
36+ "Agent.run" ,
37+ patch_agent ("Agent.run" , version , tracer ),
38+ )
39+ _W (
40+ "phi.agent.agent" ,
41+ "Agent.arun" ,
42+ patch_agent ("Agent.arun" , version , tracer ),
43+ )
44+ _W (
45+ "phi.agent.agent" ,
46+ "Agent._run" ,
47+ patch_agent ("Agent._run" , version , tracer ),
48+ )
49+ _W (
50+ "phi.agent.agent" ,
51+ "Agent._arun" ,
52+ patch_agent ("Agent._arun" , version , tracer ),
53+ )
54+
55+ _W (
56+ "phi.memory.agent" ,
57+ "AgentMemory.update_memory" ,
58+ patch_memory ("AgentMemory.update_memory" , version , tracer ),
59+ )
60+ _W (
61+ "phi.memory.agent" ,
62+ "AgentMemory.aupdate_memory" ,
63+ patch_memory ("AgentMemory.aupdate_memory" , version , tracer ),
64+ )
65+ _W (
66+ "phi.memory.agent" ,
67+ "AgentMemory.update_summary" ,
68+ patch_memory ("AgentMemory.update_summary" , version , tracer ),
69+ )
70+ _W (
71+ "phi.memory.agent" ,
72+ "AgentMemory.aupdate_summary" ,
73+ patch_memory ("AgentMemory.aupdate_summary" , version , tracer ),
74+ )
75+
76+ except Exception :
77+ pass
78+
79+ def _uninstrument (self , ** kwargs ):
80+ pass
0 commit comments