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
@@ -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
+ logger.info(" Operation started" , extra = {" operation" : " command.subcommand" , " user_input" : relevant_input})
59
60
60
61
# At end of function
61
- logger.info(" Operation completed" , extra = {
62
- " operation" : " command.subcommand" ,
63
- " success" : True
64
- })
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
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
@@ -163,15 +153,19 @@ 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
- })
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
+ )
175
169
176
170
# Your existing code...
177
171
@@ -180,30 +174,23 @@ def _run_claude_interactive(resolved_org_id: int, no_mcp: bool | None) -> None:
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
- })
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
+ )
189
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
0 commit comments