33## ⚡ TL;DR - 3 Step Process
44
551 . ** Import the logger** : ` from codegen.shared.logging.get_logger import get_logger `
6- 2 . ** Add ` extra={} ` to your log calls** : ` logger.info("message", extra={"key": "value"}) `
7- 3 . ** Enable telemetry** : ` codegen config telemetry enable `
6+ 1 . ** Add ` extra={} ` to your log calls** : ` logger.info("message", extra={"key": "value"}) `
7+ 1 . ** Enable telemetry** : ` codegen config telemetry enable `
88
99** That's it!** Your logs automatically go to Grafana Cloud when telemetry is enabled.
1010
@@ -22,11 +22,14 @@ from codegen.shared.logging.get_logger import get_logger
2222logger = get_logger(__name__ )
2323
2424# Find any existing console.print() or error handling and add:
25- logger.info(" Operation completed" , extra = {
26- " operation" : " command_name" ,
27- " org_id" : org_id, # if available
28- " success" : True
29- })
25+ logger.info(
26+ " Operation completed" ,
27+ extra = {
28+ " operation" : " command_name" ,
29+ " org_id" : org_id, # if available
30+ " success" : True ,
31+ },
32+ )
3033```
3134
3235### 2. Test the Integration Right Now
@@ -38,7 +41,7 @@ codegen config telemetry enable
3841# 2. Run the demo
3942python example_enhanced_agent_command.py
4043
41- # 3. Run any CLI command
44+ # 3. Run any CLI command
4245codegen agents # or any other command
4346
4447# 4. Check status
@@ -48,69 +51,56 @@ codegen config telemetry status
4851## 📝 Copy-Paste Patterns
4952
5053### Pattern 1: Operation Start/End
54+
5155``` python
5256logger = get_logger(__name__ )
5357
5458# At start of function
55- logger.info(" Operation started" , extra = {
56- " operation" : " command.subcommand" ,
57- " user_input" : relevant_input
58- })
59-
60- # At end of function
61- logger.info(" Operation completed" , extra = {
62- " operation" : " command.subcommand" ,
63- " success" : True
64- })
59+ logger.info(" Operation started" , extra = {" operation" : " command.subcommand" , " user_input" : relevant_input})
60+
61+ # At end of function
62+ logger.info(" Operation completed" , extra = {" operation" : " command.subcommand" , " success" : True })
6563```
6664
6765### Pattern 2: Error Handling
66+
6867``` python
6968try :
7069 # your existing code
7170 pass
7271except SomeSpecificError as e:
73- logger.error(" Specific error occurred" , extra = {
74- " operation" : " command.subcommand" ,
75- " error_type" : " specific_error" ,
76- " error_details" : str (e)
77- }, exc_info = True )
72+ logger.error(" Specific error occurred" , extra = {" operation" : " command.subcommand" , " error_type" : " specific_error" , " error_details" : str (e)}, exc_info = True )
7873 # your existing error handling
7974```
8075
8176### Pattern 3: API Calls
77+
8278``` python
8379# Before API call
84- logger.info(" Making API request" , extra = {
85- " operation" : " api.request" ,
86- " endpoint" : " agent/run" ,
87- " org_id" : org_id
88- })
80+ logger.info(" Making API request" , extra = {" operation" : " api.request" , " endpoint" : " agent/run" , " org_id" : org_id})
8981
9082# After successful API call
91- logger.info(" API request successful" , extra = {
92- " operation" : " api.request" ,
93- " endpoint" : " agent/run" ,
94- " response_id" : response.get(" id" ),
95- " status_code" : response.status_code
96- })
83+ logger.info(" API request successful" , extra = {" operation" : " api.request" , " endpoint" : " agent/run" , " response_id" : response.get(" id" ), " status_code" : response.status_code})
9784```
9885
9986## 🎯 What to Log (Priority Order)
10087
10188### 🔥 High Priority (Add These First)
89+
10290- ** Operation start/end** : When commands begin/complete
10391- ** API calls** : Requests to your backend
104- - ** Authentication events** : Login/logout/token issues
92+ - ** Authentication events** : Login/logout/token issues
10593- ** Errors** : Any exception or failure
10694- ** User actions** : Commands run, options selected
10795
10896### ⭐ Medium Priority
97+
10998- ** Performance** : Duration of operations
11099- ** State changes** : Status updates, configuration changes
111100- ** External tools** : Claude CLI detection, git operations
112101
113102### 💡 Low Priority (Nice to Have)
103+
114104- ** Debug info** : Internal state, validation steps
115105- ** User behavior** : Which features are used most
116106
@@ -126,30 +116,30 @@ logger = get_logger(__name__)
126116
127117def create (prompt : str , org_id : int | None = None , ...):
128118 """ Create a new agent run with the given prompt."""
129-
119+
130120 # ADD: Log start
131121 logger.info(" Agent creation started" , extra = {
132122 " operation" : " agent.create" ,
133123 " org_id" : org_id,
134124 " prompt_length" : len (prompt)
135125 })
136-
126+
137127 # Your existing code...
138128 try :
139129 response = requests.post(url, headers = headers, json = payload)
140130 agent_run_data = response.json()
141-
142- # ADD: Log success
131+
132+ # ADD: Log success
143133 logger.info(" Agent created successfully" , extra = {
144134 " operation" : " agent.create" ,
145135 " agent_run_id" : agent_run_data.get(" id" ),
146136 " status" : agent_run_data.get(" status" )
147137 })
148-
138+
149139 except requests.RequestException as e:
150140 # ADD: Log error
151141 logger.error(" Agent creation failed" , extra = {
152- " operation" : " agent.create" ,
142+ " operation" : " agent.create" ,
153143 " error_type" : " api_error" ,
154144 " error" : str (e)
155145 })
@@ -163,52 +153,49 @@ def create(prompt: str, org_id: int | None = None, ...):
163153
164154logger = get_logger(__name__ )
165155
156+
166157def _run_claude_interactive (resolved_org_id : int , no_mcp : bool | None ) -> None :
167158 session_id = generate_session_id()
168-
159+
169160 # ADD: Log session start
170- logger.info(" Claude session started" , extra = {
171- " operation" : " claude.session_start" ,
172- " session_id" : session_id[:8 ], # Short version for privacy
173- " org_id" : resolved_org_id
174- })
175-
161+ logger.info(
162+ " Claude session started" ,
163+ extra = {
164+ " operation" : " claude.session_start" ,
165+ " session_id" : session_id[:8 ], # Short version for privacy
166+ " org_id" : resolved_org_id,
167+ },
168+ )
169+
176170 # Your existing code...
177-
171+
178172 try :
179173 process = subprocess.Popen([claude_path, " --session-id" , session_id])
180174 returncode = process.wait()
181-
175+
182176 # ADD: Log session end
183- logger.info(" Claude session completed" , extra = {
184- " operation" : " claude.session_complete" ,
185- " session_id" : session_id[:8 ],
186- " exit_code" : returncode,
187- " status" : " COMPLETE" if returncode == 0 else " ERROR"
188- })
189-
177+ logger.info(
178+ " Claude session completed" , extra = {" operation" : " claude.session_complete" , " session_id" : session_id[:8 ], " exit_code" : returncode, " status" : " COMPLETE" if returncode == 0 else " ERROR" }
179+ )
180+
190181 except Exception as e:
191182 # ADD: Log session error
192- logger.error(" Claude session failed" , extra = {
193- " operation" : " claude.session_error" ,
194- " session_id" : session_id[:8 ],
195- " error" : str (e)
196- })
183+ logger.error(" Claude session failed" , extra = {" operation" : " claude.session_error" , " session_id" : session_id[:8 ], " error" : str (e)})
197184```
198185
199186## 🧪 Verification
200187
201188After making changes:
202189
2031901 . ** Run the command** : Execute your enhanced CLI command
204- 2 . ** Check telemetry status** : ` codegen config telemetry status `
205- 3 . ** Look for logs in Grafana Cloud** : Search for your operation names
206- 4 . ** Test with telemetry disabled** : ` codegen config telemetry disable ` - should still work normally
191+ 1 . ** Check telemetry status** : ` codegen config telemetry status `
192+ 1 . ** Look for logs in Grafana Cloud** : Search for your operation names
193+ 1 . ** Test with telemetry disabled** : ` codegen config telemetry disable ` - should still work normally
207194
208195## 🚀 Progressive Enhancement
209196
210197** Week 1** : Add basic operation logging to 2-3 commands
211- ** Week 2** : Add error logging to all commands
198+ ** Week 2** : Add error logging to all commands
212199** Week 3** : Add performance metrics and detailed context
213200** Week 4** : Create Grafana dashboards using the collected data
214201
0 commit comments