-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathclient.py
More file actions
4432 lines (3941 loc) · 176 KB
/
Copy pathclient.py
File metadata and controls
4432 lines (3941 loc) · 176 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import atexit
import inspect
import json
import logging
import os
import sys
import threading
import time
import warnings
import weakref
from contextvars import ContextVar
from datetime import datetime, timedelta, timezone
from typing import Any, Callable, Dict, List, Mapping, Optional, Union, cast
from uuid import UUID, uuid4
from typing_extensions import Unpack
from posthog._async_utils import _BackgroundEventLoopRunner
from posthog._disabled_lane_queue import _DisabledLaneQueue
from posthog.args import ID_TYPES, ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from posthog.metrics_capture import PostHogMetrics
from posthog.capture_compression import (
CaptureCompression,
_resolve_capture_compression,
)
from posthog.capture_mode import CaptureMode, _resolve_capture_mode
from posthog.capture_v1 import _send_v1_batch
from posthog.consumer import AI_MAX_MSG_SIZE, MAX_MSG_SIZE, Consumer, _DrainSignal
from posthog.contexts import (
_get_current_context,
get_capture_exception_code_variables_context,
get_code_variables_detect_secrets_context,
get_code_variables_ignore_patterns_context,
get_code_variables_mask_patterns_context,
get_code_variables_mask_url_credentials_context,
get_context_device_id,
get_context_distinct_id,
get_context_session_id,
get_tags as _context_get_tags,
identify_context as _context_identify_context,
_scoped as _context_scoped,
new_context,
set_context_device_id as _context_set_context_device_id,
set_context_session as _context_set_context_session,
tag as _context_tag,
)
from posthog.exception_capture import ExceptionCapture
from posthog._logging import _configure_posthog_logging
from posthog.exception_utils import (
DEFAULT_CODE_VARIABLES_DETECT_SECRETS,
DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS,
DEFAULT_CODE_VARIABLES_MASK_PATTERNS,
DEFAULT_CODE_VARIABLES_MASK_URL_CREDENTIALS,
exc_info_from_error,
exception_is_already_captured,
exceptions_from_error_tuple,
_get_current_otel_span_properties,
handle_in_app,
mark_exception_as_captured,
try_attach_code_variables_to_frames,
)
from posthog.feature_flag_evaluations import (
FeatureFlagEvaluations,
_EvaluatedFlagRecord,
_FeatureFlagEvaluationsHost,
)
from posthog.feature_flags import (
InconclusiveMatchError,
RequiresServerEvaluation,
match_feature_flag_properties,
resolve_bucketing_value,
)
from posthog.flag_definition_cache import (
FlagDefinitionCacheData,
FlagDefinitionCacheProvider,
)
from posthog.poller import Poller
from posthog.request import (
AI_EVENTS_ENDPOINT,
EVENTS_ENDPOINT,
APIError,
QuotaLimitError,
RequestsConnectionError,
RequestsTimeout,
batch_post,
determine_server_host,
flags,
get,
normalize_host,
remote_config,
reset_sessions,
)
from posthog.types import (
FeatureFlag,
FeatureFlagError,
FeatureFlagResult,
FlagMetadata,
FlagsAndPayloads,
FlagsResponse,
FlagValue,
SendFeatureFlagsOptions,
normalize_flags_response,
to_flags_and_payloads,
to_payloads,
to_values,
)
from posthog.utils import (
FlagCache,
RedisFlagCache,
SizeLimitedDict,
clean,
_normalize_timestamp,
guess_timezone as guess_timezone,
system_context,
)
from posthog.version import VERSION
from queue import Empty, Full, Queue
_configure_posthog_logging()
MAX_DICT_SIZE = 50_000
_ATEXIT_FLUSH_TIMEOUT_SECONDS = 1.0
_atexit_deadline: Optional[float] = None
_atexit_deadline_lock = threading.Lock()
def _supports_lane_synchronization(queue) -> bool:
return all(
hasattr(queue, attribute)
for attribute in (
"mutex",
"not_empty",
"not_full",
"all_tasks_done",
"unfinished_tasks",
"_qsize",
"_get",
)
)
def _new_lane_queue(maxsize: int) -> Queue:
"""Return a safe queue, disabling the lane instead of raising on failure."""
log = logging.getLogger("posthog")
try:
queue: Queue = Queue(maxsize)
except Exception:
log.exception(
"Failed to initialize queue.Queue; disabling asynchronous capture for the lane"
)
return cast(Queue, _DisabledLaneQueue(maxsize))
if _supports_lane_synchronization(queue):
return queue
monkey = sys.modules.get("gevent.monkey")
if monkey is None:
log.error(
"queue.Queue lacks the synchronization interface required by PostHog "
"and gevent.monkey is not loaded; disabling asynchronous capture for the lane"
)
return cast(Queue, _DisabledLaneQueue(maxsize))
try:
if not monkey.is_object_patched("queue", "Queue"):
log.error(
"queue.Queue lacks the synchronization interface required by PostHog "
"but gevent does not report it as patched; disabling asynchronous "
"capture for the lane"
)
return cast(Queue, _DisabledLaneQueue(maxsize))
original_queue = monkey.get_original("queue", "Queue")
queue = cast(Queue, original_queue(maxsize))
except Exception:
log.exception(
"Failed to restore the original queue.Queue after gevent monkey-patching; "
"disabling asynchronous capture for the lane"
)
return cast(Queue, _DisabledLaneQueue(maxsize))
if _supports_lane_synchronization(queue):
return queue
log.error(
"The queue.Queue restored after gevent monkey-patching lacks the synchronization "
"interface required by PostHog; disabling asynchronous capture for the lane"
)
return cast(Queue, _DisabledLaneQueue(maxsize))
def _get_atexit_deadline() -> float:
global _atexit_deadline
with _atexit_deadline_lock:
if _atexit_deadline is None:
_atexit_deadline = time.monotonic() + _ATEXIT_FLUSH_TIMEOUT_SECONDS
return _atexit_deadline
def get_identity_state(passed) -> tuple[str, bool]:
"""Returns the distinct id to use, and whether this is a personless event or not"""
stringified = stringify_id(passed)
if stringified and len(stringified):
return (stringified, False)
context_id = get_context_distinct_id()
if context_id:
return (context_id, False)
return (str(uuid4()), True)
def _stringify_event_uuid(value) -> str:
if isinstance(value, UUID):
return str(value)
stringified = stringify_id(value)
if not stringified:
raise ValueError(
f"Invalid event uuid {value!r}. Expected a valid UUID string or uuid.UUID instance."
)
try:
UUID(stringified)
except ValueError:
raise ValueError(
f"Invalid event uuid {value!r}. Expected a valid UUID string or uuid.UUID instance."
) from None
return stringified
def add_context_tags(properties):
properties = properties or {}
current_context = _get_current_context()
if current_context:
context_tags = current_context.collect_tags()
properties["$context_tags"] = set(context_tags.keys())
# We want explicitly passed properties to override context tags
context_tags.update(properties)
properties = context_tags
if "$session_id" not in properties and get_context_session_id():
properties["$session_id"] = get_context_session_id()
return properties
def no_throw(default_return=None):
"""
Decorator to prevent raising exceptions from public API methods.
Note that this doesn't prevent errors from propagating via `on_error`.
Exceptions will still be raised if the debug flag is enabled.
Args:
default_return: Value to return on exception (default: None)
"""
def decorator(func):
from functools import wraps
@wraps(func)
def wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except Exception as e:
if self.debug:
raise e
self.log.exception(f"Error in {func.__name__}: {e}")
return default_return
return wrapper
return decorator
# Strict allowlist for minimal ``$feature_flag_called`` events, per the cross-SDK
# contract: everything else — customer-passed properties, super properties, context
# tags, and the richer parts of system context — is stripped from the
# fully-enriched properties dict. The static platform/runtime identity keys below
# are the exception: they're cheap and useful for debugging flag behavior by
# platform, so they survive minimization.
_MINIMAL_FLAG_CALLED_EVENT_PROPERTIES: frozenset[str] = frozenset(
{
# Identity
"$feature_flag",
"$feature_flag_response",
"$feature_flag_has_experiment",
# Evaluation debug
"$feature_flag_id",
"$feature_flag_version",
"$feature_flag_reason",
"$feature_flag_request_id",
"$feature_flag_evaluated_at",
"$feature_flag_error",
"locally_evaluated",
# Correctness-required
"$groups",
"$process_person_profile",
# Linkage / SDK identity
"$session_id",
"$lib",
"$lib_version",
"$is_server",
# Processing-control sentinel this SDK sets to deliver the event correctly
"$geoip_disable",
# Static platform/runtime identity: cheap, low-cardinality dimensions kept
# for platform/runtime breakdowns on flag-call debugging.
"$os",
"$os_version",
"$os_distro",
"$python_runtime",
"$python_version",
}
)
def _parse_has_experiment(value: Any) -> Optional[bool]:
"""Server-reported experiment linkage; anything but an explicit bool means unknown."""
return value if isinstance(value, bool) else None
def _parse_flag_payload(raw_payload: Any) -> Optional[Any]:
"""Flag payloads are stored as JSON strings, both in the ``/flags`` response
metadata and in the local-evaluation flag definitions, so decode them before
handing them to callers. A string that isn't valid JSON is passed through as-is."""
if isinstance(raw_payload, str):
if not raw_payload:
return None
try:
return json.loads(raw_payload)
except (json.JSONDecodeError, TypeError):
return raw_payload
return raw_payload
def _metadata_has_experiment(metadata: Any) -> Optional[bool]:
"""Server-reported experiment linkage from flag metadata; ``None`` when absent
(e.g. ``LegacyFlagMetadata``, which doesn't carry the field)."""
return metadata.has_experiment if isinstance(metadata, FlagMetadata) else None
class _Lane:
"""A capture lane: a queue drained by its own consumer pool, posting to one endpoint.
Internal and unexported. The client owns one lane per traffic class
(analytics, AI) so each gets its own backpressure, flush cadence,
per-event size cap, and wire protocol without lane-conditional branches
in shared consumer code.
"""
log = logging.getLogger("posthog")
def __init__(
self,
*,
name,
api_key,
host,
on_error,
max_queue_size,
thread_count,
send,
flush_at,
flush_interval,
gzip,
max_retries,
timeout,
historical_migration,
endpoint,
max_msg_size,
capture_mode,
capture_compression,
eager_start,
):
self.name = name
self.api_key = api_key
self.host = host
self.on_error = on_error
self.send = send
self.flush_at = flush_at
self.flush_interval = flush_interval
self.gzip = gzip
self.max_retries = max_retries
self.timeout = timeout
self.historical_migration = historical_migration
self.endpoint = endpoint
self.max_msg_size = max_msg_size
self.capture_mode = capture_mode
self.capture_compression = capture_compression
self._max_queue_size = max_queue_size
self._thread_count = thread_count
self._eager_start = eager_start
self.queue: Queue = _new_lane_queue(max_queue_size)
self.available = not isinstance(self.queue, _DisabledLaneQueue)
self.consumers: List[Consumer] = []
self._started = False
self._closed = False
self._active_sync_sends = 0
self._start_lock = threading.Lock()
self._sync_sends_done = threading.Condition(self._start_lock)
self._drain_signal = _DrainSignal(self.queue)
if eager_start and self.available:
self.start()
def _start_locked(self) -> None:
if self._started or self._closed or not self.available:
return
for _ in range(self._thread_count):
consumer = Consumer(
self.queue,
self.api_key,
host=self.host,
on_error=self.on_error,
flush_at=self.flush_at,
flush_interval=self.flush_interval,
gzip=self.gzip,
retries=self.max_retries,
timeout=self.timeout,
historical_migration=self.historical_migration,
endpoint=self.endpoint,
max_msg_size=self.max_msg_size,
capture_mode=self.capture_mode,
capture_compression=self.capture_compression,
)
consumer._set_drain_signal(self._drain_signal)
self.consumers.append(consumer)
if self.send:
consumer.start()
self._started = True
def start(self):
"""Construct this lane's consumer pool, starting its threads when sending is enabled.
Idempotent and thread-safe, so concurrent first captures start exactly
one pool.
"""
with self._start_lock:
self._start_locked()
def enqueue(self, msg) -> bool:
"""Atomically admit and queue `msg`, starting the lane on its first event."""
with self._start_lock:
if self._closed or not self.available:
return False
self._start_locked()
try:
self.queue.put(msg, block=False)
return True
except Full:
return False
def run_sync_if_open(self, send) -> bool:
"""Run a synchronous send admitted before closure, and report whether it ran."""
with self._sync_sends_done:
if self._closed:
return False
self._active_sync_sends += 1
try:
send()
finally:
with self._sync_sends_done:
self._active_sync_sends -= 1
if not self._active_sync_sends:
self._sync_sends_done.notify_all()
return True
def close(self) -> None:
"""Terminal: atomically refuse all future queue and sync admissions."""
with self._start_lock:
self._closed = True
def wait_for_sync_sends(self) -> None:
"""Wait for synchronous sends admitted before close to finish."""
with self._sync_sends_done:
while self._active_sync_sends:
self._sync_sends_done.wait()
def flush(self, timeout_seconds: Optional[float]) -> None:
"""Block until this lane's queue drains, or until `timeout_seconds` elapse.
Signals the consumers first so a partial batch is delivered now instead
of waiting out `flush_at` / `flush_interval`.
"""
queue = self.queue
# Keep the request active only while this flush is waiting. This avoids
# an empty flush changing how events captured after it are batched.
self._drain_signal.request()
try:
size = queue.qsize()
deadline = (
None if timeout_seconds is None else time.monotonic() + timeout_seconds
)
while queue.unfinished_tasks:
if deadline is None and not any(
consumer.is_alive() for consumer in self.consumers
):
self.discard_undrainable_queued_work()
break
with queue.all_tasks_done:
if not queue.unfinished_tasks:
break
if deadline is None:
wait_seconds = 0.05
else:
remaining = deadline - time.monotonic()
if remaining <= 0:
self.log.warning(
"%s lane flush ran out of budget (%.1fs granted) with %s items pending.",
self.name,
timeout_seconds,
queue.unfinished_tasks,
)
return
wait_seconds = min(0.05, remaining)
queue.all_tasks_done.wait(wait_seconds)
# Note that this message may not be precise, because of threading.
self.log.debug("successfully flushed about %s items.", size)
finally:
self._drain_signal.complete()
def discard_undrainable_queued_work(self) -> None:
"""Balance queued work when this lane has no running sender."""
if any(consumer.is_alive() for consumer in self.consumers):
return
dropped = 0
while True:
try:
self.queue.get_nowait()
except Empty:
break
self.queue.task_done()
dropped += 1
if dropped:
self.log.warning(
"%s lane discarded %d queued events because no consumer is running",
self.name,
dropped,
)
def join(self) -> None:
"""Pause this lane's consumers and wait for them to exit."""
# Normal teardown bypasses the batching wait so a partial batch is sent.
errors: list[Exception] = []
drain_requested = False
try:
self._drain_signal.request()
drain_requested = True
except Exception as error:
self.log.exception(
"Failed to request %s lane drain during lifecycle cleanup", self.name
)
errors.append(error)
for consumer in self.consumers:
try:
consumer._pause(drain=True)
except Exception as error:
self.log.exception(
"Failed to pause %s lane consumer during lifecycle cleanup",
self.name,
)
errors.append(error)
for consumer in self.consumers:
try:
consumer.join()
except RuntimeError:
# consumer thread has not started
pass
except Exception as error:
self.log.exception(
"Failed to join %s lane consumer during lifecycle cleanup",
self.name,
)
errors.append(error)
try:
self.discard_undrainable_queued_work()
except Exception as error:
self.log.exception(
"Failed to discard queued %s lane work during lifecycle cleanup",
self.name,
)
errors.append(error)
if drain_requested:
try:
self._drain_signal.complete()
except Exception as error:
self.log.exception(
"Failed to complete %s lane drain during lifecycle cleanup",
self.name,
)
errors.append(error)
if errors:
raise errors[0]
def reset_sync_send_state_after_fork(self) -> None:
"""Replace sync-send state inherited from threads that did not survive fork."""
self._active_sync_sends = 0
self._start_lock = threading.Lock()
self._sync_sends_done = threading.Condition(self._start_lock)
def rebuild_after_fork(self, *, closed: bool) -> None:
"""Replace fork-unsafe lane state in a forked child.
Threads do not survive fork() and queue.Queue internal locks may be in
an inconsistent state, so the queue, lock, and consumer pool are
replaced. Inherited queue items are not retained as they'll be handled
by the parent process's consumers. ``closed`` normalizes every lane to
the client's fork-visible lifecycle state. An eager open lane restarts
immediately; a lazy lane returns to not-started and restarts on next use.
"""
self.queue = _new_lane_queue(self._max_queue_size)
self.available = not isinstance(self.queue, _DisabledLaneQueue)
self.reset_sync_send_state_after_fork()
self._drain_signal = _DrainSignal(self.queue)
self.consumers = []
self._started = False
self._closed = closed
if self._eager_start and self.available:
self.start()
class Client(object):
"""
This is the SDK reference for the PostHog Python SDK.
You can learn more about example usage in the [Python SDK documentation](/docs/libraries/python).
You can also follow [Flask](/docs/libraries/flask) and [Django](/docs/libraries/django)
guides to integrate PostHog into your project.
For long-running applications, create one client during application startup
and reuse it for the lifetime of the process. This keeps background queues
predictable and makes shutdown flushing straightforward. Multiple clients are
still supported for intentional multi-project or multi-host setups.
Examples:
```python
from posthog import Posthog
posthog = Posthog('<ph_project_api_key>', host='<ph_client_api_host>')
posthog.debug = True
if settings.TEST:
posthog.disabled = True
```
"""
log = logging.getLogger("posthog")
_client_registry_lock = threading.Lock()
_client_registry_pid = os.getpid()
_client_registry: dict[tuple[str, str], weakref.WeakSet] = {}
_duplicate_client_warnings: set[tuple[str, str]] = set()
def __init__(
self,
project_api_key: str,
host=None,
debug=False,
max_queue_size=10000,
send=True,
on_error=None,
flush_at=100,
flush_interval=5.0,
gzip=False,
max_retries=3,
sync_mode=False,
timeout=15,
thread=1,
poll_interval=30,
personal_api_key=None,
disabled=False,
disable_geoip=True,
is_server=True,
historical_migration=False,
feature_flags_request_timeout_seconds=3,
feature_flags_request_max_retries=1,
super_properties=None,
enable_exception_autocapture=False,
log_captured_exceptions=False,
project_root=None,
privacy_mode=False,
before_send=None,
flag_fallback_cache_url=None,
enable_local_evaluation=True,
flag_definition_cache_provider: Optional[FlagDefinitionCacheProvider] = None,
capture_exception_code_variables=False,
code_variables_mask_patterns=None,
code_variables_ignore_patterns=None,
code_variables_mask_url_credentials=None,
code_variables_detect_secrets=None,
in_app_modules: list[str] | None = None,
enable_exception_autocapture_rate_limiting=False,
exception_autocapture_bucket_size=ExceptionCapture.DEFAULT_BUCKET_SIZE,
exception_autocapture_refill_rate=ExceptionCapture.DEFAULT_REFILL_RATE,
exception_autocapture_refill_interval_seconds=ExceptionCapture.DEFAULT_REFILL_INTERVAL_SECONDS,
capture_mode: Optional[Union[CaptureMode, str]] = None,
capture_compression: Optional[Union[CaptureCompression, str]] = None,
secret_key=None,
metrics: Optional[dict] = None,
enable_full_ai_capture=False,
_use_ai_lane=False,
_enable_multimodal_capture=False,
):
"""
Initialize a new PostHog client instance.
Args:
project_api_key: PostHog project API key/token.
host: PostHog host. Defaults to the US ingestion endpoint when not
set. App hosts such as ``https://us.posthog.com`` are mapped to
the corresponding ingestion host.
debug: Enable verbose SDK logging and re-raise errors from public
API methods.
max_queue_size: Maximum number of events buffered before upload.
send: If False, queueing succeeds but events are not sent.
on_error: Optional callback invoked by background consumers when an
upload fails. Keep it short and non-blocking. Calling lifecycle
methods directly is safe and deferred, but do not start another
thread or task that calls ``flush()``, ``join()``, or
``shutdown()`` and then wait for it from the callback.
flush_at: Number of queued events that triggers a batch upload.
flush_interval: Maximum seconds a background consumer waits before
flushing a partial batch.
gzip: Whether to gzip event upload payloads.
max_retries: Number of upload retries. Values below 0 are treated as 0.
sync_mode: If True, send each event synchronously instead of using
background worker threads.
timeout: HTTP request timeout in seconds for event uploads.
thread: Number of background consumer threads.
poll_interval: Seconds between local feature flag definition refreshes.
secret_key: A Personal API Key or Project Secret API Key, used to
authenticate local feature flag evaluation, remote config
payloads, and decrypted flag payloads. Example::
posthog.Client(project_api_key, secret_key="phx_...")
personal_api_key: Deprecated alias for ``secret_key``. Still honored
for backwards compatibility; prefer ``secret_key``, which also
accepts a Project Secret API Key.
disabled: If True, disable captures and API requests. Useful in tests.
disable_geoip: Whether to disable server-side GeoIP enrichment.
Defaults to True.
is_server: Whether events are emitted from a server-side runtime.
Defaults to True; set to False when using the SDK as a client/CLI
so the device OS is attributed to the person normally.
historical_migration: Mark events as historical migration imports.
feature_flags_request_timeout_seconds: Timeout in seconds for feature
flag and remote config requests.
feature_flags_request_max_retries: Number of retries for feature flag
requests after network, transport, or timeout failures. Defaults
to 1. Set to 0 to disable retries.
super_properties: Properties merged into every captured event.
enable_exception_autocapture: Automatically capture uncaught
exceptions.
log_captured_exceptions: Also log exceptions captured by error
tracking.
project_root: Root path used to determine in-app stack frames for
captured exceptions. Defaults to the current working directory.
privacy_mode: For AI observability, capture usage metadata without
prompt inputs or outputs.
enable_full_ai_capture: Route PostHog AI wrapper events through
the dedicated AI capture endpoint and capture full AI content:
skips string truncation and passes media (base64/data URIs)
through unredacted. ``privacy_mode`` always wins. Defaults to
False.
before_send: Optional callback that can modify or drop events before
upload. Return ``None`` to drop an event.
flag_fallback_cache_url: Optional feature flag fallback cache URL,
such as ``memory://local/?ttl=300&size=10000`` or a Redis URL.
enable_local_evaluation: Whether to poll feature flag definitions for
local evaluation when a personal API key is configured.
flag_definition_cache_provider: Optional external cache provider for
sharing feature flag definitions across workers.
capture_exception_code_variables: Capture local variable values on
exception stack frames.
code_variables_mask_patterns: Variable-name patterns to mask when
capturing code variables.
code_variables_ignore_patterns: Variable-name patterns to omit when
capturing code variables.
code_variables_mask_url_credentials: Scrub credentials embedded in
URLs/DSNs (e.g. ``user:pass@host``) from captured code variables,
regardless of the surrounding variable name. Defaults to True.
code_variables_detect_secrets: Last-resort entropy-based detection that
redacts high-entropy secret-looking values (API keys, tokens, strong
passwords) sitting in innocuously-named variables, after the name and
URL checks. Skips structured ids (UUIDs, ObjectIds, hashes). Defaults
to True.
in_app_modules: Module/package prefixes treated as in-app frames in
captured exceptions.
enable_exception_autocapture_rate_limiting: Rate limit
autocaptured exceptions client-side with a token bucket per
exception type. Disabled by default.
exception_autocapture_bucket_size: Maximum burst of autocaptured
exceptions allowed per exception type (token bucket size,
clamped to 0-100).
exception_autocapture_refill_rate: Tokens restored per refill
interval for each exception type's bucket.
exception_autocapture_refill_interval_seconds: Seconds between
token refills for autocaptured exception rate limiting.
capture_mode: Capture wire protocol to use. Defaults to
``CaptureMode.V0`` (legacy ``/batch/``). Set ``CaptureMode.V1``
(or pass the string ``"v1"``) to opt into
``/i/v1/analytics/events``. When omitted, the
``POSTHOG_CAPTURE_MODE`` env var is consulted, then ``V0``.
capture_compression: Request-body compression for capture-v1 uploads
(ignored in V0, which uses ``gzip``). ``CaptureCompression.GZIP``
or ``DEFLATE`` (or the strings ``"gzip"``/``"deflate"``). When
omitted, the ``POSTHOG_CAPTURE_COMPRESSION`` env var is consulted,
then the legacy ``gzip`` flag, then no compression.
Examples:
```python
from posthog import Posthog
posthog = Posthog('<ph_project_api_key>', host='<ph_app_host>')
```
Category:
Initialization
"""
# api_key: This should be the Team API Key (token), public
self.api_key = (project_api_key or "").strip()
self.on_error = on_error
self.debug = debug
self.send = send
self.sync_mode = sync_mode
self._lifecycle_lock = threading.Lock()
self._lifecycle_condition = threading.Condition(self._lifecycle_lock)
self._lifecycle_owner: Optional[threading.Thread] = None
self._workers_joined = False
self._join_cleanup_complete = False
self._join_requested = False
self._shutdown_requested = False
self._shutdown_complete = False
self._lifecycle_cleanup_failed = False
self._deferred_lifecycle_thread_pending = False
self._deferred_lifecycle_dirty = False
self._lifecycle_callback_context: ContextVar[bool] = ContextVar(
"posthog_lifecycle_callback", default=False
)
self._deferred_flush_lock = threading.Lock()
self._deferred_flush_pending = False
self._deferred_flush_followup = False
self._deferred_flush_followup_timeout: Optional[float] = None
# Used for session replay URL generation - we don't want the server host here.
self.raw_host = normalize_host(host)
self.host = determine_server_host(host)
self._duplicate_client_registry_key: Optional[tuple[str, str]] = None
self.gzip = gzip
self.timeout = timeout
self.max_retries = max(0, max_retries)
self._feature_flags: Optional[list[Any]] = (
None # private variable to store flags
)
self.feature_flags_by_key: Optional[dict[str, Any]] = None
self.group_type_mapping: Optional[dict[str, str]] = None
self.cohorts: Optional[dict[str, Any]] = None
self.poll_interval = poll_interval
self.feature_flags_request_timeout_seconds = (
feature_flags_request_timeout_seconds
)
self.feature_flags_request_max_retries = max(
0, feature_flags_request_max_retries
)
self.poller: Optional[Poller] = None
self.distinct_ids_feature_flags_reported = SizeLimitedDict(MAX_DICT_SIZE, set)
self.flag_fallback_cache_url = flag_fallback_cache_url
self.flag_cache = self._initialize_flag_cache(flag_fallback_cache_url)
self.flag_definition_version = 0
self._flags_etag: Optional[str] = None
self._flag_definition_fetch_generation = 0
self._flag_definition_published_generation = 0
self._flag_definition_cache_generation = 0
self._flag_definition_publication_lock = threading.Lock()
self._flag_definition_cache_write_lock = threading.RLock()
self._flag_definition_cache_provider = flag_definition_cache_provider
self._flag_definition_cache_provider_async_runner: Optional[
_BackgroundEventLoopRunner
] = None
self._flag_definition_cache_provider_async_runner_lock = threading.Lock()
self.disabled = disabled or not self.api_key
self.disable_geoip = disable_geoip
self._metrics_config = metrics
self._metrics: Optional[PostHogMetrics] = None
self._metrics_lock = threading.Lock()
# `_use_ai_lane` / `_enable_multimodal_capture` are deprecated aliases.
self.enable_full_ai_capture = (
enable_full_ai_capture is True
or _use_ai_lane is True
or _enable_multimodal_capture is True
)
self.is_server = is_server
self.historical_migration = historical_migration
# Selects the capture wire protocol (V0 legacy `/batch/` vs V1
# `/i/v1/analytics/events`). Resolved here so the env-var fallback is
# applied once; V0 is the default and keeps upgrades transparent.
self.capture_mode = _resolve_capture_mode(capture_mode)
# v1-only request compression; falls back to the legacy `gzip` flag when
# neither the kwarg nor POSTHOG_CAPTURE_COMPRESSION is set.
self.capture_compression = _resolve_capture_compression(
capture_compression, gzip_fallback=gzip
)
self.super_properties = super_properties
self.enable_exception_autocapture = enable_exception_autocapture
self.log_captured_exceptions = log_captured_exceptions
self.enable_exception_autocapture_rate_limiting = (
enable_exception_autocapture_rate_limiting
)
self.exception_autocapture_bucket_size = exception_autocapture_bucket_size
self.exception_autocapture_refill_rate = exception_autocapture_refill_rate
self.exception_autocapture_refill_interval_seconds = (
exception_autocapture_refill_interval_seconds
)
self.exception_capture = None
self.privacy_mode = privacy_mode
self.enable_local_evaluation = enable_local_evaluation
# Server-controlled gate for minimal $feature_flag_called events, read from
# the /flags v2 response and the local-evaluation payload. False until the
# server reports it, so full events are the fail-safe.
self._minimal_flag_called_events: bool = False
self.capture_exception_code_variables = capture_exception_code_variables
self.code_variables_mask_patterns = (
code_variables_mask_patterns
if code_variables_mask_patterns is not None
else DEFAULT_CODE_VARIABLES_MASK_PATTERNS
)
self.code_variables_ignore_patterns = (
code_variables_ignore_patterns
if code_variables_ignore_patterns is not None
else DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS
)
self.code_variables_mask_url_credentials = (
code_variables_mask_url_credentials
if code_variables_mask_url_credentials is not None
else DEFAULT_CODE_VARIABLES_MASK_URL_CREDENTIALS
)
self.code_variables_detect_secrets = (
code_variables_detect_secrets
if code_variables_detect_secrets is not None
else DEFAULT_CODE_VARIABLES_DETECT_SECRETS
)
self.in_app_modules = in_app_modules
if project_root is None:
try:
project_root = os.getcwd()
except Exception:
project_root = None
self.project_root = project_root
if personal_api_key is not None and secret_key is None:
warnings.warn(
"`personal_api_key` is deprecated; use `secret_key` instead. "
"`secret_key` accepts a Personal API Key or a Project Secret API Key.",
DeprecationWarning,
stacklevel=2,
)
elif secret_key is not None and personal_api_key is not None:
self.log.warning(
"[FEATURE FLAGS] Both `secret_key` and `personal_api_key` were "
"provided; using `secret_key` and ignoring `personal_api_key`."
)
resolved_secret_key = secret_key if secret_key is not None else personal_api_key
self.secret_key = (
resolved_secret_key.strip()
if isinstance(resolved_secret_key, str)
else resolved_secret_key
) or None
self.personal_api_key = self.secret_key
if debug:
# Ensures that debug level messages are logged when debug mode is on.
# Otherwise, defaults to WARNING level. See https://docs.python.org/3/howto/logging.html#what-happens-if-no-configuration-is-provided
logging.basicConfig()
self.log.setLevel(logging.DEBUG)
else:
self.log.setLevel(logging.WARNING)
if not self.api_key:
self.log.error(
"api_key is empty after trimming whitespace; check your project API key"
)
self._set_before_send(before_send)
if self.enable_exception_autocapture:
self.exception_capture = ExceptionCapture(
self,
rate_limiting_enabled=self.enable_exception_autocapture_rate_limiting,
bucket_size=self.exception_autocapture_bucket_size,
refill_rate=self.exception_autocapture_refill_rate,
refill_interval_seconds=self.exception_autocapture_refill_interval_seconds,
)