Class: Apptrail::ApptrailEventsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/apptrail-application-events-sdk.rb

Overview

You can use the Apptrail Application Events Client to send audit log events from your Ruby applications.

See [Sending events](apptrail.com/docs/applications/guide/working-with-events/overview).

Instance Method Summary collapse

Constructor Details

#initialize(region:, api_key:) ⇒ ApptrailEventsClient

Returns a new instance of ApptrailEventsClient.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/apptrail-application-events-sdk.rb', line 65

def initialize(region:, api_key:)
  @_region = region
  begin
    parsed_key = Base64.urlsafe_decode64(api_key)
    application_id, *_  = parsed_key.split(',', 3)
    @_application_id = application_id
  rescue
    raise ArgumentError, "Invalid API Key." 
  end

  @_base_api_url = "https://events.#{region}.apptrail.com/applications/session"
  @_api_key = api_key
end

Instance Method Details

#inspectObject



57
58
59
# File 'lib/apptrail-application-events-sdk.rb', line 57

def inspect
  "#<ApptrailEventsClient:#{object_id}>"
end

#put_event(event) ⇒ Object

Send a single audit event to log to Apptrail.

Parameters:



82
83
84
# File 'lib/apptrail-application-events-sdk.rb', line 82

def put_event(event)
  put_events([event])
end

#put_events(events) ⇒ Object

Send a list of up to 1000 audit events to log to Apptrail.

Parameters:



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
# File 'lib/apptrail-application-events-sdk.rb', line 90

def put_events(events)
  unless events.is_a?(Array)
    raise TypeError, "Must pass in array of events."
  end
  unless events.length <= 1000
    raise ArgumentError, "Can not send more than 1000 events in a single PutEvents call."
  end

  begin
    JSON::Validator.validate!(SCHEMA, events, :list => true, :parse_data => false)
  rescue JSON::Schema::ValidationError => e
    raise Apptrail::ApptrailError.new(e.message)
  end

  if @_upload_url.nil? || @_form_data.nil?
    _refresh_post_policy()
  end

  content = ""
  events.each do |evt|
    content << JSON.generate(evt)
    content << "\n"
  end

  filename = SecureRandom.uuid + ".jsonl"
  s3_key = File.join(@_application_id, filename)

  form_file = HTTP::FormData::File.new(StringIO.new(content), :filename => filename)

  new_form_opts = {
    **@_form_data,
    :key => s3_key,
    :file => form_file
  }

  begin
    Retriable.with_context(:s3) do
      resp = HTTP.post(@_upload_url, :form => new_form_opts)
      if !resp.status.success?
        if (resp.status.server_error?)
          raise ApptrailRetryableError.new("Server Error while putting events.")
        elsif (resp.status.client_error?)
          if (resp.status.code === 403 && resp.body.to_s.include?("Policy expired"))
            _refresh_post_policy()
            raise ApptrailRetryableError.new("Session expired.")
          else
            puts resp.body.to_s
            raise ApptrailError.new("Error while putting events.")
          end
        else
          raise ApptrailError.new("Error while putting events.")
        end
      end
    end
  rescue
    raise ApptrailError.new("Failed to put #{events.length} events. Encountered error.")
  else
    Apptrail::logger.info("Successfully wrote #{events.length} events.")
  end

end

#to_sObject



61
62
63
# File 'lib/apptrail-application-events-sdk.rb', line 61

def to_s
  "ApptrailEventsClient{}"
end