3
3
## ⚡ TL;DR - 3 Step Process
4
4
5
5
1 . ** 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 `
8
8
9
9
** That's it!** Your logs automatically go to Grafana Cloud when telemetry is enabled.
10
10
@@ -22,11 +22,14 @@ from codegen.shared.logging.get_logger import get_logger
22
22
logger = get_logger(__name__ )
23
23
24
24
# 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
+ )
30
33
```
31
34
32
35
### 2. Test the Integration Right Now
@@ -38,7 +41,7 @@ codegen config telemetry enable
38
41
# 2. Run the demo
39
42
python example_enhanced_agent_command.py
40
43
41
- # 3. Run any CLI command
44
+ # 3. Run any CLI command
42
45
codegen agents # or any other command
43
46
44
47
# 4. Check status
@@ -48,69 +51,56 @@ codegen config telemetry status
48
51
## 📝 Copy-Paste Patterns
49
52
50
53
### Pattern 1: Operation Start/End
54
+
51
55
``` python
52
56
logger = get_logger(__name__ )
53
57
54
58
# 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 })
65
63
```
66
64
67
65
### Pattern 2: Error Handling
66
+
68
67
``` python
69
68
try :
70
69
# your existing code
71
70
pass
72
71
except 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 )
78
73
# your existing error handling
79
74
```
80
75
81
76
### Pattern 3: API Calls
77
+
82
78
``` python
83
79
# 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})
89
81
90
82
# 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})
97
84
```
98
85
99
86
## 🎯 What to Log (Priority Order)
100
87
101
88
### 🔥 High Priority (Add These First)
89
+
102
90
- ** Operation start/end** : When commands begin/complete
103
91
- ** API calls** : Requests to your backend
104
- - ** Authentication events** : Login/logout/token issues
92
+ - ** Authentication events** : Login/logout/token issues
105
93
- ** Errors** : Any exception or failure
106
94
- ** User actions** : Commands run, options selected
107
95
108
96
### ⭐ Medium Priority
97
+
109
98
- ** Performance** : Duration of operations
110
99
- ** State changes** : Status updates, configuration changes
111
100
- ** External tools** : Claude CLI detection, git operations
112
101
113
102
### 💡 Low Priority (Nice to Have)
103
+
114
104
- ** Debug info** : Internal state, validation steps
115
105
- ** User behavior** : Which features are used most
116
106
@@ -126,30 +116,30 @@ logger = get_logger(__name__)
126
116
127
117
def create (prompt : str , org_id : int | None = None , ...):
128
118
""" Create a new agent run with the given prompt."""
129
-
119
+
130
120
# ADD: Log start
131
121
logger.info(" Agent creation started" , extra = {
132
122
" operation" : " agent.create" ,
133
123
" org_id" : org_id,
134
124
" prompt_length" : len (prompt)
135
125
})
136
-
126
+
137
127
# Your existing code...
138
128
try :
139
129
response = requests.post(url, headers = headers, json = payload)
140
130
agent_run_data = response.json()
141
-
142
- # ADD: Log success
131
+
132
+ # ADD: Log success
143
133
logger.info(" Agent created successfully" , extra = {
144
134
" operation" : " agent.create" ,
145
135
" agent_run_id" : agent_run_data.get(" id" ),
146
136
" status" : agent_run_data.get(" status" )
147
137
})
148
-
138
+
149
139
except requests.RequestException as e:
150
140
# ADD: Log error
151
141
logger.error(" Agent creation failed" , extra = {
152
- " operation" : " agent.create" ,
142
+ " operation" : " agent.create" ,
153
143
" error_type" : " api_error" ,
154
144
" error" : str (e)
155
145
})
@@ -163,52 +153,49 @@ def create(prompt: str, org_id: int | None = None, ...):
163
153
164
154
logger = get_logger(__name__ )
165
155
156
+
166
157
def _run_claude_interactive (resolved_org_id : int , no_mcp : bool | None ) -> None :
167
158
session_id = generate_session_id()
168
-
159
+
169
160
# 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
+
176
170
# Your existing code...
177
-
171
+
178
172
try :
179
173
process = subprocess.Popen([claude_path, " --session-id" , session_id])
180
174
returncode = process.wait()
181
-
175
+
182
176
# 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
+
190
181
except Exception as e:
191
182
# 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)})
197
184
```
198
185
199
186
## 🧪 Verification
200
187
201
188
After making changes:
202
189
203
190
1 . ** 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
207
194
208
195
## 🚀 Progressive Enhancement
209
196
210
197
** 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
212
199
** Week 3** : Add performance metrics and detailed context
213
200
** Week 4** : Create Grafana dashboards using the collected data
214
201
0 commit comments