Skip to content

Commit c30f7e5

Browse files
authored
docs: document public API annotations (#149)
1 parent ee54a99 commit c30f7e5

30 files changed

Lines changed: 436 additions & 546 deletions

lib/posthog/backoff_policy.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
require 'posthog/defaults'
44

55
module PostHog
6+
# Retry backoff policy used by the SDK transport.
7+
#
8+
# @api private
69
class BackoffPolicy
710
include PostHog::Defaults::BackoffPolicy
811

9-
# @param [Hash] opts
12+
# @param opts [Hash]
1013
# @option opts [Numeric] :min_timeout_ms The minimum backoff timeout
1114
# @option opts [Numeric] :max_timeout_ms The maximum backoff timeout
1215
# @option opts [Numeric] :multiplier The value to multiply the current

lib/posthog/client.rb

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,28 @@ def _decrement_instance_count(api_key)
5050
end
5151
end
5252

53-
# @param [Hash] opts
54-
# @option opts [String] :api_key Your project's api_key
55-
# @option opts [String] :personal_api_key Your personal API key
56-
# @option opts [FixNum] :max_queue_size Maximum number of calls to be
57-
# remain queued. Defaults to 10_000.
58-
# @option opts [Bool] :test_mode +true+ if messages should remain
59-
# queued for testing. Defaults to +false+.
60-
# @option opts [Bool] :sync_mode +true+ to send events synchronously
61-
# on the calling thread. Useful in forking environments like Sidekiq
62-
# and Resque. Defaults to +false+.
63-
# @option opts [Proc] :on_error Handles error calls from the API.
64-
# @option opts [String] :host Fully qualified hostname of the PostHog server. Defaults to `https://us.i.posthog.com`
65-
# @option opts [Integer] :feature_flags_polling_interval How often to poll for feature flag definition changes.
66-
# Measured in seconds, defaults to 30.
67-
# @option opts [Integer] :feature_flag_request_timeout_seconds How long to wait for feature flag evaluation.
68-
# Measured in seconds, defaults to 3.
69-
# @option opts [Proc] :before_send A block that receives the event hash and should return either a modified hash
70-
# to be sent to PostHog or nil to prevent the event from being sent. e.g. `before_send: ->(event) { event }`
71-
# @option opts [Bool] :disable_singleton_warning +true+ to suppress the warning when multiple clients
72-
# share the same API key. Use only when you intentionally need multiple clients. Defaults to +false+.
73-
# @option opts [Object] :flag_definition_cache_provider An object implementing the
74-
# {FlagDefinitionCacheProvider} interface for distributed flag definition caching.
75-
# EXPERIMENTAL: This API may change in future minor version bumps.
53+
# @param opts [Hash] Client configuration.
54+
# @option opts [String] :api_key Your project's API key. Required.
55+
# @option opts [String, nil] :personal_api_key Your personal API key. Required for local feature flag evaluation.
56+
# @option opts [String] :host Fully qualified hostname of the PostHog server. Defaults to `https://us.i.posthog.com`.
57+
# @option opts [Integer] :max_queue_size Maximum number of calls to remain queued. Defaults to 10_000.
58+
# @option opts [Integer] :batch_size Maximum number of events to send in one async batch.
59+
# @option opts [Boolean] :test_mode +true+ if messages should remain queued for testing. Defaults to +false+.
60+
# @option opts [Boolean] :sync_mode +true+ to send events synchronously on the calling thread. Useful in
61+
# forking environments like Sidekiq and Resque. Defaults to +false+.
62+
# @option opts [Proc] :on_error Callback invoked as `on_error.call(status, error)` for API or serialization errors.
63+
# @option opts [Integer] :feature_flags_polling_interval How often to poll for feature flag definition changes,
64+
# in seconds. Defaults to 30.
65+
# @option opts [Integer] :feature_flag_request_timeout_seconds How long to wait for feature flag evaluation,
66+
# in seconds. Defaults to 3.
67+
# @option opts [Proc] :before_send A callback that receives the event hash and should return either a modified
68+
# hash to be sent to PostHog or nil to prevent the event from being sent. e.g. `before_send: ->(event) { event }`.
69+
# @option opts [Boolean] :disable_singleton_warning +true+ to suppress the warning when multiple clients share
70+
# the same API key. Use only when you intentionally need multiple clients. Defaults to +false+.
71+
# @option opts [Boolean] :skip_ssl_verification +true+ to disable SSL certificate verification for requests.
72+
# Intended only for local development or custom deployments.
73+
# @option opts [Object] :flag_definition_cache_provider An object implementing the {FlagDefinitionCacheProvider}
74+
# interface for distributed flag definition caching. EXPERIMENTAL: This API may change in future minor versions.
7675
def initialize(opts = {})
7776
symbolize_keys!(opts)
7877

@@ -143,7 +142,9 @@ def initialize(opts = {})
143142
# Synchronously waits until the worker has cleared the queue.
144143
#
145144
# Use only for scripts which are not long-running, and will specifically
146-
# exit
145+
# exit.
146+
#
147+
# @return [void]
147148
def flush
148149
if @sync_mode
149150
# Wait for any in-flight sync send to complete
@@ -159,7 +160,9 @@ def flush
159160

160161
# Clears the queue without waiting.
161162
#
162-
# Use only in test mode
163+
# Use only in test mode.
164+
#
165+
# @return [void]
163166
def clear
164167
@queue.clear
165168
end
@@ -176,8 +179,10 @@ def clear
176179
#
177180
# @option attrs [String] :event Event name
178181
# @option attrs [Hash] :properties Event properties (optional)
179-
# @option attrs [Bool, Hash, SendFeatureFlagsOptions] :send_feature_flags
180-
# Whether to send feature flags with this event, or configuration for feature flag evaluation (optional)
182+
# @option attrs [Hash] :groups Group analytics mapping from group type to group key (optional)
183+
# @option attrs [Boolean, Hash, SendFeatureFlagsOptions] :send_feature_flags
184+
# Deprecated. Whether to send feature flags with this event, or configuration for feature flag evaluation
185+
# (optional)
181186
# @option attrs [PostHog::FeatureFlagEvaluations] :flags A snapshot returned by
182187
# {#evaluate_flags}. When present, `$feature/<key>` and `$active_feature_flags` are
183188
# attached from the snapshot without making an additional /flags request, and this
@@ -189,6 +194,7 @@ def clear
189194
# @note If `:distinct_id` is omitted, request/context distinct_id is used when
190195
# available; otherwise a UUID is generated and the event is marked personless
191196
# with `$process_person_profile: false`.
197+
# @return [Boolean] Whether the event was queued or sent.
192198
# @macro common_attrs
193199
def capture(attrs)
194200
symbolize_keys! attrs
@@ -268,9 +274,10 @@ def capture(attrs)
268274
# @param [String] distinct_id The ID for the user (optional, defaults to request/context distinct_id
269275
# or a generated UUID)
270276
# @param [Hash] additional_properties Additional properties to include with the exception event (optional)
271-
# @param [PostHog::FeatureFlagEvaluations] flags A snapshot returned by {#evaluate_flags}.
277+
# @param flags [PostHog::FeatureFlagEvaluations, nil] A snapshot returned by {#evaluate_flags}.
272278
# Forwarded to the inner {#capture} call so the captured `$exception` event carries the
273279
# same `$feature/<key>` and `$active_feature_flags` properties as the snapshot.
280+
# @return [Boolean, nil] Whether the exception event was queued or sent, or nil if the input could not be parsed.
274281
def capture_exception(exception, distinct_id = nil, additional_properties = {}, flags: nil)
275282
exception_info = ExceptionCapture.build_parsed_exception(exception)
276283

@@ -295,6 +302,7 @@ def capture_exception(exception, distinct_id = nil, additional_properties = {},
295302
# @param [Hash] attrs
296303
#
297304
# @option attrs [Hash] :properties User properties (optional)
305+
# @return [Boolean] Whether the identify event was queued or sent.
298306
# @macro common_attrs
299307
def identify(attrs)
300308
symbolize_keys! attrs
@@ -309,6 +317,7 @@ def identify(attrs)
309317
# @option attrs [String] :group_key Group key
310318
# @option attrs [Hash] :properties Group properties (optional)
311319
# @option attrs [String] :distinct_id Distinct ID (optional)
320+
# @return [Boolean] Whether the group identify event was queued or sent.
312321
# @macro common_attrs
313322
def group_identify(attrs)
314323
symbolize_keys! attrs
@@ -320,23 +329,32 @@ def group_identify(attrs)
320329
# @param [Hash] attrs
321330
#
322331
# @option attrs [String] :alias The alias to give the distinct id
332+
# @return [Boolean] Whether the alias event was queued or sent.
323333
# @macro common_attrs
324334
def alias(attrs)
325335
symbolize_keys! attrs
326336
enqueue(FieldParser.parse_for_alias(attrs))
327337
end
328338

329-
# @return [Hash] pops the last message from the queue
339+
# @return [Hash] Pops the last message from the queue. Intended for test mode.
330340
def dequeue_last_message
331341
@queue.pop
332342
end
333343

334-
# @return [Fixnum] number of messages in the queue
344+
# @return [Integer] Number of messages in the queue. Intended for test mode.
335345
def queued_messages
336346
@queue.length
337347
end
338348

339-
# @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#is_enabled} instead.
349+
# @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#enabled?} instead.
350+
# @param flag_key [String, Symbol] The unique key of the feature flag.
351+
# @param distinct_id [String] The distinct id of the user.
352+
# @param groups [Hash] Group analytics mapping from group type to group key.
353+
# @param person_properties [Hash] Properties to use when evaluating the user locally or remotely.
354+
# @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
355+
# @param only_evaluate_locally [Boolean] Skip the remote /flags call.
356+
# @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
357+
# @return [Boolean, nil] Whether the flag is enabled, or nil when the flag could not be evaluated.
340358
# TODO: In future version, rename to `feature_flag_enabled?`
341359
def is_feature_enabled( # rubocop:disable Naming/PredicateName
342360
flag_key,
@@ -366,8 +384,8 @@ def is_feature_enabled( # rubocop:disable Naming/PredicateName
366384
!!response
367385
end
368386

369-
# @param [String, Symbol] flag_key The unique flag key of the feature flag
370-
# @return [String] The decrypted value of the feature flag payload
387+
# @param flag_key [String, Symbol] The unique flag key of the remote config feature flag.
388+
# @return [Hash] The parsed remote config payload response.
371389
def get_remote_config_payload(flag_key)
372390
@feature_flags_poller.get_remote_config_payload(flag_key.to_s)
373391
end
@@ -379,8 +397,10 @@ def get_remote_config_payload(flag_key)
379397
# @param [Hash] groups
380398
# @param [Hash] person_properties key-value pairs of properties to associate with the user.
381399
# @param [Hash] group_properties
400+
# @param only_evaluate_locally [Boolean] Skip the remote /flags call.
401+
# @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
382402
#
383-
# @return [String, nil] The value of the feature flag
403+
# @return [String, Boolean, nil] The value of the feature flag
384404
#
385405
# The provided properties are used to calculate feature flags locally, if possible.
386406
#
@@ -420,6 +440,14 @@ def get_feature_flag(
420440

421441
# @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#get_flag} /
422442
# {FeatureFlagEvaluations#get_flag_payload} instead.
443+
# @param key [String, Symbol] The unique key of the feature flag.
444+
# @param distinct_id [String] The distinct id of the user.
445+
# @param groups [Hash] Group analytics mapping from group type to group key.
446+
# @param person_properties [Hash] Properties to use when evaluating the user locally or remotely.
447+
# @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
448+
# @param only_evaluate_locally [Boolean] Skip the remote /flags call.
449+
# @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
450+
# @return [PostHog::FeatureFlagResult, nil]
423451
def get_feature_flag_result(
424452
key,
425453
distinct_id,
@@ -456,7 +484,8 @@ def get_feature_flag_result(
456484
# @param [Hash] person_properties key-value pairs of properties to associate with the user
457485
# @param [Hash] group_properties
458486
# @param [Boolean] only_evaluate_locally Skip the remote /flags call entirely
459-
# @param [Boolean] disable_geoip Stamped on captured access events
487+
# @param [Boolean, nil] disable_geoip When true, disables GeoIP lookup for remote evaluation and stamps captured
488+
# access events.
460489
# @param [Array<String, Symbol>] flag_keys When set, scopes the underlying /flags
461490
# request to only these flag keys (sent as `flag_keys_to_evaluate`).
462491
# Distinct from {FeatureFlagEvaluations#only}, which filters the
@@ -580,6 +609,7 @@ def evaluate_flags(
580609
# @param [Hash] groups
581610
# @param [Hash] person_properties key-value pairs of properties to associate with the user.
582611
# @param [Hash] group_properties
612+
# @param only_evaluate_locally [Boolean] Skip the remote /flags call.
583613
#
584614
# @return [Hash] String (not symbol) key value pairs of flag and their values
585615
def get_all_flags(
@@ -602,11 +632,12 @@ def get_all_flags(
602632
#
603633
# @param [String, Symbol] key The key of the feature flag
604634
# @param [String] distinct_id The distinct id of the user
605-
# @option [String or boolean] match_value The value of the feature flag to be matched
606-
# @option [Hash] groups
607-
# @option [Hash] person_properties key-value pairs of properties to associate with the user.
608-
# @option [Hash] group_properties
609-
# @option [Boolean] only_evaluate_locally
635+
# @param match_value [String, Boolean, nil] The value of the feature flag to be matched
636+
# @param groups [Hash]
637+
# @param person_properties [Hash] key-value pairs of properties to associate with the user.
638+
# @param group_properties [Hash]
639+
# @param only_evaluate_locally [Boolean]
640+
# @return [Object, nil] The parsed payload for the matched flag value.
610641
#
611642
# @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#get_flag_payload} instead.
612643
def get_feature_flag_payload(
@@ -639,10 +670,10 @@ def get_feature_flag_payload(
639670
# featureFlagPayloads: A hash of feature flag payloads
640671
#
641672
# @param [String] distinct_id The distinct id of the user
642-
# @option [Hash] groups
643-
# @option [Hash] person_properties key-value pairs of properties to associate with the user.
644-
# @option [Hash] group_properties
645-
# @option [Boolean] only_evaluate_locally
673+
# @param groups [Hash]
674+
# @param person_properties [Hash] key-value pairs of properties to associate with the user.
675+
# @param group_properties [Hash]
676+
# @param only_evaluate_locally [Boolean] Skip the remote /flags call.
646677
#
647678
def get_all_flags_and_payloads(
648679
distinct_id,
@@ -664,6 +695,9 @@ def get_all_flags_and_payloads(
664695
response
665696
end
666697

698+
# Reload locally cached feature flag definitions.
699+
#
700+
# @return [void]
667701
def reload_feature_flags
668702
unless @personal_api_key
669703
logger.error(
@@ -674,6 +708,9 @@ def reload_feature_flags
674708
@feature_flags_poller.load_feature_flags(true)
675709
end
676710

711+
# Flush pending events and stop background resources.
712+
#
713+
# @return [void]
677714
def shutdown
678715
self.class._decrement_instance_count(@api_key) if @api_key
679716
@feature_flags_poller.shutdown_poller

lib/posthog/exception_capture.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@
1111
# 💖 open source (under MIT License)
1212

1313
module PostHog
14+
# Builds PostHog exception payloads from Ruby exception objects.
15+
#
16+
# @api private
1417
module ExceptionCapture
1518
RUBY_INPUT_FORMAT = /
1619
^ \s* (?: [a-zA-Z]: | uri:classloader: )? ([^:]+ | <.*>):
1720
(\d+)
1821
(?: :in\s('|`)(?:([\w:]+)\#)?([^']+)')?$
1922
/x
2023

24+
# @param value [Exception, String, Object] Exception input to parse.
25+
# @return [Hash, nil] Parsed exception payload, or nil when the input is unsupported.
2126
def self.build_parsed_exception(value)
2227
title, message, backtrace = coerce_exception_input(value)
2328
return nil if title.nil?
2429

2530
build_single_exception_from_data(title, message, backtrace)
2631
end
2732

33+
# @param title [String]
34+
# @param message [String, nil]
35+
# @param backtrace [Array<String>, nil]
36+
# @return [Hash]
2837
def self.build_single_exception_from_data(title, message, backtrace)
2938
{
3039
'type' => title,
@@ -37,6 +46,8 @@ def self.build_single_exception_from_data(title, message, backtrace)
3746
}
3847
end
3948

49+
# @param backtrace [Array<String>, nil]
50+
# @return [Hash, nil]
4051
def self.build_stacktrace(backtrace)
4152
return nil unless backtrace && !backtrace.empty?
4253

@@ -50,6 +61,8 @@ def self.build_stacktrace(backtrace)
5061
}
5162
end
5263

64+
# @param line [String]
65+
# @return [Hash, nil]
5366
def self.parse_backtrace_line(line)
5467
match = line.match(RUBY_INPUT_FORMAT)
5568
return nil unless match
@@ -72,13 +85,20 @@ def self.parse_backtrace_line(line)
7285
frame
7386
end
7487

88+
# @param path [String]
89+
# @return [Boolean]
7590
def self.gem_path?(path)
7691
path.include?('/gems/') ||
7792
path.include?('/ruby/') ||
7893
path.include?('/.rbenv/') ||
7994
path.include?('/.rvm/')
8095
end
8196

97+
# @param frame [Hash]
98+
# @param file_path [String]
99+
# @param lineno [Integer]
100+
# @param context_size [Integer]
101+
# @return [void]
82102
def self.add_context_lines(frame, file_path, lineno, context_size = 5)
83103
lines = File.readlines(file_path)
84104
return if lines.empty?
@@ -97,6 +117,8 @@ def self.add_context_lines(frame, file_path, lineno, context_size = 5)
97117
# Silently ignore file read errors
98118
end
99119

120+
# @param value [Exception, String, Object]
121+
# @return [Array] Three-item array of title, message, and backtrace.
100122
def self.coerce_exception_input(value)
101123
if value.is_a?(String)
102124
title = 'Error'

0 commit comments

Comments
 (0)