awsim-cloudwatch-logs 0.5.0

AWS CloudWatch Logs emulator for AWSim
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use awsim_core::{AwsError, RequestContext};
use serde_json::{Value, json};
use tracing::info;

use crate::sqlite_store::LogEventRow;
use crate::state::{LogsState, now_millis};

// ---------------------------------------------------------------------------
// PutLogEvents
// ---------------------------------------------------------------------------

pub fn put_log_events(
    state: &LogsState,
    input: &Value,
    ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let group_name = input["logGroupName"].as_str().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logGroupName is required")
    })?;

    let stream_name = input["logStreamName"].as_str().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logStreamName is required")
    })?;

    let log_events = input["logEvents"].as_array().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logEvents is required")
    })?;

    // AWS documents a per-request cap of 10000 log events and rejects
    // anything larger with InvalidParameterException at the API
    // boundary. Beyond that, batched ingestion silently drops events
    // here which masks the failure clients would see in prod.
    const MAX_EVENTS_PER_REQUEST: usize = 10_000;
    if log_events.len() > MAX_EVENTS_PER_REQUEST {
        return Err(AwsError::bad_request(
            "InvalidParameterException",
            format!(
                "logEvents contains {} entries; the maximum allowed per PutLogEvents request is {MAX_EVENTS_PER_REQUEST}.",
                log_events.len()
            ),
        ));
    }

    let group = state.log_groups.get(group_name).ok_or_else(|| {
        AwsError::not_found(
            "ResourceNotFoundException",
            format!("Log group not found: {group_name}"),
        )
    })?;

    let mut stream = group.streams.get_mut(stream_name).ok_or_else(|| {
        AwsError::not_found(
            "ResourceNotFoundException",
            format!("Log stream not found: {stream_name}"),
        )
    })?;

    // AWS validates `sequenceToken` strictly when the caller supplies
    // one: a mismatch returns InvalidSequenceTokenException carrying
    // the expected token. Modern SDKs (>= 2021) omit the field and the
    // server-side enforcement is skipped — match that behaviour by
    // only validating when the caller passed something non-empty.
    let supplied_token = input["sequenceToken"].as_str().filter(|s| !s.is_empty());
    if let Some(token) = supplied_token {
        let expected = stream
            .upload_sequence_token
            .load(std::sync::atomic::Ordering::SeqCst)
            .to_string();
        if token != expected {
            let mut err = AwsError::bad_request(
                "InvalidSequenceTokenException",
                format!(
                    "The given sequenceToken `{token}` is invalid. The next expected token is `{expected}`."
                ),
            );
            let mut extras = serde_json::Map::new();
            extras.insert("expectedSequenceToken".to_string(), Value::String(expected));
            err.extras = Some(Box::new(extras));
            return Err(err);
        }
    }

    let ingestion_time = now_millis();
    // AWS rejects events whose timestamp falls outside the documented
    // ingestion window: older than 14 days or more than 2 hours in the
    // future. Rejected entries are reported via rejectedLogEventsInfo
    // and are not persisted. Indices here match the original input
    // order so callers can correlate to their request.
    const PAST_WINDOW_MS: u64 = 14 * 24 * 60 * 60 * 1000;
    const FUTURE_WINDOW_MS: u64 = 2 * 60 * 60 * 1000;
    let oldest_allowed = ingestion_time.saturating_sub(PAST_WINDOW_MS);
    let newest_allowed = ingestion_time + FUTURE_WINDOW_MS;

    let mut new_events: Vec<LogEventRow> = Vec::with_capacity(log_events.len());
    let mut min_ts = u64::MAX;
    let mut max_ts = 0u64;
    let mut too_old_end_idx: Option<usize> = None;
    let mut too_new_start_idx: Option<usize> = None;

    for (idx, ev) in log_events.iter().enumerate() {
        let timestamp = ev["timestamp"].as_u64().ok_or_else(|| {
            AwsError::bad_request(
                "InvalidParameterException",
                "each logEvent must have a timestamp",
            )
        })?;
        let message = ev["message"].as_str().ok_or_else(|| {
            AwsError::bad_request(
                "InvalidParameterException",
                "each logEvent must have a message",
            )
        })?;
        if timestamp < oldest_allowed {
            too_old_end_idx = Some(idx);
            continue;
        }
        if timestamp > newest_allowed {
            if too_new_start_idx.is_none() {
                too_new_start_idx = Some(idx);
            }
            continue;
        }
        if timestamp < min_ts {
            min_ts = timestamp;
        }
        if timestamp > max_ts {
            max_ts = timestamp;
        }
        new_events.push(LogEventRow {
            timestamp,
            message: message.to_string(),
            ingestion_time,
        });
    }

    let seq_token = stream.next_sequence_token();

    let sqlite = state.sqlite().ok_or_else(|| {
        AwsError::internal("CloudWatch Logs sqlite store not initialised".to_string())
    })?;
    sqlite.put_events(
        &ctx.account_id,
        &ctx.region,
        group_name,
        stream_name,
        &new_events,
    )?;

    // Enforce retention immediately after writes, so a chatty workload
    // doesn't accumulate events past `retentionInDays` between sweeps.
    if let Some(days) = group.retention_in_days
        && days > 0
    {
        let cutoff = ingestion_time.saturating_sub((days as u64) * 86_400_000);
        let _ = sqlite.trim_older_than(&ctx.account_id, &ctx.region, group_name, cutoff);
    }

    if !new_events.is_empty() {
        if stream.first_event_timestamp.is_none_or(|ex| min_ts < ex) {
            stream.first_event_timestamp = Some(min_ts);
        }
        if stream.last_event_timestamp.is_none_or(|ex| max_ts > ex) {
            stream.last_event_timestamp = Some(max_ts);
        }
        stream.last_ingestion_time = Some(ingestion_time);
    }

    info!(
        log_group = %group_name,
        log_stream = %stream_name,
        count = new_events.len(),
        "Put log events"
    );

    let mut response = json!({ "nextSequenceToken": seq_token.to_string() });
    if too_old_end_idx.is_some() || too_new_start_idx.is_some() {
        let mut info = serde_json::Map::new();
        if let Some(idx) = too_old_end_idx {
            info.insert("tooOldLogEventEndIndex".to_string(), json!(idx));
            info.insert("expiredLogEventEndIndex".to_string(), json!(idx));
        }
        if let Some(idx) = too_new_start_idx {
            info.insert("tooNewLogEventStartIndex".to_string(), json!(idx));
        }
        response["rejectedLogEventsInfo"] = Value::Object(info);
    }
    Ok(response)
}

