Class: Orb::Resources::Events
- Inherits:
-
Object
- Object
- Orb::Resources::Events
- Defined in:
- lib/orb/resources/events.rb,
lib/orb/resources/events/volume.rb,
lib/orb/resources/events/backfills.rb,
sig/orb/resources/events.rbs,
sig/orb/resources/events/volume.rbs,
sig/orb/resources/events/backfills.rbs
Overview
The Event resource represents a usage event that has been created for a customer. Events are the core of Orb's usage-based billing model, and are used to calculate the usage charges for a given billing period.
Defined Under Namespace
Instance Attribute Summary collapse
-
#backfills ⇒ Orb::Resources::Events::Backfills
readonly
The Event resource represents a usage event that has been created for a customer.
-
#volume ⇒ Orb::Resources::Events::Volume
readonly
The Event resource represents a usage event that has been created for a customer.
Instance Method Summary collapse
-
#deprecate(event_id, request_options: {}) ⇒ Orb::Models::EventDeprecateResponse
This endpoint is used to deprecate a single usage event with a given
event_id. -
#ingest(events:, backfill_id: nil, debug: nil, request_options: {}) ⇒ Orb::Models::EventIngestResponse
Some parameter documentations has been truncated, see Models::EventIngestParams for more details.
-
#initialize(client:) ⇒ Events
constructor
private
A new instance of Events.
-
#search(event_ids:, timeframe_end: nil, timeframe_start: nil, request_options: {}) ⇒ Orb::Models::EventSearchResponse
Some parameter documentations has been truncated, see Models::EventSearchParams for more details.
-
#update(event_id, event_name:, properties:, timestamp:, customer_id: nil, external_customer_id: nil, request_options: {}) ⇒ Orb::Models::EventUpdateResponse
Some parameter documentations has been truncated, see Models::EventUpdateParams for more details.
Constructor Details
#initialize(client:) ⇒ Events
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 Events.
412 413 414 415 416 |
# File 'lib/orb/resources/events.rb', line 412 def initialize(client:) @client = client @backfills = Orb::Resources::Events::Backfills.new(client: client) @volume = Orb::Resources::Events::Volume.new(client: client) end |
Instance Attribute Details
#backfills ⇒ Orb::Resources::Events::Backfills (readonly)
The Event resource represents a usage event that has been created for a customer. Events are the core of Orb's usage-based billing model, and are used to calculate the usage charges for a given billing period.
13 14 15 |
# File 'lib/orb/resources/events.rb', line 13 def backfills @backfills end |
#volume ⇒ Orb::Resources::Events::Volume (readonly)
The Event resource represents a usage event that has been created for a customer. Events are the core of Orb's usage-based billing model, and are used to calculate the usage charges for a given billing period.
19 20 21 |
# File 'lib/orb/resources/events.rb', line 19 def volume @volume end |
Instance Method Details
#deprecate(event_id, request_options: {}) ⇒ Orb::Models::EventDeprecateResponse
This endpoint is used to deprecate a single usage event with a given event_id.
event_id refers to the idempotency_key passed in during ingestion.
This endpoint will mark the existing event as ignored. Note that if you attempt
to re-ingest an event with the same event_id as a deprecated event, Orb will
return an error.
This is a powerful and audit-safe mechanism to retroactively deprecate a single event in cases where you need to:
- no longer bill for an event that was improperly reported
- no longer bill for an event based on the result of an external API call (e.g. call to a payment gateway failed and the user should not be billed)
If you want to only change specific properties of an event, but keep the event as part of the billing calculation, use the Amend event endpoint instead.
This API is always audit-safe. The process will still retain the deprecated event, though it will be ignored for billing calculations. For auditing and data fidelity purposes, Orb never overwrites or permanently deletes ingested usage data.
Request validation
- Orb does not accept an
idempotency_keywith the event in this endpoint, since this request is by design idempotent. On retryable errors, you should retry the request and assume the deprecation operation has not succeeded until receipt of a 2xx. - The event's
timestampmust fall within the customer's current subscription's billing period, or within the grace period of the customer's current subscription's previous billing period. Orb does not allow deprecating events for billing periods that have already invoiced customers. - The
customer_idor theexternal_customer_idof the original event ingestion request must identify a Customer resource within Orb, even if this event was ingested during the initial integration period. We do not allow deprecating events for customers not in the Orb system. - By default, no more than 100 events can be deprecated for a single customer in a 100 day period. For higher volume updates, consider using the event backfill endpoint.
149 150 151 152 153 154 155 156 |
# File 'lib/orb/resources/events.rb', line 149 def deprecate(event_id, params = {}) @client.request( method: :put, path: ["events/%1$s/deprecate", event_id], model: Orb::Models::EventDeprecateResponse, options: params[:request_options] ) end |
#ingest(events:, backfill_id: nil, debug: nil, request_options: {}) ⇒ Orb::Models::EventIngestResponse
Some parameter documentations has been truncated, see Models::EventIngestParams for more details.
Orb's event ingestion model and API is designed around two core principles:
- Data fidelity: The accuracy of your billing model depends on a robust foundation of events. Orb's API protocol encourages usage patterns that ensure that your data is consistently complete and correct.
- Fast integration: Sending events into Orb requires no tedious setup steps or explicit field schema for your event shape, making it instant to start streaming in usage in real-time.
Event shape
Events are the starting point for all usage calculations in the system, and are simple at their core:
{
// customer_id and external_customer_id are used to
// attribute usage to a given Customer. Exactly one of these
// should be specified in a given ingestion event.
// `customer_id` is the Orb generated identifier for the Customer,
// which is returned from the Create customer API call.
customer_id: string,
// external_customer_id is an alternate identifier which is associated
// with a Customer at creation time. This is treated as an alias for
// customer_id, and is usually set to an identifier native to your system.
external_customer_id: string,
// A string name identifying the event, usually a usage
// action. By convention, this should not contain any whitespace.
event_name: string,
// An ISO 8601 format date with no timezone offset.
// This should represent the time that usage occurred
// and is important to attribute usage to a given
// billing period. See the notes below on determining the timestamp.
// e.g. 2020-12-09T16:09:53Z
timestamp: string,
// A unique value, generated by the client, that is
// used to de-duplicate events.
// Exactly one event with a given
// idempotency key will be ingested, which allows for
// safe request retries.
idempotency_key: string
// Optional custom metadata to attach to the event.
// This might include a numeric value used for aggregation,
// or a string/boolean value used for filtering.
// The schema of this dictionary need not be pre-declared, and
// properties can be added at any time.
properties: {
[key: string]?: string | number | boolean,
},
}
Required fields
Because events streamed to Orb are meant to be as flexible as possible, there are only a few required fields in every event.
- We recommend that
idempotency_keyare unique strings that you generated with V4 UUIDs, but only require that they uniquely identify an event (i.e. don’t collide). - The
timestampfield in the event body will be used to determine which billable period a given event falls into. For example, with a monthly billing cycle starting from the first of December, Orb will calculate metrics based on events that fall into the range12-01 00:00:00 <= timestamp < 01-01 00:00:00.
Logging metadata
Orb allows tagging events with metadata using a flexible properties dictionary. Since Orb does not enforce a rigid schema for this field-set, key-value pairs can be added dynamically as your events evolve.
This dictionary can be helpful for a wide variety of use cases:
- Numeric properties on events like
compute_time_mscan later be inputs to our flexible query engine to determine usage. - Logging a region or cluster with each event can help you provide customers more granular visibility into their usage.
- If you are using matrix pricing and matching a matrix price key with a property, you should ensure the value for that property is sent as a string.
We encourage logging this metadata with an eye towards future use cases to ensure full coverage for historical data. The datatype of the value in the properties dictionary is important for metric creation from an event source. Values that you wish to numerically aggregate should be of numeric type in the event.
Determining event timestamp
For cases where usage is being reported in real time as it is occurring, timestamp should correspond to the time that usage occurred.
In cases where usage is reported in aggregate for a historical timeframe at a
regular interval, we recommend setting the event timestamp to the midpoint of
the interval. As an example, if you have an hourly reporter that sends data once
an hour for the previous hour of usage, setting the timestamp to the half-hour
mark will ensure that the usage is counted within the correct period.
Note that other time-related fields (e.g. time elapsed) can be added to the properties dictionary as necessary.
In cases where usage is reported in aggregate for a historical timeframe, the
timestamp must be within the grace period set for your account. Events with
timestamp < current_time - grace_period will not be accepted as a valid event,
and will throw validation errors. Enforcing the grace period enables Orb to
accurately map usage to the correct billing cycle and ensure that all usage is
billed for in the corresponding billing period.
In general, Orb does not expect events with future dated timestamps. In cases where the timestamp is 5 minutes ahead of the current time, the event will not be accepted as a valid event, and will throw validation errors.
Event validation
Orb’s validation ensures that you recognize errors in your events as quickly as possible, and the API provides informative error messages to help you fix problems quickly.
We validate the following:
- Exactly one of
customer_idandexternal_customer_idshould be specified. - If the
customer_idis specified, the customer in Orb must exist. - If the
external_customer_idis specified, the customer in Orb does not need to exist. Events will be attributed to any future customers with theexternal_customer_idon subscription creation. timestampmust conform to ISO 8601 and represent a timestamp at most 5 minutes in the future. This timestamp should be sent in UTC timezone (no timezone offset).
Idempotency and retry semantics
Orb's idempotency guarantees allow you to implement safe retry logic in the event of network or machine failures, ensuring data fidelity. Each event in the request payload is associated with an idempotency key, and Orb guarantees that a single idempotency key will be successfully ingested at most once. Note that when Orb encounters events with duplicate idempotency keys and differing event bodies in a batch of events, the entire batch will be rejected.
- Successful responses return a 200 HTTP status code. The response contains information about previously processed events.
- Requests that return a
4xxHTTP status code indicate a payload error and contain at least one event with a validation failure. An event with a validation failure can be re-sent to the ingestion endpoint (after the payload is fixed) with the original idempotency key since that key is not marked as processed. - Requests that return a
5xxHTTP status code indicate a server-side failure. These requests should be retried in their entirety.
API usage and limits
The ingestion API is designed made for real-time streaming ingestion and architected for high throughput. Even if events are later deemed unnecessary or filtered out, we encourage you to log them to Orb if they may be relevant to billing calculations in the future.
To take advantage of the real-time features of the Orb platform and avoid any chance of dropped events by producers, we recommend reporting events to Orb frequently. Optionally, events can also be briefly aggregated at the source, as this API accepts an array of event bodies.
Orb does not currently enforce a hard rate-limit for API usage or a maximum request payload size, but please give us a heads up if you’re changing either of these factors by an order of magnitude from initial setup.
Example: ingestion response
{
"validation_failed": []
}
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/orb/resources/events.rb', line 352 def ingest(params) query_params = [:backfill_id, :debug] parsed, = Orb::EventIngestParams.dump_request(params) query = Orb::Internal::Util.encode_query_params(parsed.slice(*query_params)) @client.request( method: :post, path: "ingest", query: query, body: parsed.except(*query_params), model: Orb::Models::EventIngestResponse, options: ) end |
#search(event_ids:, timeframe_end: nil, timeframe_start: nil, request_options: {}) ⇒ Orb::Models::EventSearchResponse
Some parameter documentations has been truncated, see Models::EventSearchParams for more details.
This endpoint returns a filtered set of events for an account in a paginated list format.
Note that this is a POST endpoint rather than a GET endpoint because it
employs a JSON body for search criteria rather than query parameters, allowing
for a more flexible search syntax.
Note that a search criteria must be specified. Currently, Orb supports the following criteria:
event_ids: This is an explicit array of IDs to filter by. Note that an event's ID is theidempotency_keythat was originally used for ingestion.
By default, Orb will not throw a 404 if no events matched, Orb will return an
empty array for data instead.
398 399 400 401 402 403 404 405 406 407 |
# File 'lib/orb/resources/events.rb', line 398 def search(params) parsed, = Orb::EventSearchParams.dump_request(params) @client.request( method: :post, path: "events/search", body: parsed, model: Orb::Models::EventSearchResponse, options: ) end |
#update(event_id, event_name:, properties:, timestamp:, customer_id: nil, external_customer_id: nil, request_options: {}) ⇒ Orb::Models::EventUpdateResponse
Some parameter documentations has been truncated, see Models::EventUpdateParams for more details.
This endpoint is used to amend a single usage event with a given event_id.
event_id refers to the idempotency_key passed in during ingestion. The event
will maintain its existing event_id after the amendment.
This endpoint will mark the existing event as ignored, and Orb will only use the
new event passed in the body of this request as the source of truth for that
event_id. Note that a single event can be amended any number of times, so the
same event can be overwritten in subsequent calls to this endpoint. Only a
single event with a given event_id will be considered the source of truth at
any given time.
This is a powerful and audit-safe mechanism to retroactively update a single event in cases where you need to:
- update an event with new metadata as you iterate on your pricing model
- update an event based on the result of an external API call (e.g. call to a payment gateway succeeded or failed)
This amendment API is always audit-safe. The process will still retain the original event, though it will be ignored for billing calculations. For auditing and data fidelity purposes, Orb never overwrites or permanently deletes ingested usage data.
Request validation
- The
timestampof the new event must match thetimestampof the existing event already ingested. As with ingestion, all timestamps must be sent in ISO8601 format with UTC timezone offset. - The
customer_idorexternal_customer_idof the new event must match thecustomer_idorexternal_customer_idof the existing event already ingested. Exactly one ofcustomer_idandexternal_customer_idshould be specified, and similar to ingestion, the ID must identify a Customer resource within Orb. Unlike ingestion, for event amendment, we strictly enforce that the Customer must be in the Orb system, even during the initial integration period. We do not allow updating theCustomeran event is associated with. - Orb does not accept an
idempotency_keywith the event in this endpoint, since this request is by design idempotent. On retryable errors, you should retry the request and assume the amendment operation has not succeeded until receipt of a 2xx. - The event's
timestampmust fall within the customer's current subscription's billing period, or within the grace period of the customer's current subscription's previous billing period. - By default, no more than 100 events can be amended for a single customer in a 100 day period. For higher volume updates, consider using the event backfill endpoint.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/orb/resources/events.rb', line 89 def update(event_id, params) parsed, = Orb::EventUpdateParams.dump_request(params) @client.request( method: :put, path: ["events/%1$s", event_id], body: parsed, model: Orb::Models::EventUpdateResponse, options: ) end |