Class: PostHog::Transport Private

Inherits:
Object
  • Object
show all
Includes:
Defaults::Request, Logging, Utils
Defined in:
lib/posthog/transport.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

HTTP transport used by the SDK workers.

Constant Summary

Constants included from Utils

Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON

Constants included from Defaults::Request

Defaults::Request::HEADERS, Defaults::Request::HOST, Defaults::Request::PATH, Defaults::Request::PORT, Defaults::Request::RETRIES, Defaults::Request::SSL

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

Methods included from Utils

convert_to_datetime, date_in_iso8601, datetime_in_iso8601, deep_symbolize_keys, formatted_offset, get_by_symbol_or_string_key, is_valid_regex, isoify_dates, isoify_dates!, seconds_to_utc_offset, stringify_keys, symbolize_keys, symbolize_keys!, time_in_iso8601, uid

Constructor Details

#initialize(options = {}) ⇒ Transport

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Transport.

Parameters:

  • options (Hash) (defaults to: {})

    Transport configuration.

Options Hash (options):

  • :api_host (String)

    Full PostHog API host URL.

  • :host (String)

    Hostname to connect to.

  • :port (Integer)

    Port to connect to.

  • :ssl (Boolean)

    Whether to use HTTPS.

  • :headers (Hash)

    HTTP headers for batch requests.

  • :path (String)

    HTTP path for batch requests.

  • :retries (Integer)

    Number of retry attempts for retryable failures.

  • :backoff_policy (PostHog::BackoffPolicy)

    Backoff policy used between retries.

  • :skip_ssl_verification (Boolean)

    Disable SSL certificate verification.



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
# File 'lib/posthog/transport.rb', line 31

def initialize(options = {})
  if options[:api_host]
    uri = URI.parse(options[:api_host])
    options[:host] = uri.host
    options[:ssl] = uri.scheme == 'https'
    options[:port] = uri.port
  end

  options[:host] = options[:host].nil? ? HOST : options[:host]
  options[:port] = options[:port].nil? ? PORT : options[:port]
  options[:ssl] = options[:ssl].nil? ? SSL : options[:ssl]

  @headers = options[:headers] || HEADERS
  @path = options[:path] || PATH
  @retries = options[:retries] || RETRIES
  @backoff_policy = options[:backoff_policy] || PostHog::BackoffPolicy.new

  http = Net::HTTP.new(options[:host], options[:port])
  http.use_ssl = options[:ssl]
  http.read_timeout = 8
  http.open_timeout = 4
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:skip_ssl_verification]

  @http = http
end

Class Attribute Details

.stubObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



161
162
163
# File 'lib/posthog/transport.rb', line 161

def stub
  @stub || ENV.fetch('STUB', nil)
end

Instance Method Details

#send(api_key, batch) ⇒ Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a batch of messages to the API

Parameters:

  • api_key (String)

    Project API key.

  • batch (PostHog::MessageBatch, Array<Hash>)

    Batch of messages to send.

Returns:



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
# File 'lib/posthog/transport.rb', line 62

def send(api_key, batch)
  logger.debug("Sending request for #{batch.length} items")

  last_response, exception =
    retry_with_backoff(@retries) do
      status_code, body = send_request(api_key, batch)
      error =
        begin
          JSON.parse(body)['error']
        rescue JSON::ParserError
          body
        end
      should_retry = should_retry_request?(status_code, body)
      logger.debug("Response status code: #{status_code}")
      logger.debug("Response error: #{error}") if error

      [Response.new(status_code, error), should_retry]
    end

  if exception
    logger.error(exception.message)
    exception.backtrace.each { |line| logger.error(line) }
    Response.new(-1, exception.to_s)
  else
    last_response
  end
end

#shutdownvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Closes a persistent connection if it exists.



93
94
95
# File 'lib/posthog/transport.rb', line 93

def shutdown
  @http.finish if @http.started?
end