// ---------------------------------------------------------------------------
// GetLogEvents
// ---------------------------------------------------------------------------

pub fn get_log_events(
    state: &LogsState,
    input: &Value,
    ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let group_name = input["logGroupName"].as_str().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logGroupName is required")
    })?;

    let stream_name = input["logStreamName"].as_str().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logStreamName is required")
    })?;

    let start_time = input["startTime"].as_u64();
    let end_time = input["endTime"].as_u64();
    let start_from_head = input["startFromHead"].as_bool().unwrap_or(false);
    let limit = input["limit"].as_u64().unwrap_or(10000).min(10000) as usize;
    let next_token = input["nextToken"].as_str().unwrap_or("");

    let group = state.log_groups.get(group_name).ok_or_else(|| {
        AwsError::not_found(
            "ResourceNotFoundException",
            format!("Log group not found: {group_name}"),
        )
    })?;

    if !group.streams.contains_key(stream_name) {
        return Err(AwsError::not_found(
            "ResourceNotFoundException",
            format!("Log stream not found: {stream_name}"),
        ));
    }

    let sqlite = state.sqlite().ok_or_else(|| {
        AwsError::internal("CloudWatch Logs sqlite store not initialised".to_string())
    })?;

    let total = sqlite.count_events(
        &ctx.account_id,
        &ctx.region,
        group_name,
        stream_name,
        start_time,
        end_time,
    )?;

    // Token format: "{f|b}/{offset}" — keep the legacy shape so SDK
    // callers don't need to change. Offsets count from head when
    // ascending, from tail otherwise.
    let offset = parse_offset(next_token);

    let (page_offset, ascending) = if start_from_head {
        (offset, true)
    } else {
        // From tail: page_offset advances backward through the data.
        let bound = total.saturating_sub(offset + limit);
        (bound, true)
    };
    let take = if start_from_head {
        limit
    } else {
        // When pulling from the tail, clamp the requested page so we
        // don't slip below offset 0 once we've exhausted the buffer.
        limit.min(total.saturating_sub(offset))
    };

    let rows = sqlite.get_events(
        &ctx.account_id,
        &ctx.region,
        group_name,
        stream_name,
        start_time,
        end_time,
        page_offset,
        take,
        ascending,
    )?;

    let page: Vec<Value> = rows.iter().map(event_to_json).collect();

    let next_forward_offset = if start_from_head {
        offset + page.len()
    } else {
        // Tail pagination: forward = older, so accumulated offset
        // grows by the page size up to total.
        (offset + page.len()).min(total)
    };
    let next_backward_offset = if offset == 0 {
        0
    } else {
        offset.saturating_sub(limit)
    };

    Ok(json!({
        "events": page,
        "nextForwardToken": format!("f/{next_forward_offset}"),
        "nextBackwardToken": format!("b/{next_backward_offset}"),
    }))
}

// ---------------------------------------------------------------------------
// FilterLogEvents
// ---------------------------------------------------------------------------

