The Power of Enhanced Usage Metrics in Optimizing Platform Event Allocation

The Power of Enhanced Usage Metrics in Optimizing Platform Event Allocation

Last Updated on July 11, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

  • How to monitor the consumption of Platform Events based on event name, client ID, event type, and usage type

Objectives:

After reading this blog, you’ll be able to:

  • Gain a comprehensive understanding of platform events
  • Discover the advantages of monitoring consumption patterns for system performance optimization and usage trend identification
  • Implement enhanced usage metrics for platform events through a step-by-step guide
  • Explore the benefits of monitoring consumption patterns for optimizing system performance and identifying usage trends
  • and much more

Business Use case

Jestilla Zetkin is working as a Salesforce Architect at Gurukul On Cloud (GoC), recently came across a blog post titled Monitoring Platform Event Publishing and Delivery Usage. This post provided insights on how to various ways to monitor the usage of platform event publishing and delivery.

As GoC’s business expands, the influx of data has led to frequent instances of hitting the daily limits for platform events. Consequently, Jestilla is seeking a solution to monitor platform event consumption based on event name, client ID, or type. This monitoring capability will enable her to analyze which event is consuming a significant portion of the allocation, empowering her to take appropriate actions accordingly.

What is Platform Events?

Event-driven architectures have become very popular in recent years and for a good reason. It is based on the fire-and-forget pattern. Firing an event and letting each system handle business logic allows you to keep unrelated systems decoupled and can help simplify the architecture.

Platform events enable you to deliver secure, scalable, and customizable event notifications within Salesforce or from external sources. Apps can publish and subscribe to Platform Events on the Salesforce platform using Apex, Flow, or in an external system using CometD. If you want to learn more about Platform Event, check out the Trailhead Module.

Understand PlatformEventUsageMetric Object

Let’s take a pause here and familiarize yourself with the PlatformEventUsageMetric object in Salesforce.

It contains usage data for event publishing and delivery to CometD and Pub/Sub API clients, empApi Lightning components, and event relays. If Enhanced Usage Metrics is enabled, usage data is available for the last 24 hours, ending at the last hour, and for historical daily usage.

In API 58.0 and later, you can enable Enhanced Usage Metrics to get usage data by event name and client for granular time intervals. PlatformEventUsageMetric contains separate usage metrics for platform events and change data capture events.

Field Name
Details
EventType The type of event you would like to query usage metrics for, such as a change event or a custom platform event. This field is available when Enhanced Usage Metrics is enabled.
TimeSegment  The time interval used for aggregating usage data returned in the query results. This field is available when Enhanced Usage Metrics is enabled.
UsageType This field is available when Enhanced Usage Metrics is enabled. The type of event usage metrics to query for, such as event publishing or event delivery. Use this field with the EventName or EventType fields.Possible values are:

  • PUBLISH—Usage metrics for published events.
  • DELIVERY—Usage metrics for events that were delivered to subscribers.
Value The usage value for the specified metric and date range.

To obtain comprehensive usage data for each event, we will leverage Enhanced Event Usage Metrics. This feature will enable us to gather aggregated information and identify the specific events that are consuming a significant portion of the allocation.

Whereas, PlatformEventSettings represents settings for platform events and change data capture events.

Field Name
Details
enableEnhancedUsageMetrics Enables enhanced usage metrics for queries run against PlatformEventUsageMetric. Enhanced usage metrics provide additional fields for the queries and granular time segments. For more information, see Enhanced Usage Metrics in the Platform Events Developer Guide. Default value is false.

Guided Practice (We-do):

Before you can get more usage metrics,, you need to enable Enhanced Usage Metrics in the Metadata API. To do this, set the value of the enableEnhancedUsageMetrics field to true in the PlatformEventSettings. Once this is done, you’ll be able to get detailed usage metrics for platform event consumption.

Step 1: Enable Enhanced Usage Metrics

  1. Ensure that Visual Studio Code is properly set up and your Salesforce org is connected to it.
  2. Modify the package.xml to retrieve platform event-related metadata.
    1. Sample package.xml to retrieve PlatformEvent Settings.
      
      
       <types>
           <members>PlatformEvent</members>
           <name>Settings</name>
      </types>

  3. The next step is to modify PlatformEventSettings.
    1. Proceed to modify PlatformEventSettings, which values are stored in the PlatformEvent.settings file in the settings folder.
    2. In PlatformEventSettings, set the enableEnhancedUsageMetrics field to true.
    3. Save the changes in the PlatformEvent.settings-meta.xml file.
  4. Finally, right-click your package file in Explorer and select SFDX: Deploy Source in Manifest to Org.

Step 2.1: Get Daily Usage Metrics Aggregated by Event Name

  1. Click Setup | Developer Console.
  2. In the Query Editor, run the following SOQL query
    
    
    SELECT EventName, EventType, UsageType, Value, StartDate, EndDate FROM PlatformEventUsageMetric WHERE TimeSegment='Daily'

Step 2.2: Get Usage Metrics for the Last 24 Hours Aggregated by Event Name

  1. Click Setup | Developer Console.
  2. In the Query Editor, run the following SOQL query
    
    
    SELECT EventName, EventType, UsageType, Value, StartDate, EndDate FROM PlatformEventUsageMetric WHERE TimeSegment='Hourly'

👉 Check out the video for step-by-step instructions.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.