posthog-rs 0.23.2

The official Rust client for Posthog (https://posthog.com/).
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
//! End-to-end coverage for the `on_error` hook on the feature-flags and
//! local-evaluation poller surfaces (the capture surface is covered by the
//! transport unit tests). Each test drives the public client against an
//! httpmock server and asserts the hook observes the failure.

use httpmock::prelude::*;
use posthog_rs::PostHogError;
use serde_json::json;
use std::sync::{Arc, Mutex};

/// A single local-eval flag that resolves without extra properties, so a
/// degraded `evaluate_flags` still produces a non-empty snapshot.
fn definitions_body() -> serde_json::Value {
    json!({
        "flags": [
            {
                "key": "feature-a",
                "active": true,
                "filters": {
                    "groups": [
                        { "properties": [], "rollout_percentage": 100.0, "variant": null }
                    ],
                    "multivariate": null,
                    "payloads": {}
                }
            }
        ],
        "group_type_mapping": {},
        "cohorts": {}
    })
}

type FlagsRecord = (Option<u16>, Option<String>, bool, Option<String>, bool);
type FlagsSink = Arc<Mutex<Vec<FlagsRecord>>>;
type LocalEvalSink = Arc<Mutex<Vec<Option<u16>>>>;

/// Records every `FeatureFlags` failure the hook sees.
fn flags_sink() -> (FlagsSink, impl Fn(&PostHogError<'_>) + Send + 'static) {
    let recorded: FlagsSink = Arc::new(Mutex::new(Vec::new()));
    let sink = recorded.clone();
    let hook = move |failure: &PostHogError<'_>| {
        if let PostHogError::FeatureFlags(f) = failure {
            sink.lock().unwrap_or_else(|p| p.into_inner()).push((
                f.status(),
                f.distinct_id().map(str::to_string),
                f.endpoint().contains("/flags/"),
                f.body().map(str::to_string),
                f.error().to_string().contains("500"),
            ));
        }
    };
    (recorded, hook)
}

/// Records the HTTP status of every `LocalEvaluation` failure the hook sees.
fn local_eval_sink() -> (LocalEvalSink, impl Fn(&PostHogError<'_>) + Send + 'static) {
    let recorded: LocalEvalSink = Arc::new(Mutex::new(Vec::new()));
    let sink = recorded.clone();
    let hook = move |failure: &PostHogError<'_>| {
        if let PostHogError::LocalEvaluation(e) = failure {
            sink.lock()
                .unwrap_or_else(|p| p.into_inner())
                .push(e.status());
        }
    };
    (recorded, hook)
}

#[cfg(not(feature = "async-client"))]
mod blocking {
    use super::*;
    use posthog_rs::EvaluateFlagsOptions;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn flags_failure_reports_status_endpoint_and_body() {
        let server = MockServer::start();
        let flags = server.mock(|when, then| {
            when.method(POST).path("/flags/");
            then.status(500).body("boom");
        });
        let (recorded, hook) = flags_sink();
        let client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .on_error(hook)
                .build()
                .unwrap(),
        );

        let err = client
            .evaluate_flags("user-1", EvaluateFlagsOptions::default())
            .expect_err("a 500 with no local results propagates");
        assert!(err.to_string().contains("500"));
        flags.assert_hits(1);

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "exactly one flags failure expected");
        let (status, distinct_id, endpoint_ok, body, error_has_500) = &recorded[0];
        assert_eq!(*status, Some(500));
        assert_eq!(distinct_id.as_deref(), Some("user-1"));
        assert!(*endpoint_ok);
        assert_eq!(body.as_deref(), Some("boom"));
        assert!(error_has_500);
    }

    #[test]
    fn flags_failure_fires_even_when_degrading_to_local_results() {
        // Local evaluation covers the flag, so a failed remote `/flags` degrades
        // to a local-only snapshot (Ok) rather than erroring — the hook must
        // still observe the remote failure.
        let server = MockServer::start();
        let _defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(200).json_body(definitions_body());
        });
        let flags = server.mock(|when, then| {
            when.method(POST).path("/flags/");
            then.status(500).body("boom");
        });
        let (recorded, hook) = flags_sink();
        let client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(3600)
                .on_error(hook)
                .build()
                .unwrap(),
        );

        let snapshot = client
            .evaluate_flags("user-1", EvaluateFlagsOptions::default())
            .expect("degrades to a local-only snapshot");
        assert!(snapshot.keys().contains(&"feature-a".to_string()));
        flags.assert_hits(1);

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "degrade path still reports once");
        assert_eq!(recorded[0].0, Some(500));
    }

    #[test]
    fn local_eval_poller_reports_initial_load_failure() {
        let server = MockServer::start();
        let defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(401).body("unauthorized");
        });
        let (recorded, hook) = local_eval_sink();
        let _client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(3600)
                .on_error(hook)
                .build()
                .unwrap(),
        );

        defs.assert_hits(1);
        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "initial poll failure reported once");
        assert_eq!(recorded[0], Some(401));
    }

    #[test]
    fn on_error_hook_reentering_flags_path_does_not_deadlock() {
        // Regression guard for the hook's concurrency design. `on_error` is an
        // `Arc<dyn Fn + Send + Sync>` invoked without holding any SDK lock; an
        // earlier `Arc<Mutex<Box<FnMut>>>` design would re-lock the hook's own
        // mutex on the same thread if the hook re-entered a client method that
        // fires `on_error` again, deadlocking the caller. Re-entering the SDK
        // from a hook is unsupported (see the docs) — this only proves the SDK
        // itself can't self-deadlock, and locks in the `Fn`-not-`Mutex` design.
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::mpsc;
        use std::sync::{OnceLock, Weak};

        let server = MockServer::start();
        let _flags = server.mock(|when, then| {
            when.method(POST).path("/flags/");
            then.status(500).body("boom");
        });

        // The hook needs a handle to the client that owns it; a `Weak` cell
        // breaks the ownership cycle (Client -> hook -> cell -> Client) so the
        // client still drops normally at end of test.
        let cell: Arc<OnceLock<Weak<posthog_rs::Client>>> = Arc::new(OnceLock::new());
        let depth = Arc::new(AtomicUsize::new(0));
        let cell_for_hook = cell.clone();
        let depth_for_hook = depth.clone();
        let hook = move |failure: &PostHogError<'_>| {
            if let PostHogError::FeatureFlags(_) = failure {
                // Re-enter exactly once to bound the recursion.
                if depth_for_hook.fetch_add(1, Ordering::SeqCst) == 0 {
                    if let Some(client) = cell_for_hook.get().and_then(Weak::upgrade) {
                        let _ = client.evaluate_flags("user-2", EvaluateFlagsOptions::default());
                    }
                }
            }
        };

        let client = Arc::new(posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .on_error(hook)
                .build()
                .unwrap(),
        ));
        cell.set(Arc::downgrade(&client)).ok();

        // Drive the re-entrant sequence on a worker thread so a regression fails
        // fast (watchdog) instead of hanging the whole test binary.
        let (tx, rx) = mpsc::channel();
        let worker = thread::spawn(move || {
            let err = client
                .evaluate_flags("user-1", EvaluateFlagsOptions::default())
                .expect_err("a 500 with no local results propagates");
            tx.send(err.to_string()).ok();
        });

        let msg = rx
            .recv_timeout(Duration::from_secs(10))
            .expect("evaluate_flags re-entry deadlocked (on_error hook self-deadlock regression)");
        assert!(msg.contains("500"));
        worker.join().unwrap();

        assert_eq!(
            depth.load(Ordering::SeqCst),
            2,
            "hook fired for the outer failure and the one re-entered failure"
        );
    }

    #[test]
    fn local_eval_poller_reports_recurring_loop_failures() {
        // The background poll loop (not just the synchronous initial load) must
        // fire the hook on each failed poll. With a 1s interval, a second poll
        // fails after the initial load — assert the hook sees >= 2 failures.
        let server = MockServer::start();
        let _defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(401).body("unauthorized");
        });
        let (recorded, hook) = local_eval_sink();
        let _client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(1)
                .on_error(hook)
                .build()
                .unwrap(),
        );

        // Initial load fails synchronously during construction (1 hit). Wait
        // for the background loop to fire at least one more poll — the hook
        // firing is proof the poll ran.
        thread::sleep(Duration::from_millis(1500));

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert!(
            recorded.len() >= 2,
            "recurring poll failures reported, got {}",
            recorded.len()
        );
        assert!(recorded.iter().all(|s| *s == Some(401)));
    }
}

