-
Notifications
You must be signed in to change notification settings - Fork 192
Add telemetry logging support to Trigger Actions Framework #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ssk42
wants to merge
1
commit into
mitchspano:main
Choose a base branch
from
ssk42:feature/176-telemetry-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
trigger-actions-framework/main/default/classes/ITriggerActionTelemetryLogger.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| Copyright 2020 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * @group Trigger Actions Framework | ||
| * @description Interface for telemetry logging implementations within the Trigger Actions Framework. | ||
| * | ||
| * This interface allows pluggable telemetry logging functionality that can be used to track | ||
| * limits, performance metrics, and other diagnostic information during trigger execution. | ||
| * | ||
| * Implementations can range from simple System.debug logging to integration with external | ||
| * logging frameworks like Nebula Logger. | ||
| * | ||
| * Example implementation: | ||
| * ``` | ||
| * public class LimitLogger implements ITriggerActionTelemetryLogger { | ||
| * public void log(TelemetryContext context) { | ||
| * System.debug('Queries: ' + Limits.getQueries() + '/' + Limits.getLimitQueries()); | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| public interface ITriggerActionTelemetryLogger { | ||
| /** | ||
| * @description Log telemetry information using the provided context. | ||
| * @param context The telemetry context containing information about the current execution state | ||
| */ | ||
| void log(TelemetryContext context); | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
trigger-actions-framework/main/default/classes/ITriggerActionTelemetryLogger.cls-meta.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>63.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
59 changes: 59 additions & 0 deletions
59
trigger-actions-framework/main/default/classes/LimitLogger.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| Copyright 2020 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * @group Trigger Actions Framework | ||
| * @description Basic implementation of ITriggerActionTelemetryLogger that logs Salesforce limits | ||
| * to the debug log. This implementation tracks SOQL queries, DML statements, CPU time, and heap size. | ||
| * | ||
| * This logger is designed for debugging and development purposes and should not be enabled | ||
| * in production environments as it may impact performance. | ||
| * | ||
| * Example usage: | ||
| * Configure this class in the Trigger_Action__mdt.Telemetry_Logger_Class__c field to enable | ||
| * telemetry logging for specific trigger actions. | ||
| */ | ||
| public class LimitLogger implements ITriggerActionTelemetryLogger { | ||
|
|
||
| /** | ||
| * @description Logs current Salesforce limits to the debug log with context information. | ||
| * @param context The telemetry context containing execution information | ||
| */ | ||
| public void log(TelemetryContext context) { | ||
| String phaseLabel = context.phase == TelemetryPhase.START ? 'START' : 'FINISH'; | ||
|
|
||
| String message = String.format( | ||
| '[{0}] {1} on {2}.{3} ({4} records) - Queries: {5}/{6}, DML: {7}/{8}, CPU: {9}/{10}ms, Heap: {11}/{12}bytes', | ||
| new List<String>{ | ||
| phaseLabel, | ||
| context.triggerOperation, | ||
| context.sObjectType, | ||
| context.actionClassName, | ||
| String.valueOf(context.recordCount), | ||
| String.valueOf(Limits.getQueries()), | ||
| String.valueOf(Limits.getLimitQueries()), | ||
| String.valueOf(Limits.getDmlStatements()), | ||
| String.valueOf(Limits.getLimitDmlStatements()), | ||
| String.valueOf(Limits.getCpuTime()), | ||
| String.valueOf(Limits.getLimitCpuTime()), | ||
| String.valueOf(Limits.getHeapSize()), | ||
| String.valueOf(Limits.getLimitHeapSize()) | ||
| } | ||
| ); | ||
|
|
||
| System.debug(LoggingLevel.DEBUG, message); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
trigger-actions-framework/main/default/classes/LimitLogger.cls-meta.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>63.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
85 changes: 85 additions & 0 deletions
85
trigger-actions-framework/main/default/classes/LimitLoggerTest.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| Copyright 2020 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * @group Trigger Actions Framework | ||
| * @description Test class for the LimitLogger telemetry implementation. | ||
| */ | ||
| @IsTest | ||
| private class LimitLoggerTest { | ||
|
|
||
| @IsTest | ||
| static void testLogWithStartPhase() { | ||
| TelemetryContext context = new TelemetryContext(); | ||
| context.actionClassName = 'TestTriggerAction'; | ||
| context.triggerOperation = 'BEFORE_INSERT'; | ||
| context.sObjectType = 'Account'; | ||
| context.recordCount = 5; | ||
| context.phase = TelemetryPhase.START; | ||
|
|
||
| LimitLogger logger = new LimitLogger(); | ||
|
|
||
| Test.startTest(); | ||
| logger.log(context); | ||
| Test.stopTest(); | ||
|
|
||
| // Since LimitLogger uses System.debug, we can't directly assert the output | ||
| // but we can verify the method executed without exceptions | ||
| System.assert(true, 'LimitLogger.log should execute without throwing exceptions'); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void testLogWithEndPhase() { | ||
| TelemetryContext context = new TelemetryContext(); | ||
| context.actionClassName = 'TestTriggerAction'; | ||
| context.triggerOperation = 'AFTER_UPDATE'; | ||
| context.sObjectType = 'Contact'; | ||
| context.recordCount = 10; | ||
| context.phase = TelemetryPhase.FINISH; | ||
|
|
||
| LimitLogger logger = new LimitLogger(); | ||
|
|
||
| Test.startTest(); | ||
| logger.log(context); | ||
| Test.stopTest(); | ||
|
|
||
| System.assert(true, 'LimitLogger.log should execute without throwing exceptions'); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void testLogWithZeroRecords() { | ||
| TelemetryContext context = new TelemetryContext(); | ||
| context.actionClassName = 'TestTriggerAction'; | ||
| context.triggerOperation = 'BEFORE_DELETE'; | ||
| context.sObjectType = 'Opportunity'; | ||
| context.recordCount = 0; | ||
| context.phase = TelemetryPhase.START; | ||
|
|
||
| LimitLogger logger = new LimitLogger(); | ||
|
|
||
| Test.startTest(); | ||
| logger.log(context); | ||
| Test.stopTest(); | ||
|
|
||
| System.assert(true, 'LimitLogger.log should handle zero records without throwing exceptions'); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void testImplementsInterface() { | ||
| ITriggerActionTelemetryLogger logger = new LimitLogger(); | ||
| System.assert(logger != null, 'LimitLogger should implement ITriggerActionTelemetryLogger interface'); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
trigger-actions-framework/main/default/classes/LimitLoggerTest.cls-meta.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>63.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did Claude list the year as 2020?