Skip to content

Commit 4cd8239

Browse files
authored
Merge branch 'develop' into NR-364636
2 parents 6beaf72 + 472e2a8 commit 4cd8239

File tree

7 files changed

+53
-48
lines changed

7 files changed

+53
-48
lines changed

Agent/HandledException/NRMAHandledExceptions.mm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ - (void) recordHandledException:(NSException*)exception {
334334

335335
- (void) processAndPublishPersistedReports {
336336
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
337-
auto context = _persistenceManager->retrieveStoreReports();
338-
if (context) {
339-
_publisher->publish(context);
340-
}
337+
_persistenceManager->retrieveAndPublishReports();
341338
});
342339
}
343340

Tests/Unit-Tests/NewRelicAgentTests/Handled-Exception-Tests/TestHexUploadPublisher.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ - (void)setUp {
5050
auto appLicense = std::make_shared<NewRelic::Hex::Report::ApplicationLicense>("ABCDEF12345");
5151
applicationInfo = std::make_shared<NewRelic::Hex::Report::AppInfo>(appLicense.get(),fbs::Platform_iOS);
5252
attributeValidator = new NewRelic::AttributeValidator([](const char*) {return true;},[](const char*) {return true;},[](const char*) {return true;});
53-
context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator);
53+
auto uploadPublisher = new NewRelic::Hex::HexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
54+
context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator,uploadPublisher);
5455
sessionId = std::string("ABSDFWERQWE");
5556
e = std::make_shared<NewRelic::Hex::Report::HandledException>(sessionId,
5657
1,
@@ -70,14 +71,14 @@ - (void)testHexUploaderCreation {
7071
auto appLicense = std::make_shared<NewRelic::Hex::Report::ApplicationLicense>("ABCDEF12345");
7172
std::shared_ptr<NewRelic::Hex::Report::AppInfo> applicationInfo = std::make_shared<NewRelic::Hex::Report::AppInfo>(appLicense.get(),fbs::Platform_iOS);
7273
NewRelic::AttributeValidator* attributeValidator = new NewRelic::AttributeValidator([](const char*) {return true;},[](const char*) {return true;},[](const char*) {return true;});
73-
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator);
74+
auto uploadPublisher = new NewRelic::Hex::HexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
75+
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator,uploadPublisher);
7476
std::shared_ptr<NewRelic::Hex::Report::HandledException> exception = std::make_shared<NewRelic::Hex::Report::HandledException>("sessionId",
7577
1,
7678
"The tea is too hot.",
7779
"HotTeaException",
7880
std::vector<std::shared_ptr<NewRelic::Hex::Report::Thread>>());
7981

80-
auto uploadPublisher = new NewRelic::Hex::HexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
8182
XCTAssertTrue(uploadPublisher!=NULL);
8283
}
8384

@@ -88,15 +89,15 @@ - (void) testHexUploaderWrapper {
8889
auto appLicense = std::make_shared<NewRelic::Hex::Report::ApplicationLicense>("ABCDEF12345");
8990
std::shared_ptr<NewRelic::Hex::Report::AppInfo> applicationInfo = std::make_shared<NewRelic::Hex::Report::AppInfo>(appLicense.get(),fbs::Platform_iOS);
9091
NewRelic::AttributeValidator* attributeValidator = new NewRelic::AttributeValidator([](const char*) {return true;},[](const char*) {return true;},[](const char*) {return true;});
91-
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator);
92+
auto uploadPublisher = new NewRelic::Hex::TestHexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
93+
auto uploader = uploadPublisher->getUploaderImpl();
94+
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator,uploadPublisher);
9295
std::shared_ptr<NewRelic::Hex::Report::HandledException> exception = std::make_shared<NewRelic::Hex::Report::HandledException>("sessionId",
9396
1,
9497
"The tea is too hot.",
9598
"HotTeaException",
9699
std::vector<std::shared_ptr<NewRelic::Hex::Report::Thread>>());
97100

98-
auto uploadPublisher = new NewRelic::Hex::TestHexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
99-
auto uploader = uploadPublisher->getUploaderImpl();
100101

101102
id mockWrapper = [OCMockObject partialMockForObject:((UploaderImpl*)uploader)->wrapper];
102103

