ic-canister-serve 0.1.0

A library for serving assets over the http_endpoint of smart contracts running on the Internet Computer.
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use by_address::ByAddress;
use ic_canister_log::{GlobalBuffer, LogBuffer, LogEntry};
use ic_cdk::api::management_canister::http_request::{
    CanisterHttpRequestArgument, HttpHeader, HttpResponse,
};
use ic_metrics_encoder::MetricsEncoder;
use maplit::hashmap;
use priority_queue::PriorityQueue;
use std::{
    cmp::Reverse,
    collections::HashMap,
    fmt,
    fmt::{Debug, Display, Formatter},
    mem::size_of,
    str::FromStr,
};

// 1 Mi. Approximately 10^6, 1 million (slightly more).
const MAX_LOGS_RESPONSE_SIZE: usize = 1 << 20;

/// Transforms an `ic_metrics_encoder::MetricsEncoder` into an HttpResponse that can be
/// served via a Canister's `http_request` query method.
///
/// ```
/// use ic_canister_serve::serve_metrics;
/// use ic_cdk::api::management_canister::http_request::{CanisterHttpRequestArgument, HttpResponse};
/// use ic_metrics_encoder::MetricsEncoder;
///
/// fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
///     w.encode_gauge("example_metric_name", 0 as f64, "Example metric description")?;
///     Ok(())
/// }
///
/// #[ic_cdk::query]
/// fn http_request(request: CanisterHttpRequestArgument) -> HttpResponse {
///     let path = match request.url.find('?') {
///         None => &request.url[..],
///         Some(index) => &request.url[..index],
///     };
///
///     match path {
///         "/metrics" => serve_metrics(encode_metrics),
///         _ => HttpResponse {
///                 status: 404.into(),
///                 body: "not_found".into(),
///                 ..Default::default()
///             }
///     }
/// }
/// ```
pub fn serve_metrics(
    encode_metrics: impl FnOnce(&mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()>,
) -> HttpResponse {
    let mut writer = MetricsEncoder::new(vec![], now() as i64 / 1_000_000);

    match encode_metrics(&mut writer) {
        Ok(()) => {
            let content_body: Vec<u8> = writer.into_inner();
            HttpResponse {
                status: 200.into(),
                headers: vec![
                    HttpHeader {
                        name: "Content-Type".to_string(),
                        value: "text/plain; version=0.0.4".to_string(),
                    },
                    HttpHeader {
                        name: "Content-Length".to_string(),
                        value: content_body.len().to_string(),
                    },
                ],
                body: content_body,
            }
        }
        Err(err) => HttpResponse {
            status: 500.into(),
            headers: vec![],
            body: format!("Failed to encode metrics: {}", err).into(),
        },
    }
}

/// Given an INFO and ERROR `GlobalBuffer`, render the buffers into a json encoded body of an
/// HttpResponse that can be served via a Canister's `http_request` query method. The method's
/// `CanisterHttpRequestArgument` allows selecting the logs based on severity (INFO/ERROR) and
/// timestamp.
///
/// ```
/// use ic_canister_log::{declare_log_buffer, export, log};
/// use ic_canister_serve::serve_logs;
/// use ic_cdk::api::management_canister::http_request::{CanisterHttpRequestArgument, HttpResponse};
///
/// declare_log_buffer!(name = INFO, capacity = 100);
/// declare_log_buffer!(name = ERROR, capacity = 100);
///
/// #[ic_cdk::query]
/// fn http_request(request: CanisterHttpRequestArgument) -> HttpResponse {
///     log!(INFO, "This is an INFO log");
///     log!(ERROR, "This is an ERROR log");
///     
///     let path = match request.url.find('?') {
///         None => &request.url[..],
///         Some(index) => &request.url[..index],
///     };
///
///     match path {
///         "/logs" => serve_logs(request, &INFO, &ERROR),
///         _ => HttpResponse {
///                 status: 404.into(),
///                 body: "not_found".into(),
///                 ..Default::default()
///             }
///     }
/// }
/// ```
pub fn serve_logs(
    request: CanisterHttpRequestArgument,
    info_logs: &'static GlobalBuffer,
    error_logs: &'static GlobalBuffer,
) -> HttpResponse {
    // Convert from generic HTTP request to LogsRequest.
    let request = match LogsRequest::try_from(request) {
        Ok(request) => request,
        Err(message) => {
            let content_body = serde_json::to_string(&hashmap! {"error_description" => message})
                .unwrap_or_default()
                .into_bytes();

            return HttpResponse {
                status: 400.into(),
                headers: vec![
                    HttpHeader {
                        name: "Content-Type".to_string(),
                        value: "application/json".to_string(),
                    },
                    HttpHeader {
                        name: "Content-Length".to_string(),
                        value: content_body.len().to_string(),
                    },
                ],
                body: content_body,
            };
        }
    };

    let body = info_logs.with(|info_logs| {
        let info_logs = info_logs.borrow();
        error_logs.with(|error_logs| {
            let error_logs = error_logs.borrow();

            request.render_json(&info_logs, &error_logs)
        })
    });

    let content_body: Vec<u8> = body.into_bytes();
    HttpResponse {
        status: 200.into(),
        headers: vec![
            HttpHeader {
                name: "Content-Type".to_string(),
                value: "application/json".to_string(),
            },
            HttpHeader {
                name: "Content-Length".to_string(),
                value: content_body.len().to_string(),
            },
        ],
        body: content_body,
    }
}

/// Fields are query parameters. See serve_logs.
///
/// This does two main things:
///
///   1. Tries to convert from a generic CanisterHttpRequestArgument
///     (via impl From<CanisterHttpRequestArgument>).
///
///   2. Renders JSON (via LogsRequest::render_json). Of course, this needs to
///      be fed logs.
struct LogsRequest {
    severity: LogSeverity,
    time: u64,
}

impl LogsRequest {
    /// Returns JSON serialized response body, based on parameters in self.
    ///
    /// This is not entirely straightforward because this needs to do two
    /// things
    ///
    ///   a. Merge INFO and ERROR logs (in the future, adding more severity levels
    ///        is would be pretty straightforward).
    ///
    ///   b. Implement the filtering specified by the query parameters.
    fn render_json(&self, info_logs: &LogBuffer, error_logs: &LogBuffer) -> String {
        let mut info_logs = LogIter::new(LogSeverity::Info, self.skip_old_log_entries(info_logs));
        let mut error_logs =
            LogIter::new(LogSeverity::Error, self.skip_old_log_entries(error_logs));

        // Select sources. They will be merged later.
        // Prioritize them by the timestamp of their first element.
        let mut sources = PriorityQueue::new();
        {
            let info_priority = info_logs.priority();
            let error_priority = error_logs.priority();
            match self.severity {
                LogSeverity::Info => {
                    sources.push(ByAddress(&mut info_logs), info_priority);
                    sources.push(ByAddress(&mut error_logs), error_priority);
                }
                LogSeverity::Error => {
                    sources.push(ByAddress(&mut error_logs), error_priority);
                }
            }
        }

        // Merge sources by timestamp.
        let mut approximate_total_size = 0;
        let mut interleaved_logs = vec![];
        loop {
            // PriorityQueue::pop removes the element with the highest priority.
            // We prioritize by Reverse(first_log_entry.timestamp). See
            // LogIter::priority. Therefore, this should be an Iterator with the
            // earliest first LogEntry.
            let mut log_iter = match sources.pop() {
                None => break, // No more sources.
                Some((log_iter, _priority)) => log_iter,
            };

            let log_entry = match log_iter.next() {
                Some(log_entry) => log_entry,
                None => continue,
            };

            let enhanced_log_entry = EnhancedLogEntry::new(log_iter.severity, log_entry);
            approximate_total_size += enhanced_log_entry.approximate_size();
            if approximate_total_size > MAX_LOGS_RESPONSE_SIZE {
                break;
            }
            interleaved_logs.push(enhanced_log_entry);

            if log_iter.head.is_some() {
                // This guard is a minor optimization, because earlier in this
                // loop continue handles log_iter being empty.
                let priority = log_iter.priority();
                sources.push(log_iter, priority);
            }
        }

        serde_json::json!({
            "entries": interleaved_logs,
        })
        .to_string()
    }

    fn skip_old_log_entries<'a>(
        &self,
        log_buffer: &'a LogBuffer,
    ) -> impl Iterator<Item = &'a LogEntry> {
        let max_skip_timestamp = self.time;
        log_buffer
            .entries_partition_point(move |log_entry| log_entry.timestamp <= max_skip_timestamp)
    }
}

