hey-sdk 0.31.0

Rust client for the HEY API, generated from a Smithy model of the API
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
//! The `tracing` span every operation and every attempt runs under: its fields, its parent,
//! and what a redacted value looks like there.

#![cfg(feature = "tracing")]

mod support;

use std::collections::BTreeMap;
use std::fmt::Debug;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use serde_json::json;
use tracing::Subscriber;
use tracing::field::{Field, Visit};
use tracing::span::{Attributes, Id, Record};
use tracing_subscriber::layer::{Context, Layer, SubscriberExt};
use tracing_subscriber::registry::LookupSpan;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

use support::client;

#[derive(Debug, Clone, Default)]
struct Span {
    name: String,
    parent: Option<u64>,
    fields: BTreeMap<String, String>,
    closed: bool,
}

/// Every span the SDK opened, with the fields it recorded, in the order they were opened.
#[derive(Clone, Default)]
struct Capture {
    spans: Arc<Mutex<BTreeMap<u64, Span>>>,
    events: Arc<Mutex<Vec<BTreeMap<String, String>>>>,
}

impl Capture {
    fn install(&self) -> tracing::subscriber::DefaultGuard {
        tracing::subscriber::set_default(tracing_subscriber::registry().with(self.clone()))
    }

    fn named(&self, name: &str) -> Vec<Span> {
        self.spans
            .lock()
            .unwrap()
            .values()
            .filter(|span| span.name == name)
            .cloned()
            .collect()
    }

    /// Every value any span or event carried.
    fn values(&self) -> Vec<String> {
        let mut values: Vec<String> = self
            .spans
            .lock()
            .unwrap()
            .values()
            .flat_map(|span| span.fields.values().cloned())
            .collect();
        values.extend(
            self.events
                .lock()
                .unwrap()
                .iter()
                .flat_map(|fields| fields.values().cloned()),
        );
        values
    }

    fn operations(&self) -> Vec<Span> {
        self.named("hey.operation")
    }

    fn attempts(&self) -> Vec<Span> {
        self.named("hey.attempt")
    }

    fn operations_with_ids(&self) -> Vec<(u64, Span)> {
        self.spans
            .lock()
            .unwrap()
            .iter()
            .filter(|(_, span)| span.name == "hey.operation")
            .map(|(id, span)| (*id, span.clone()))
            .collect()
    }

    fn id_of_named(&self, name: &str) -> u64 {
        *self
            .spans
            .lock()
            .unwrap()
            .iter()
            .find(|(_, span)| span.name == name)
            .map(|(id, _)| id)
            .unwrap()
    }

    fn id_of(&self, name: &str, operation: &str) -> u64 {
        *self
            .spans
            .lock()
            .unwrap()
            .iter()
            .find(|(_, span)| {
                span.name == name
                    && span
                        .fields
                        .get("operation")
                        .is_some_and(|op| op == operation)
            })
            .map(|(id, _)| id)
            .unwrap()
    }
}

struct Fields<'a>(&'a mut BTreeMap<String, String>);

impl Visit for Fields<'_> {
    fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
        self.0
            .insert(field.name().to_string(), format!("{value:?}"));
    }

    fn record_str(&mut self, field: &Field, value: &str) {
        self.0.insert(field.name().to_string(), value.to_string());
    }

    fn record_u64(&mut self, field: &Field, value: u64) {
        self.0.insert(field.name().to_string(), value.to_string());
    }

    fn record_i64(&mut self, field: &Field, value: i64) {
        self.0.insert(field.name().to_string(), value.to_string());
    }
}

impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for Capture {
    fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
        let mut span = Span {
            name: attrs.metadata().name().to_string(),
            parent: ctx
                .span(id)
                .and_then(|span| span.parent().map(|parent| parent.id().into_u64())),
            ..Span::default()
        };
        attrs.record(&mut Fields(&mut span.fields));
        self.spans.lock().unwrap().insert(id.into_u64(), span);
    }

    fn on_record(&self, id: &Id, values: &Record<'_>, _ctx: Context<'_, S>) {
        if let Some(span) = self.spans.lock().unwrap().get_mut(&id.into_u64()) {
            values.record(&mut Fields(&mut span.fields));
        }
    }

    fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
        let mut fields = BTreeMap::new();
        event.record(&mut Fields(&mut fields));
        self.events.lock().unwrap().push(fields);
    }

    fn on_close(&self, id: Id, _ctx: Context<'_, S>) {
        if let Some(span) = self.spans.lock().unwrap().get_mut(&id.into_u64()) {
            span.closed = true;
        }
    }
}

/// An attempt span as a test reads it: its parent and its fields.
type Attempt<'a> = (Option<u64>, Vec<(&'a str, &'a str)>);

fn fields(span: &Span) -> Vec<(&str, &str)> {
    span.fields
        .iter()
        .map(|(name, value)| (name.as_str(), value.as_str()))
        .collect()
}

#[tokio::test]
async fn one_span_per_operation_names_it_and_records_what_hey_answered() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/boxes/123.json"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("x-request-id", "req-4242")
                .set_body_json(json!({ "id": 123, "kind": "imbox", "name": "Imbox" })),
        )
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();

    client(&server)
        .boxes()
        .get(123, &hey_sdk::services::boxes::GetBoxParams::default())
        .await
        .unwrap();

    let operations = capture.operations();
    assert_eq!(operations.len(), 1);
    assert_eq!(
        fields(&operations[0]),
        [
            ("http.status", "200"),
            ("operation", "GetBox"),
            ("request_id", "req-4242"),
            ("service", "Boxes"),
        ]
    );
    assert_eq!(operations[0].parent, None);
    assert!(operations[0].closed);
    let attempts = capture.attempts();
    assert_eq!(attempts.len(), 1);
    assert_eq!(
        fields(&attempts[0]),
        [("attempt", "1"), ("http.status", "200")]
    );
    assert_eq!(
        attempts[0].parent,
        Some(capture.id_of("hey.operation", "GetBox"))
    );
}

#[tokio::test]
async fn every_attempt_is_a_child_span_numbered_in_order() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/boxes.json"))
        .respond_with(ResponseTemplate::new(503))
        .up_to_n_times(2)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/boxes.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();

    client(&server).boxes().list().await.unwrap();

    let operation = capture.id_of("hey.operation", "ListBoxes");
    let attempts = capture.attempts();
    let numbered: Vec<Attempt<'_>> = attempts
        .iter()
        .map(|span| (span.parent, fields(span)))
        .collect();
    assert_eq!(
        numbered,
        [
            (
                Some(operation),
                vec![("attempt", "1"), ("http.status", "503")]
            ),
            (
                Some(operation),
                vec![("attempt", "2"), ("http.status", "503")]
            ),
            (
                Some(operation),
                vec![("attempt", "3"), ("http.status", "200")]
            ),
        ]
    );
    assert!(attempts.iter().all(|span| span.closed));
    assert_eq!(capture.operations()[0].fields["http.status"], "200");
}

#[tokio::test]
async fn a_failure_records_the_status_it_failed_with() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/boxes/9.json"))
        .respond_with(ResponseTemplate::new(404).insert_header("x-request-id", "req-404"))
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();

    client(&server)
        .boxes()
        .get(9, &hey_sdk::services::boxes::GetBoxParams::default())
        .await
        .unwrap_err();

    let operation = &capture.operations()[0];
    assert_eq!(operation.fields["http.status"], "404");
    assert_eq!(operation.fields["request_id"], "req-404");
    assert!(operation.closed);
}

/// Two operations in flight at once each keep their own span: each hangs off the span its
/// caller was in, carries the status its own call got, and parents only its own attempts.
#[tokio::test]
async fn concurrent_operations_keep_their_own_spans() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/boxes/1.json"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_delay(Duration::from_millis(50))
                .set_body_json(json!({ "id": 1, "kind": "imbox", "name": "Imbox" })),
        )
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/boxes/2.json"))
        .respond_with(ResponseTemplate::new(404))
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();
    let client = client(&server);

    let params = hey_sdk::services::boxes::GetBoxParams::default();
    let boxes = client.boxes();
    let (first, second) = tokio::join!(
        tracing::Instrument::instrument(boxes.get(1, &params), tracing::info_span!("first")),
        tracing::Instrument::instrument(boxes.get(2, &params), tracing::info_span!("second"))
    );
    first.unwrap();
    second.unwrap_err();

    let first = capture.id_of_named("first");
    let second = capture.id_of_named("second");
    let operations = capture.operations_with_ids();
    assert_eq!(operations.len(), 2);
    let under = |caller: u64| -> (u64, &Span) {
        operations
            .iter()
            .find(|(_, span)| span.parent == Some(caller))
            .map(|(id, span)| (*id, span))
            .unwrap()
    };
    let (first_op, first_span) = under(first);
    let (second_op, second_span) = under(second);
    assert_eq!(first_span.fields["http.status"], "200");
    assert_eq!(second_span.fields["http.status"], "404");
    let mut parents: Vec<u64> = capture
        .attempts()
        .iter()
        .map(|span| span.parent.unwrap())
        .collect();
    parents.sort_unstable();
    let mut expected = vec![first_op, second_op];
    expected.sort_unstable();
    assert_eq!(parents, expected);
}