@@ -106,7 +107,7 @@ - (void) testHexUploaderWrapper {
106107

107108
context->finalize();
108109

109-
uploadPublisher->publish(context);
110+
// uploadPublisher->publish(context);
110111

111112
XCTAssertNoThrow([mockWrapper verify]);
112113
}

libMobileAgent/src/Hex/include/Hex/HexPersistenceManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace NewRelic {
1919

2020
~HexPersistenceManager() = default;
2121

22-
std::shared_ptr<HexContext> retrieveStoreReports();
22+
void retrieveAndPublishReports();
2323

2424
void publishContext(std::shared_ptr<HexContext>const& context);
2525

libMobileAgent/src/Hex/include/Hex/HexReportContext.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
#define LIBMOBILEAGENT_HEXREPORTCONTEXT_HPP
88

99
#include <Hex/HexContext.hpp>
10+
#include <Hex/HexPublisher.hpp>
1011

1112
namespace NewRelic {
1213
namespace Hex {
13-
class HexReportContext : public HexContext {
14+
class HexReportContext : public HexContext, public std::enable_shared_from_this<HexReportContext> {
1415

1516
public:
1617
HexReportContext(const std::shared_ptr<Report::AppInfo>& applicationInfo,
17-
const AttributeValidator& attributeValidator);
18+
const AttributeValidator& attributeValidator,
19+
HexPublisher* publisher);
1820

1921
virtual void finalize();
2022

@@ -29,6 +31,7 @@ namespace NewRelic {
2931
std::vector<std::shared_ptr<Report::HexReport>> reportList;
3032
const AttributeValidator& _attributeValidator;
3133
const std::shared_ptr<Report::AppInfo>& _applicationInfo;
34+
HexPublisher* _publisher;
3235
};
3336
}
3437
}

libMobileAgent/src/Hex/src/HexController.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ HexController::HexController(std::shared_ptr<const AnalyticsController>& analyti
2020
_publisher(publisher),
2121
_store(store),
2222
_sessionId(sessionId),
23-
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator())) {
23+
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), publisher)) {
2424
}
2525

2626
HexController::HexController(std::shared_ptr<const AnalyticsController>&& analytics,
@@ -33,7 +33,7 @@ HexController::HexController(std::shared_ptr<const AnalyticsController>&& analyt
3333
_publisher(publisher),
3434
_store(store),
3535
_sessionId(sessionId),
36-
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator())) {
36+
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), publisher)) {
3737
}
3838

3939
// New Event System
@@ -78,20 +78,20 @@ std::shared_ptr<Report::HexReport> HexController::createReport(uint64_t epochMs,
7878
std::shared_ptr<HexReportContext> HexController::detachKeyContext() {
7979
std::unique_lock<std::mutex> detachLock(_keyContextMutex);
8080
auto context = _keyContext;
81-
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator());
81+
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), _publisher);
8282
return context;
8383
}
8484

8585
void HexController::resetKeyContext() {
8686
std::unique_lock<std::mutex> resetLock(_keyContextMutex);
87-
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator());
87+
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), _publisher);
8888
}
8989

9090
void HexController::publish() {
9191
auto context = detachKeyContext();
9292
if (context->reports() > 0) {
9393
context->finalize();
94-
_publisher->publish(context);
94+
// _publisher->publish(context);
9595
}
9696
}
9797

libMobileAgent/src/Hex/src/HexPersistenceManager.cxx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "HexPersistenceManager.hpp"
88
#include "hex-agent-data-bundle_generated.h"
99
#include "jserror_generated.h"
10+
#include <Utilities/libLogger.hpp>
1011

1112
using namespace NewRelic::Hex;
1213
using namespace com::newrelic::mobile;
@@ -17,32 +18,28 @@ HexPersistenceManager::HexPersistenceManager(std::shared_ptr<HexStore>& store,
1718

1819
}
1920