pub fn filter_log_events(
    state: &LogsState,
    input: &Value,
    ctx: &RequestContext,
) -> Result<Value, AwsError> {
    let group_name = input["logGroupName"].as_str().ok_or_else(|| {
        AwsError::bad_request("InvalidParameterException", "logGroupName is required")
    })?;

    let stream_names: Option<Vec<String>> = input["logStreamNames"].as_array().map(|arr| {
        arr.iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect()
    });

    let filter_pattern = input["filterPattern"].as_str().unwrap_or("");
    let start_time = input["startTime"].as_u64();
    let end_time = input["endTime"].as_u64();
    let limit = input["limit"].as_u64().unwrap_or(10000).min(10000) as usize;

    let group = state.log_groups.get(group_name).ok_or_else(|| {
        AwsError::not_found(
            "ResourceNotFoundException",
            format!("Log group not found: {group_name}"),
        )
    })?;

    let sqlite = state.sqlite().ok_or_else(|| {
        AwsError::internal("CloudWatch Logs sqlite store not initialised".to_string())
    })?;

    let substring = if filter_pattern.is_empty() {
        None
    } else {
        Some(filter_pattern)
    };

    let rows = sqlite.filter_events(
        &ctx.account_id,
        &ctx.region,
        group_name,
        stream_names.as_deref(),
        substring,
        start_time,
        end_time,
        limit,
    )?;

    let matched_events: Vec<Value> = rows
        .into_iter()
        .map(|(stream_name, ev)| {
            let mut obj = event_to_json(&ev);
            obj["logStreamName"] = json!(stream_name);
            obj["eventId"] = json!(format!("{}-{}", stream_name, ev.timestamp));
            obj
        })
        .collect();

    let searched_streams: Vec<Value> = group
        .streams
        .iter()
        .filter_map(|s| {
            let sname = s.key();
            if let Some(ref names) = stream_names
                && !names.iter().any(|n| n == sname)
            {
                return None;
            }
            Some(json!({
                "logStreamName": sname,
                "searchedCompletely": true,
            }))
        })
        .collect();

    Ok(json!({
        "events": matched_events,
        "searchedLogStreams": searched_streams,
    }))
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn event_to_json(e: &LogEventRow) -> Value {
    json!({
        "timestamp": e.timestamp,
        "message": e.message,
        "ingestionTime": e.ingestion_time,
    })
}

/// Pagination tokens have the form `"{f|b}/{offset}"`. Older
/// awsim builds emitted a bare integer; tolerate both.
fn parse_offset(token: &str) -> usize {
    if token.is_empty() {
        return 0;
    }
    let body = token.split_once('/').map(|(_, rest)| rest).unwrap_or(token);
    body.parse::<usize>().unwrap_or(0)
}

#[cfg(test)]
mod sequence_token_tests {
    use super::*;
    use crate::SqliteStore;
    use crate::state::LogGroup;
    use std::sync::Arc;

    fn ctx() -> RequestContext {
        RequestContext::new("logs", "us-east-1")
    }

    fn fresh_state() -> LogsState {
        let dir =
            std::env::temp_dir().join(format!("awsim-logs-seqtoken-{}", uuid::Uuid::new_v4()));
        let path = dir.join("logs.db");
        std::fs::create_dir_all(&dir).unwrap();
        let store = Arc::new(SqliteStore::open(path).unwrap());
        let state = LogsState::default();
        state.set_sqlite(store);
        state
    }

    fn seed_stream(state: &LogsState) {
        let group = LogGroup::new(
            "g".to_string(),
            "arn:aws:logs:us-east-1:000000000000:log-group:g".to_string(),
            std::collections::HashMap::new(),
        );
        state.log_groups.insert("g".to_string(), group);
        let group = state.log_groups.get("g").unwrap();
        group.streams.insert(
            "s".to_string(),
            crate::state::LogStream::new(
                "s".to_string(),
                "arn:aws:logs:us-east-1:000000000000:log-group:g:log-stream:s".to_string(),
            ),
        );
    }

    fn make_input(token: Option<&str>) -> Value {
        let mut input = json!({
            "logGroupName": "g",
            "logStreamName": "s",
            "logEvents": [ { "timestamp": now_millis(), "message": "hello" } ],
        });
        if let Some(t) = token {
            input["sequenceToken"] = json!(t);
        }
        input
    }

    #[test]
    fn omitted_token_passes_through() {
        let state = fresh_state();
        seed_stream(&state);
        put_log_events(&state, &make_input(None), &ctx()).unwrap();
    }

    #[test]
    fn matching_token_succeeds() {
        let state = fresh_state();
        seed_stream(&state);
        // Initial token == "1".
        put_log_events(&state, &make_input(Some("1")), &ctx()).unwrap();
    }

    #[test]
    fn mismatched_token_returns_invalid_sequence_token() {
        let state = fresh_state();
        seed_stream(&state);
        let err = put_log_events(&state, &make_input(Some("999")), &ctx()).unwrap_err();
        assert_eq!(err.code, "InvalidSequenceTokenException");
        let extras = err.extras.unwrap();
        assert_eq!(
            extras.get("expectedSequenceToken").and_then(|v| v.as_str()),
            Some("1")
        );
    }
}