/// A caller that drops the call mid-flight drops the span with it: it closes, with no
/// status, rather than staying open for the life of the subscriber.
#[tokio::test]
async fn a_cancelled_operation_closes_its_span_without_an_answer() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/boxes.json"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_delay(Duration::from_secs(5))
                .set_body_json(json!([])),
        )
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();
    let client = client(&server);

    let abandoned = tokio::time::timeout(Duration::from_millis(50), client.boxes().list()).await;
    assert!(abandoned.is_err());

    let operation = &capture.operations()[0];
    assert!(operation.closed);
    assert!(!operation.fields.contains_key("http.status"));
    let attempt = &capture.attempts()[0];
    assert!(attempt.closed);
    assert!(!attempt.fields.contains_key("http.status"));
}

/// A quiet send is one request inside another operation and opens no operation span of
/// its own: its attempt hangs off whatever span its caller is in, the way the hooks hear of
/// one operation rather than two.
#[tokio::test]
async fn a_quiet_send_runs_in_the_span_its_caller_is_in() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/topics/5/publication"))
        .respond_with(ResponseTemplate::new(302).insert_header("Location", "/topics/5"))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/topics/5/publication.json"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({ "published": true, "url": "https://public.hey.com/p/abc" })),
        )
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();
    let client = client(&server);

    let caller = tracing::info_span!("caller");
    tracing::Instrument::instrument(client.publications().publish(5), caller)
        .await
        .unwrap();

    let caller = capture.id_of_named("caller");
    let operations = capture.operations();
    assert_eq!(operations.len(), 1);
    assert_eq!(operations[0].fields["operation"], "CreateTopicPublication");
    assert_eq!(operations[0].parent, Some(caller));
    let operation = capture.id_of("hey.operation", "CreateTopicPublication");
    let parents: Vec<Option<u64>> = capture.attempts().iter().map(|span| span.parent).collect();
    assert_eq!(parents, [Some(operation), Some(caller)]);
}

/// Nothing the caller passed in reaches a span or an event: a request for a path the
/// caller wrote is named by its method alone, and a resend is logged by the operation's
/// name and the failure's code, not by anything that could carry the URL.
#[tokio::test]
async fn nothing_the_caller_passed_reaches_a_span_or_an_event() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/topics/search.json"))
        .respond_with(ResponseTemplate::new(503))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/topics/search.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
        .mount(&server)
        .await;
    let capture = Capture::default();
    let _guard = capture.install();
    let client = client(&server);

    let mut operation = client.request(hey_sdk::http::Method::GET, "/topics/search");
    operation.query("q", "secret plans");
    client.send_unit(operation).await.unwrap();

    let operations = capture.operations();
    assert_eq!(operations.len(), 1);
    assert_eq!(
        fields(&operations[0]),
        [
            ("http.status", "200"),
            ("operation", "GET"),
            ("service", "Raw")
        ]
    );
    assert_eq!(capture.attempts().len(), 2);
    let leaked: Vec<String> = capture
        .values()
        .into_iter()
        .filter(|value| value.contains("secret") || value.contains("topics"))
        .collect();
    assert!(leaked.is_empty(), "leaked {leaked:?}");
    assert!(
        capture
            .events
            .lock()
            .unwrap()
            .iter()
            .any(|fields| fields.get("operation").is_some_and(|op| op == "GET")),
        "the resend was logged"
    );
}