Apptrail Application Events SDK for Ruby

You can use the Apptrail Application Events SDK for Ruby to send audit logs from your Ruby applications to your customers.

Learn more

Notes and tips

  • Instantiate the client once at the top of your application and reuse it to prevent unnecessary recreation.

Installing

gem "apptrail-application-events-sdk"

Inside of your Ruby program, run:

require "apptrail-application-events-sdk"

Instantiating client

require "apptrail-application-events-sdk"

my_api_key = loadMySecretApiKey()
my_region = "us-west-2"

events_client = Apptrail::ApptrailEventsClient.new(
  region: my_region,
  apiKey: my_api_key,
)

Sending an event

event = {
  tenantId: "cust_MGY4MmYzNDMtZjEwOC00OWI",
  eventName: "CreateRepository",
  eventTime: "2022-01-26T06:01:00Z",
  actor: {
    id: "acct_MmRlODllZDctM2I0Yi0",
    details: {
      type: "account",
      name: "API Access",
    },
  },
  resources: [
    {
      id: "repo_YWI5NjkzY2UtNzI1Ny00N",
      details: {
        repositoryType: "V2",
      },
    },
  ],
  context: {
    sourceIpAddress: "103.6.179.245",
    userAgent: "Apache-HttpClient/4.5.3 (Java/11.0.11)"
  },
  tags: {
    severity: "LOW",
  },
  eventDetails: {
    request: {
      repositoryName: "my-repository",
    },
  },
}

events_client.putEvent(event)

Handling errors

As a best practice, you should handle errors while sending events, especially if you are sending critical logs. The Events client includes automatic retries with backoff, but errors can happen due to rare server issues or client side issues.

You can choose what to do with failing events. For example, you may sideline them to disk, or a dead letter queue for retry or remediation.

begin
  events_client.putEvent(event);
rescue ApptrailError => e
  puts e
  # handle error
end