Skip to content

Commit f894e9f

Browse files
eli-r-phclaude
andcommitted
fix: redirect origin and logging fixes from review; scope AI routing docs
- Compare redirect origins as requests sends them, so a Unicode host matches its IDNA form. - Log a refused or exhausted capture redirect at debug; failed-batch reporting owns warnings. - The migration guide says the OpenTelemetry integration still exports to /i/v0/ai/otel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5ede78a commit f894e9f

3 files changed

Lines changed: 46 additions & 22 deletions

File tree

‎docs/migration-7.x-to-8.0.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ You need to change code if your app does any of these:
2929
| Method | Endpoint |
3030
| --- | --- |
3131
| `capture`, `set`, `set_once`, `alias`, `group_identify`, `capture_exception` | `/i/v1/analytics/events` |
32-
| `capture_ai`, `capture_ai_immediate`, and every built-in AI integration | `/i/v1/ai/events` |
32+
| `capture_ai`, `capture_ai_immediate`, and the AI wrapper integrations | `/i/v1/ai/events` |
33+
| OpenTelemetry (`posthog.ai.otel`) | `/i/v0/ai/otel`, unchanged |
3334

3435
If you send events to a self-hosted PostHog, check that it serves both endpoints before you upgrade.
3536
An endpoint that is not served drops every event sent to it.
@@ -174,7 +175,8 @@ These changes follow from this order:
174175

175176
## AI capture
176177

177-
- `capture_ai` and every built-in AI integration send to `/i/v1/ai/events`. The AI endpoint accepts events up to 8 MiB.
178+
- `capture_ai` and the AI wrapper integrations send to `/i/v1/ai/events`. The AI endpoint accepts events up to 8 MiB.
179+
- The OpenTelemetry integration (`PostHogSpanProcessor`, `PostHogTraceExporter`) is unchanged: it still exports spans over OTLP to `/i/v0/ai/otel`.
178180
- `enable_full_ai_capture` now controls only content: string truncation and media redaction. It no longer chooses the endpoint. `_use_ai_lane` and `_enable_multimodal_capture` still work as aliases.
179181
- The AI integrations call your client's `capture_ai`, and never `capture`. A custom client object must implement `capture_ai` and accept its `options` keyword argument. If the client has no `capture_ai`, the wrapper, callback handler or tracing processor logs one error when you create it and captures no AI events; provider calls still work. Before, such a client got `capture` calls.
180182
- Tests that pass a `Mock` client to an AI integration must assert on `mock.capture_ai`, not `mock.capture`.

‎posthog/capture_send.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from urllib.parse import urljoin, urlsplit
3333
from uuid import UUID
3434

35+
from requests.exceptions import RequestException
36+
from requests.models import PreparedRequest
37+
3538
from posthog.capture_compression import CaptureCompression, _zstandard
3639
from posthog.capture_event import _build_v1_batch_body, _to_v1_event
3740
from posthog.request import (
@@ -280,10 +283,13 @@ def _compress_v1(
280283

281284

282285
def _origin(url: str) -> Optional[tuple[str, str, int]]:
283-
parsed = urlsplit(url)
286+
# Prepare the URL as requests sends it, so a Unicode host matches its IDNA form.
287+
prepared = PreparedRequest()
284288
try:
289+
prepared.prepare_url(url, None)
290+
parsed = urlsplit(prepared.url or "")
285291
port = parsed.port
286-
except ValueError:
292+
except (RequestException, UnicodeError, ValueError):
287293
return None
288294
if port is None:
289295
port = 443 if parsed.scheme.lower() == "https" else 80
@@ -362,13 +368,13 @@ def _post_v1(
362368
trimmed_host, url, res.headers.get("Location")
363369
)
364370
if target is None:
365-
log.warning(
371+
log.debug(
366372
"capture v1 did not follow a %s redirect to another origin",
367373
res.status_code,
368374
)
369375
return res
370376
url = target
371-
log.warning("capture v1 stopped after %d redirects", _MAX_REDIRECTS)
377+
log.debug("capture v1 stopped after %d redirects", _MAX_REDIRECTS)
372378
return res
373379

374380

‎posthog/test/test_capture_send.py‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ def test_zstd_without_package_raises_actionable_error(self) -> None:
238238
"https://us.i.posthog.com:443/other",
239239
"https://us.i.posthog.com/other",
240240
),
241+
(
242+
"unicode_host_idna_location",
243+
"https://bücher.example",
244+
"https://xn--bcher-kva.example/ingest",
245+
"https://bücher.example/ingest",
246+
),
247+
(
248+
"idna_host_unicode_location",
249+
"https://xn--bcher-kva.example",
250+
"https://bücher.example/ingest",
251+
"https://xn--bcher-kva.example/ingest",
252+
),
241253
]
242254
)
243255
def test_follows_same_origin_redirect_with_same_body(
@@ -261,6 +273,7 @@ def test_follows_same_origin_redirect_with_same_body(
261273
@parameterized.expand(
262274
[
263275
("other_host", 307, "https://attacker.example.com/collect"),
276+
("other_unicode_host", 307, "https://bücher-andere.example/collect"),
264277
("loopback", 308, "http://127.0.0.1:8080/collect"),
265278
("https_to_http", 307, "http://us.i.posthog.com/i/v1/analytics/events"),
266279
("other_port", 308, "https://us.i.posthog.com:8443/i/v1/analytics/events"),
@@ -274,14 +287,16 @@ def test_does_not_follow_other_redirects(self, _name, status, location) -> None:
274287
)
275288
session = _RecordingSession(redirect, _results_response({}))
276289
body = _build_v1_batch_body([_to_v1_event(_msg("u-1"))])
277-
res = _post_v1(
278-
"phc_key",
279-
"https://us.i.posthog.com",
280-
body,
281-
attempt=1,
282-
request_id="r",
283-
session=session,
284-
)
290+
# Failed-batch reporting owns warnings, so the transport logs nothing here.
291+
with self.assertNoLogs("posthog", level="WARNING"):
292+
res = _post_v1(
293+
"phc_key",
294+
"https://us.i.posthog.com",
295+
body,
296+
attempt=1,
297+
request_id="r",
298+
session=session,
299+
)
285300

286301
self.assertIs(res, redirect)
287302
self.assertEqual(len(session.calls), 1)
@@ -290,14 +305,15 @@ def test_stops_after_max_redirects(self) -> None:
290305
loop = _FakeResponse(307, headers={"Location": "/i/v1/analytics/events"})
291306
session = _RecordingSession(loop)
292307
body = _build_v1_batch_body([_to_v1_event(_msg("u-1"))])
293-
res = _post_v1(
294-
"phc_key",
295-
"https://us.i.posthog.com",
296-
body,
297-
attempt=1,
298-
request_id="r",
299-
session=session,
300-
)
308+
with self.assertNoLogs("posthog", level="WARNING"):
309+
res = _post_v1(
310+
"phc_key",
311+
"https://us.i.posthog.com",
312+
body,
313+
attempt=1,
314+
request_id="r",
315+
session=session,
316+
)
301317

302318
self.assertIs(res, loop)
303319
self.assertEqual(len(session.calls), 6)

0 commit comments

Comments
 (0)