1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
mod private
{
//! Structured logging support using the `tracing` crate.
//!
//! This module provides instrumentation helpers for tracking
//! API requests, responses, and errors with structured logging.
//!
//! # Design Decisions
//!
//! ## Why Macros with #[ cfg ] Guards?
//!
//! This module uses `macro_rules!` with conditional compilation (`#[ cfg( feature = "structured_logging") ]`)
//! instead of regular functions for several reasons:
//!
//! 1. **Zero Runtime Overhead**: When the `structured_logging` feature is disabled,
//! all logging code is completely eliminated at compile time. No function calls,
//! no string formatting, no performance impact.
//!
//! 2. **Ergonomic API**: Macros allow flexible argument patterns without trait bounds
//! or generic constraints that would complicate the API.
//!
//! 3. **Compile-Time Feature Detection**: The `#[ cfg ]` guards ensure that tracing
//! dependency and code only exist when explicitly enabled via cargo features.
//!
//! ## Alternatives Considered
//!
//! - **Regular functions**: Would require runtime checks or always-compiled code
//! - **Trait-based logging**: Would add complexity and generic constraints
//! - **Direct tracing calls**: Would scatter logging logic across the codebase
//!
//! ## Known Pitfalls
//!
//! ### Macro Invocation Syntax (Fix : issue-doctest-macro-syntax)
//!
//! **Root Cause**: Rust macros require the `!` suffix for invocation. Documentation
//! examples that call macros without `!` will fail during doc tests with error
//! "expected function, found macro".
//!
//! **Pitfall**: When writing documentation examples for macros, it's easy to forget
//! the `!` suffix because the syntax looks like a function call. The compiler
//! helpfully suggests adding `!`, but this only appears during doc test runs.
//!
//! **Prevention**: Always use `cargo test --doc` to validate documentation examples
//! during development. The macro invocation syntax is not optional - without `!`,
//! the code won't compile.
//!
//! Example of correct syntax:
//! ```rust,ignore
//! log_request!( "POST", "/chat", Some("grok-2-1212") ); // Correct
//! log_request( "POST", "/chat", Some("grok-2-1212") ); // Wrong - won't compile
//! ```
/// Logs an API request.
///
/// Records request details including method, path, and model.
///
/// # Arguments
///
/// * `method` - HTTP method (GET, POST, etc.)
/// * `path` - API endpoint path
/// * `model` - Optional model name
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_request;
///
/// log_request!( "POST", "/chat/completions", Some( "grok-2-1212" ) );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_request
{
( $method:expr, $path:expr, $model:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::info!(
method = $method,
path = $path,
model = $model,
"API request"
);
}
};
}
/// Logs an API response.
///
/// Records response details including status code and duration.
///
/// # Arguments
///
/// * `status` - HTTP status code
/// * `duration_ms` - Request duration in milliseconds
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_response;
///
/// log_response!( 200, 145 );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_response
{
( $status:expr, $duration_ms:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::info!(
status = $status,
duration_ms = $duration_ms,
"API response"
);
}
};
}
/// Logs an API error.
///
/// Records error details including error type and message.
///
/// # Arguments
///
/// * `error_type` - Type of error
/// * `message` - Error message
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_error;
///
/// log_error!( "RateLimit", "Rate limit exceeded" );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_error
{
( $error_type:expr, $message:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::error!(
error_type = $error_type,
message = $message,
"API error"
);
}
};
}
/// Logs a retry attempt.
///
/// Records retry details including attempt number and delay.
///
/// # Arguments
///
/// * `attempt` - Current attempt number
/// * `max_attempts` - Maximum number of attempts
/// * `delay_ms` - Delay before retry in milliseconds
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_retry;
///
/// log_retry!( 2, 5, 1000 );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_retry
{
( $attempt:expr, $max_attempts:expr, $delay_ms:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::warn!(
attempt = $attempt,
max_attempts = $max_attempts,
delay_ms = $delay_ms,
"Retrying request"
);
}
};
}
/// Logs a circuit breaker state change.
///
/// Records state transition details.
///
/// # Arguments
///
/// * `from_state` - Previous state
/// * `to_state` - New state
/// * `reason` - Reason for state change
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_circuit_breaker_state;
///
/// log_circuit_breaker_state!( "Closed", "Open", "Failure threshold reached" );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_circuit_breaker_state
{
( $from_state:expr, $to_state:expr, $reason:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::warn!(
from_state = $from_state,
to_state = $to_state,
reason = $reason,
"Circuit breaker state change"
);
}
};
}
/// Logs a failover event.
///
/// Records endpoint rotation details.
///
/// # Arguments
///
/// * `from_endpoint` - Previous endpoint
/// * `to_endpoint` - New endpoint
/// * `reason` - Reason for failover
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_failover;
///
/// log_failover!(
/// "https://api.x.ai/v1/",
/// "https://api-backup.x.ai/v1/",
/// "Primary endpoint unhealthy"
/// );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_failover
{
( $from_endpoint:expr, $to_endpoint:expr, $reason:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::warn!(
from_endpoint = $from_endpoint,
to_endpoint = $to_endpoint,
reason = $reason,
"Failover to backup endpoint"
);
}
};
}
/// Logs rate limiting information.
///
/// Records rate limit details.
///
/// # Arguments
///
/// * `tokens_available` - Tokens available
/// * `tokens_requested` - Tokens requested
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::log_rate_limit;
///
/// log_rate_limit!( 50, 10 );
/// # }
/// ```
#[ macro_export ]
macro_rules! log_rate_limit
{
( $tokens_available:expr, $tokens_requested:expr ) =>
{
#[ cfg( feature = "structured_logging" ) ]
{
tracing::debug!(
tokens_available = $tokens_available,
tokens_requested = $tokens_requested,
"Rate limit check"
);
}
};
}
/// Creates a tracing span for tracking an API operation.
///
/// This helper creates a named span for tracking the lifecycle
/// of an API operation from start to finish.
///
/// # Arguments
///
/// * `name` - Span name (e.g., `chat_completion`)
/// * `model` - Optional model name
///
/// # Returns
///
/// Returns a tracing span that can be entered.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "structured_logging") ]
/// # {
/// use api_xai::create_operation_span;
///
/// let span = create_operation_span( "chat_completion", Some( "grok-2-1212" ) );
/// let _guard = span.enter();
/// // Your operation here
/// # }
/// ```
#[ cfg( feature = "structured_logging" ) ]
pub fn create_operation_span(
name : &str,
model : Option< &str >
) -> tracing::Span
{
tracing::info_span!(
"api_operation",
operation = name,
model = model,
)
}
}
#[ cfg( feature = "structured_logging" ) ]
crate::mod_interface!
{
exposed use
{
create_operation_span,
};
}