impl TryFrom<CanisterHttpRequestArgument> for LogsRequest {
    type Error = String;

    fn try_from(
        http_request: CanisterHttpRequestArgument,
    ) -> Result<Self, /* description */ String> {
        // Parse query parameters.
        let query = query_parameters_map(&http_request.url);

        let severity = query
            .get("severity")
            .map(|v| v.to_string())
            .unwrap_or_else(|| "Info".to_string());
        let time = query
            .get("time")
            .map(|v| v.to_string())
            .unwrap_or_else(|| "0".to_string());

        let mut defects = vec![];

        let severity = match LogSeverity::from_str(&severity) {
            Ok(severity) => severity,
            Err(err) => {
                defects.push(format!(
                    "Invalid value for query parameter `severity` ({}): {}",
                    severity, err,
                ));
                // Dummy value; won't actually be used, because defects is now nonempty.
                LogSeverity::Info
            }
        };

        let time = match u64::from_str(&time) {
            Ok(time) => time,
            Err(err) => {
                defects.push(format!(
                    "Invalid value for query parameter `time` ({}): {}",
                    time, err,
                ));
                // Dummy value; won't actually be used, because defects is now nonempty.
                0
            }
        };

        if !defects.is_empty() {
            return Err(format!(
                "Invalid request for the following reason(s):\n  -{}",
                defects.join("\n  -"),
            ));
        }

        Ok(Self { severity, time })
    }
}