20-
std::shared_ptr<NewRelic::Hex::HexContext> NewRelic::Hex::HexPersistenceManager::retrieveStoreReports() {
21-
auto context = std::make_shared<HexContext>();
22-
std::vector<flatbuffers::Offset<fbs::HexAgentData>> agentDataVector;
23-
24-
25-
auto future = _store->readAll([&agentDataVector, &context](uint8_t* buf, std::size_t size) {
21+
void NewRelic::Hex::HexPersistenceManager::retrieveAndPublishReports() {
22+
auto future = _store->readAll([this](uint8_t* buf, std::size_t size) {
2623
auto verifier = flatbuffers::Verifier(buf, size);
27-
if(fbs::VerifyHexAgentDataBuffer(verifier)) {
24+
if (fbs::VerifyHexAgentDataBuffer(verifier)) {
2825
auto agentDataObj = UnPackHexAgentData(buf, nullptr);
29-
flatbuffers::Offset<HexAgentData> agentDataOffset = HexAgentData::Pack(*context->getBuilder(), agentDataObj.get(),
30-
nullptr);
31-
agentDataVector.push_back(agentDataOffset);
32-
}
33-
});
3426

35-
future.get();
27+
// Create a new context for each piece of agent data
28+
auto context = std::make_shared<HexContext>();
29+
flatbuffers::Offset<HexAgentData> agentDataOffset = HexAgentData::Pack(*context->getBuilder(), agentDataObj.get(), nullptr);
3630

37-
if (agentDataVector.empty()) {
38-
return nullptr;
39-
}
40-
auto bundle = fbs::CreateHexAgentDataBundle(*context->getBuilder(),
41-
context->getBuilder()->CreateVector(agentDataVector));
31+
Offset<Vector<Offset<HexAgentData>>> agentDataVector = context->getBuilder()->CreateVector(&agentDataOffset, 1);
32+
auto bundle = fbs::CreateHexAgentDataBundle(*context->getBuilder(), agentDataVector);
33+
FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);
4234

43-
FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);
35+
// Publish the context for this agent data
36+
if (context) {
37+
_publisher->publish(context);
38+
}
39+
}
40+
});
4441

45-
return context;
42+
future.get();
4643
}
4744

4845
void NewRelic::Hex::HexPersistenceManager::publishContext(std::shared_ptr<HexContext>const& context) {

libMobileAgent/src/Hex/src/HexReportContext.cxx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@ using namespace NewRelic::Hex;
1212
using namespace com::newrelic::mobile;
1313

1414
HexReportContext::HexReportContext(const std::shared_ptr<Report::AppInfo>& applicationInfo,
15-
const AttributeValidator& attributeValidator)
15+
const AttributeValidator& attributeValidator,
16+
HexPublisher* publisher)
1617
: HexContext::HexContext(),
1718
_attributeValidator(attributeValidator),
18-
_applicationInfo(applicationInfo) {}
19+
_applicationInfo(applicationInfo),
20+
_publisher(publisher){}
1921

2022
void HexReportContext::finalize() {
2123
std::unique_lock<std::mutex> finalizeLock(reportMutex);
22-
std::vector<flatbuffers::Offset<fbs::HexAgentData>> agentDataList;
24+
2325
for (auto& it : reportList) {
2426
try {
25-
agentDataList.push_back(it->finalize(*getBuilder()));
27+
auto agentDataOffset = it->finalize(*getBuilder());
28+
auto agentDataVector = getBuilder()->CreateVector(&agentDataOffset, 1);
29+
30+
auto bundle = fbs::CreateHexAgentDataBundle(*getBuilder(), agentDataVector);
31+
FinishHexAgentDataBundleBuffer(*getBuilder(), bundle);
32+
auto sharedThis = std::static_pointer_cast<HexContext>(shared_from_this());
33+
34+
_publisher->publish(sharedThis);
35+
36+
// Clear the builder for the next report
37+
getBuilder()->Clear();
2638
} catch (std::invalid_argument& e) {
2739
LLOG_AUDIT("Hex report not finalized: %s", e.what());
2840
} catch (...) {
2941
LLOG_AUDIT("Hex report not finalized:");
3042
}
3143
}
32-
getBuilder()->CreateVector(agentDataList);
33-
34-
auto bundle = fbs::CreateHexAgentDataBundle(*getBuilder(), getBuilder()->CreateVector(agentDataList));
35-
FinishHexAgentDataBundleBuffer(*getBuilder(), bundle);
36-
3744
}
3845

3946
std::shared_ptr<Report::HexReport> HexReportContext::createReport(std::shared_ptr<Report::HandledException> exception) {

0 commit comments

Comments
 (0)