#[cfg(feature = "async-client")]
mod async_tests {
    use super::*;
    use posthog_rs::EvaluateFlagsOptions;

    #[tokio::test]
    async fn flags_failure_reports_status_endpoint_and_body() {
        let server = MockServer::start();
        let flags = server.mock(|when, then| {
            when.method(POST).path("/flags/");
            then.status(500).body("boom");
        });
        let (recorded, hook) = flags_sink();
        let client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .on_error(hook)
                .build()
                .unwrap(),
        )
        .await;

        let err = client
            .evaluate_flags("user-1", EvaluateFlagsOptions::default())
            .await
            .expect_err("a 500 with no local results propagates");
        assert!(err.to_string().contains("500"));
        flags.assert_hits(1);

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "exactly one flags failure expected");
        let (status, distinct_id, endpoint_ok, body, error_has_500) = &recorded[0];
        assert_eq!(*status, Some(500));
        assert_eq!(distinct_id.as_deref(), Some("user-1"));
        assert!(*endpoint_ok);
        assert_eq!(body.as_deref(), Some("boom"));
        assert!(error_has_500);
    }

    #[tokio::test]
    async fn flags_failure_fires_even_when_degrading_to_local_results() {
        let server = MockServer::start();
        let _defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(200).json_body(definitions_body());
        });
        let flags = server.mock(|when, then| {
            when.method(POST).path("/flags/");
            then.status(500).body("boom");
        });
        let (recorded, hook) = flags_sink();
        let client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(3600)
                .on_error(hook)
                .build()
                .unwrap(),
        )
        .await;
        // AsyncFlagPoller::start() awaits the initial definitions load before
        // returning, so the cache is populated by the time client().await
        // resolves — no sleep needed here.

        let snapshot = client
            .evaluate_flags("user-1", EvaluateFlagsOptions::default())
            .await
            .expect("degrades to a local-only snapshot");
        assert!(snapshot.keys().contains(&"feature-a".to_string()));
        flags.assert_hits(1);

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "degrade path still reports once");
        assert_eq!(recorded[0].0, Some(500));
    }

    #[tokio::test]
    async fn local_eval_poller_reports_initial_load_failure() {
        let server = MockServer::start();
        let defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(401).body("unauthorized");
        });
        let (recorded, hook) = local_eval_sink();
        let _client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(3600)
                .on_error(hook)
                .build()
                .unwrap(),
        )
        .await;

        defs.assert_hits(1);
        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert_eq!(recorded.len(), 1, "initial poll failure reported once");
        assert_eq!(recorded[0], Some(401));
    }

    #[tokio::test]
    async fn local_eval_poller_reports_recurring_loop_failures() {
        // The async background poll task (not just the initial load awaited by
        // start()) must fire the hook on each failed poll. With a 1s interval,
        // a second poll fails after start() returns — assert >= 2 failures.
        let server = MockServer::start();
        let _defs = server.mock(|when, then| {
            when.method(GET).path("/flags/definitions/");
            then.status(401).body("unauthorized");
        });
        let (recorded, hook) = local_eval_sink();
        let _client = posthog_rs::client(
            posthog_rs::ClientOptionsBuilder::default()
                .api_key("phc_test".to_string())
                .host(server.base_url())
                .personal_api_key("phx_test".to_string())
                .enable_local_evaluation(true)
                .poll_interval_seconds(1)
                .on_error(hook)
                .build()
                .unwrap(),
        )
        .await;

        // start() awaits the initial load (1 hit). Wait for the background
        // task's interval to fire at least one more poll — the hook firing is
        // proof the poll ran.
        tokio::time::sleep(std::time::Duration::from_millis(1500)).await;

        let recorded = recorded.lock().unwrap_or_else(|p| p.into_inner());
        assert!(
            recorded.len() >= 2,
            "recurring poll failures reported, got {}",
            recorded.len()
        );
        assert!(recorded.iter().all(|s| *s == Some(401)));
    }
}