/// The "right" way to implement this is to use the url crate, but that causes
/// our WASMs to be inordinately larger.
fn query_parameters_map(url: &str) -> HashMap<String, String> {
    const QUERY_SEPARATOR: &str = "?";
    let mut it = url.split(QUERY_SEPARATOR);
    let _skip = it.next();
    let query_string = it.next().unwrap_or_default();

    let mut result = HashMap::new();
    if query_string.is_empty() {
        return result;
    }

    const PARAMETER_SEPARATOR: &str = "&";
    for chunk in query_string.split(PARAMETER_SEPARATOR) {
        const KEY_VALUE_SEPARATOR: &str = "=";
        let mut split = chunk.splitn(2, KEY_VALUE_SEPARATOR);
        let name = split
            .next()
            .expect("Unable to get head of split (this should be impossible).");
        let value = split.next().unwrap_or_default();
        result.insert(name.to_string(), value.to_string());
    }

    result
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, serde::Serialize)]
enum LogSeverity {
    Info,
    Error,
}

impl FromStr for LogSeverity {
    type Err = String;

    fn from_str(name: &str) -> Result<Self, /* description */ String> {
        let severity = match name {
            "Info" => Self::Info,
            "Error" => Self::Error,
            _ => return Err(format!("Unknown log severity name: {}", name)),
        };

        Ok(severity)
    }
}

impl Display for LogSeverity {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        let s = match self {
            Self::Info => "Info",
            Self::Error => "Error",
        };

        write!(formatter, "{}", s)
    }
}

struct LogIter<'a, I>
where
    I: Iterator<Item = &'a LogEntry>,
{
    severity: LogSeverity,
    head: Option<&'a LogEntry>,
    tail: I,
}

impl<'a, I> LogIter<'a, I>
where
    I: Iterator<Item = &'a LogEntry>,
{
    fn new(severity: LogSeverity, mut tail: I) -> Self {
        let head = tail.next();
        Self {
            severity,
            head,
            tail,
        }
    }

    /// Based on the timestamp of the head log entry; earlier entries have
    /// higher priority.
    fn priority(&self) -> impl Ord + Debug {
        Reverse(
            self.head
                .map(|log_entry| log_entry.timestamp)
                .unwrap_or_default(),
        )
    }
}

impl<'a, I> Iterator for LogIter<'a, I>
where
    I: Iterator<Item = &'a LogEntry>,
{
    type Item = &'a LogEntry;

    fn next(&mut self) -> Option<&'a LogEntry> {
        let result = self.head;
        self.head = self.tail.next();
        result
    }
}

impl<'a, I> Debug for LogIter<'a, I>
where
    I: Iterator<Item = &'a LogEntry>,
{
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        formatter
            .debug_struct("LogIter")
            .field("severity", &self.severity)
            .field("head", &self.head)
            .field("tail", &"...")
            .finish()
    }
}

/// Like LogEntry, but with severity.
#[derive(serde::Serialize)]
struct EnhancedLogEntry<'a> {
    severity: LogSeverity,
    timestamp: u64,
    file: &'static str,
    line: u32,
    message: &'a String,
}

impl<'a> EnhancedLogEntry<'a> {
    fn new(severity: LogSeverity, log_entry: &'a LogEntry) -> Self {
        // If the definition of LogEntry is ever changed, this will need to be
        // updated.
        let LogEntry {
            timestamp,
            file,
            line,
            message,
        } = log_entry;

        let timestamp = *timestamp;
        let line = *line;

        Self {
            severity,
            timestamp,
            file,
            line,
            message,
        }
    }

    fn approximate_size(&self) -> usize {
        let min = size_of::<LogSeverity>() // severity
            + size_of::<u64>() // timestamp
            + self.file.len()
            + size_of::<u32>() // line
            + self.message.len();

        // 1.33x factor of safety, because JSON serialization has some overhead
        // (because of quotes, spaces, colons, etc.).
        min * 4 / 3
    }
}

mod private {
    #[cfg(target_arch = "wasm32")]
    pub fn timestamp() -> u64 {
        ic_cdk::api::time()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn timestamp() -> u64 {
        use std::time::SystemTime;

        match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
            Ok(d) => d.as_nanos() as u64,
            Err(_) => panic!("SystemTime before UNIX EPOCH!"),
        }
    }
}

/// Returns the current time as a number of nanoseconds passed since the Unix
/// epoch.
#[doc(hidden)]
pub fn now() -> u64 {
    private::timestamp()
}