Skip to content
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

Enable sending items from triggers, futures, etc. via platform events #16

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions force-app/main/default/classes/DataBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ public with sharing class DataBuilder {
this.config = config;
}

public Map<String, Object> buildPayload(String level, String message)
public Map<String, Object> buildPayload(String level, String message, Map<String, Object> custom)
{
return buildPayloadStructure(level, buildMessageBody(message), null);
return buildPayloadStructure(level, buildMessageBody(message), custom);
}

public Map<String, Object> buildPayload(Exception exc)
public Map<String, Object> buildPayload(String level, Exception exc, Map<String, Object> custom)
{
return buildPayloadStructure('error', buildExceptionBody(exc), null);
}

public Map<String, Object> buildPayload(Exception exc, Map<String, Object> custom)
{
return buildPayloadStructure('error', buildExceptionBody(exc), custom);
return buildPayloadStructure(level, buildExceptionBody(exc), custom);
}

public Map<String, Object> buildPayload(ExceptionData exData)
Expand Down Expand Up @@ -52,14 +47,14 @@ public with sharing class DataBuilder {
}

private Map<String, Object> buildPayloadStructure(
String level,
Map<String, Object> body,
String level,
Map<String, Object> body,
Map<String, Object> custom
) {
Map<String, Object> data = this.buildDataStructure(
level,
this.config.environment(),
body,
level,
this.config.environment(),
body,
custom
);

Expand All @@ -75,7 +70,7 @@ public with sharing class DataBuilder {
Map<String, Object> body,
Map<String, Object> custom
) {

Map<String, Object> notifierMap = new Map<String, Object>();
notifierMap.put('name', Notifier.NAME);
notifierMap.put('version', Notifier.VERSION);
Expand Down Expand Up @@ -118,7 +113,7 @@ public with sharing class DataBuilder {
Map<String, Object> excMap = new Map<String, Object>();
excMap.put('class', exData.className());
excMap.put('message', exData.message());

return buildTraceStructure(excMap, framesList);
}

Expand Down
51 changes: 40 additions & 11 deletions force-app/main/default/classes/Notifier.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,61 @@ public with sharing class Notifier
this.dataBuilder = new DataBuilder(this.config);
}

public HttpResponse log(String level, String message)
public HttpResponse log(String level, String message, Map<String, Object> custom, SendMethod method)
{
return send(dataBuilder.buildPayload(level, message));
return send(dataBuilder.buildPayload(level, message, custom), method);
}

public HttpResponse log(Exception exc)
public HttpResponse log(String level, Exception exc, Map<String, Object> custom, SendMethod method)
{
return send(dataBuilder.buildPayload(exc));
return send(dataBuilder.buildPayload(level, exc, custom), method);
}

public HttpResponse log(Exception exc, Map<String, Object> custom)
public HttpResponse log(ExceptionData exData, SendMethod method)
{
return send(dataBuilder.buildPayload(exc, custom));
return send(dataBuilder.buildPayload(exData), method);
}

public HttpResponse log(ExceptionData exData)
public HttpResponse send(Map<String, Object> payload, SendMethod method)
{
return send(dataBuilder.buildPayload(exData));
return methodSend(JSON.serialize(payload), method);
}

private HttpResponse send(Map<String, Object> payload)
public HttpResponse methodSend(String payload, SendMethod method)
{
return send(JSON.serialize(payload));
HttpResponse response = null;

switch on method {
when SYNC {
response = syncSend(payload);
}
when FUTURE {
Notifier.futureSend(payload);
}
when else {
eventSend(payload);
}
}

return response;
}

private void eventSend(String payload)
{
RollbarEvent__e event = new RollbarEvent__e(Data1__c = payload);

EventBus.publish(event);
}

@future (callout=true)
private static void futureSend(String payload)
{
Notifier notifier = Rollbar.initializedInstance().notifier();

notifier.syncSend(payload);
}

private HttpResponse send(String payload)
private HttpResponse syncSend(String payload)
{
HttpRequest request = new HttpRequest();
request.setEndpoint(this.config.endpoint());
Expand Down
35 changes: 29 additions & 6 deletions force-app/main/default/classes/Rollbar.cls
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,45 @@ global with sharing class Rollbar {
}

global static HttpResponse log(String level, String message) {
return log(level, message, null, null);
}

global static HttpResponse log(String level, String message, Map<String, Object> custom) {
return log(level, message, custom, null);
}

global static HttpResponse log(String level, String message, SendMethod method) {
return log(level, message, null, method);
}

global static HttpResponse log(String level, String message, Map<String, Object> custom, SendMethod method) {
Rollbar instance = initializedInstance();
return instance.notifier.log(level, message);
return instance.notifier.log(level, message, custom, method);
}

global static HttpResponse log(Exception exc) {
Rollbar instance = initializedInstance();
return instance.notifier.log(exc);
return log(exc, null, null);
}

global static HttpResponse log(Exception exc, SendMethod method) {
return log(exc, null, method);
}

global static HttpResponse log(Exception exc, Map<String, Object> custom) {
return log(exc, custom, null);
}

global static HttpResponse log(Exception exc, Map<String, Object> custom, SendMethod method) {
Rollbar instance = initializedInstance();
return instance.notifier.log(exc, custom);
return instance.notifier.log('error', exc, custom, method);
}

global static HttpResponse log(ExceptionData exData) {
Rollbar instance = initializedInstance();
return instance.notifier.log(exData);
return instance.notifier.log(exData, SendMethod.SYNC);
}

private static Rollbar initializedInstance()
public static Rollbar initializedInstance()
{
Rollbar instance = Rollbar.instance();
if (!instance.initialized) {
Expand All @@ -73,6 +92,10 @@ global with sharing class Rollbar {
private Rollbar() {
}

public Notifier notifier() {
return notifier;
}

private static Rollbar instance = null;
private Config config = null;
private Notifier notifier = null;
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/RollbarInstaller.cls
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public with sharing class RollbarInstaller {
public static Boolean accessTokenCorrect(Config config)
{
Rollbar.init(config);
System.HttpResponse response = Rollbar.log('info', 'Rollbar Apex SDK installed correctly in ' + UserInfo.getOrganizationName());
System.HttpResponse response = Rollbar.log('info', 'Rollbar Apex SDK installed correctly in ' + UserInfo.getOrganizationName(), SendMethod.SYNC);
return response.getStatusCode() == 200;
}

Expand Down
1 change: 1 addition & 0 deletions force-app/main/default/classes/SendMethod.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global enum SendMethod {SYNC, FUTURE, EVENT}
5 changes: 5 additions & 0 deletions force-app/main/default/classes/SendMethod.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="SendMethod">
<apiVersion>47.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<deploymentStatus>Deployed</deploymentStatus>
<eventType>HighVolume</eventType>
<label>Rollbar Event</label>
<pluralLabel>Rollbar Events</pluralLabel>
<publishBehavior>PublishImmediately</publishBehavior>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Data1__c</fullName>
<externalId>false</externalId>
<isFilteringDisabled>false</isFilteringDisabled>
<isNameField>false</isNameField>
<isSortingDisabled>false</isSortingDisabled>
<label>Data1</label>
<required>false</required>
<type>LongTextArea</type>
<length>128000</length>
<visibleLines>3</visibleLines>
<unique>false</unique>
</CustomField>
16 changes: 8 additions & 8 deletions force-app/main/default/tests/DataBuilderTest.cls
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
@isTest
public class DataBuilderTest
public class DataBuilderTest
{
@isTest
static void testBuildMessagePayload()
{
DataBuilder subject = new DataBuilder(new Config('foo', 'bar'));
String expected = 'Message built in DataBuilderTest';

Map<String, Object> result = subject.buildPayload('info', expected);
Map<String, Object> result = subject.buildPayload('info', expected, null);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');

System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
Expand Down Expand Up @@ -40,7 +40,7 @@ public class DataBuilderTest
expectedClass = exc.getTypeName();
throw exc;
} catch(Exception exc) {
Map<String, Object> result = subject.buildPayload(exc);
Map<String, Object> result = subject.buildPayload('error', exc, null);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Expand Down Expand Up @@ -71,7 +71,7 @@ public class DataBuilderTest

DataBuilderTestException exc = new DataBuilderTestException('foobar');

Map<String, Object> result = subject.buildPayload(exc, expectedCustom);
Map<String, Object> result = subject.buildPayload('error', exc, expectedCustom);
Map<String, Object> data = (Map<String, Object>)result.get('data');
Map<String, Object> custom = (Map<String, Object>)data.get('custom');

Expand All @@ -98,7 +98,7 @@ public class DataBuilderTest
} catch(Exception exc1) {
expectedClass1 = exc1.getTypeName();

Map<String, Object> result = subject.buildPayload(exc1);
Map<String, Object> result = subject.buildPayload('error', exc1, null);

Map<String, Object> data = (Map<String, Object>)result.get('data');

Expand Down Expand Up @@ -135,7 +135,7 @@ public class DataBuilderTest
expected.put('context', 'Exception context');
expected.put('line', 14);
expected.put('column', 12);

ExceptionData exData = ExceptionData.fromMap(expected);

Map<String, Object> result = subject.buildPayload(exData);
Expand All @@ -145,7 +145,7 @@ public class DataBuilderTest
Map<String, Object> custom = (Map<String, Object>)data.get('custom');

System.assertEquals((String)expected.get('context'), (String)custom.get('context'));

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');
System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version'));
Expand Down
22 changes: 11 additions & 11 deletions force-app/main/default/tests/NotifierTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public class NotifierTest {

Notifier subject = new Notifier(config);

HttpResponse response = subject.log('info', 'Message from the Apex SDK');
HttpResponse response = subject.log('info', 'Message from the Apex SDK', null, SendMethod.SYNC);

System.assertEquals(200, response.getStatusCode());

JSONParser parser = JSON.createParser(response.getBody());
Integer err = null;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'err')) {
parser.nextToken();
err = parser.getIntegerValue();
Expand All @@ -35,14 +35,14 @@ public class NotifierTest {

DataBuilderTestException exc = new DataBuilderTestException();

HttpResponse response = subject.log(exc);
HttpResponse response = subject.log('error', exc, null, SendMethod.SYNC);

System.assertEquals(200, response.getStatusCode());

JSONParser parser = JSON.createParser(response.getBody());
Integer err = null;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'err')) {
parser.nextToken();
err = parser.getIntegerValue();
Expand All @@ -69,17 +69,17 @@ public class NotifierTest {
expected.put('context', 'Exception context');
expected.put('line', 14);
expected.put('column', 12);

ExceptionData exData = ExceptionData.fromMap(expected);

HttpResponse response = subject.log(exData);
HttpResponse response = subject.log(exData, SendMethod.SYNC);

System.assertEquals(200, response.getStatusCode());

JSONParser parser = JSON.createParser(response.getBody());
Integer err = null;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'err')) {
parser.nextToken();
err = parser.getIntegerValue();
Expand All @@ -88,4 +88,4 @@ public class NotifierTest {

System.assertEquals(0, err);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@isTest
global class RollbarApiVerifyRequestCalloutMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
String body = req.getBody();
Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(body);
Map<String, Object> data = (Map<String, Object>)payload.get('data');

Map<String, Object> notifierMap = (Map<String, Object>)data.get('notifier');
System.assertEquals(Notifier.NAME, (String)notifierMap.get('name'));
System.assertEquals(Notifier.VERSION, (String)notifierMap.get('version'));

HttpResponse response = new HttpResponse();
response.setStatusCode(200);
response.setBody('{"err":0,"result":{"id":null,"uuid":"e5ea9bee-08e6-41cc-a850-1863980dc224"}}');
return response;
}
}
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>47.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading