Xem thêm

Monitor Platform Event Limits – Platform Event Tracker

Trying to keep track of the current limit for platform events in your organization can be a challenging task. It becomes even more difficult when you need to monitor your platform event usage limits. But...

connected_dots

Trying to keep track of the current limit for platform events in your organization can be a challenging task. It becomes even more difficult when you need to monitor your platform event usage limits. But fear not, we have some insights that can make this process easier for you.

REST API

One way to identify the number of events that have been delivered to external subscribers is by using the REST API. By getting the limits, you can quickly create and publish some generic events for an external subscriber. However, there is a catch. The API has changed after version 47.0, and the method of getting the limits is not as straightforward as before. The previous endpoint "MonthlyPlatformEvents" has been replaced with "MonthlyPlatformEventsUsageEntitlement," which is currently broken. So this method may not be the most reliable option.

PlatformEventUsageMetric

Another option is to query the "PlatformEventUsageMetric" object. This object contains usage data for event publishing and CometD-client delivery. However, there are some nuances to be aware of. When queried from the Dev Console, it may only show one row, but there are actually more rows stored. To see all the rows, you can run a specific query in the Dev Console.

Dev Console Query

By examining the StartDate and EndDate values, you can identify the rolling 24-hour period. The important thing to note is the name field, which can have four different values:

  • CHANGE_EVENTS_DELIVERED: Number of change data capture events delivered to CometD clients.
  • CHANGE_EVENTS_PUBLISHED: Number of change data capture events published.
  • PLATFORM_EVENTS_DELIVERED: Number of platform events delivered to CometD clients.
  • PLATFORM_EVENTS_PUBLISHED: Number of platform events published.

We will focus on tracking PLATFORM_EVENTS_DELIVERED, as it is usually the most sought-after and has the lowest limit. However, with a few small changes, you can track the other types of events as well.

To make tracking easier, we recommend creating a custom object named "Platform_Event_Tracker__c" with the following fields:

  • Api Name
  • Data Type
  • EndDate__c
  • DateTime
  • StartDate__c
  • DateTime
  • UniqueName__c
  • Text (Unique)
  • Value__c
  • Number (18,0)

Once you have created the custom object, you can create a schedulable class, "Schedulable_PlatformEventUsageCheck," that queries the "PlatformEventUsageMetric" object for records created on the current day. The class will then create a "Platform_Event_Tracker__c" record for each result and add it to a map. The map will remove the previous day's records and override them with the current rolling 24-hour data. Finally, the records will be inserted.

You can schedule the class to run hourly using the DynamicJobScheduler by executing the following code in Anonymous Apex:

// Code snippet to schedule the class

Additionally, you can create a subscribed report that will only be sent if the values reach a certain threshold. This way, you can stay informed about your platform event usage without constantly checking the numbers.

And there you have it! With these tips and tools, monitoring your platform event limits will become a much smoother process. For more information, you can check out our GitHub repo.

Note: The images used in this article are from the original source.

1