Skip to content

Commit

Permalink
Merge branch 'develop' into NR-364636
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR authored Feb 5, 2025
2 parents 6beaf72 + 472e2a8 commit 4cd8239
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 48 deletions.
5 changes: 1 addition & 4 deletions Agent/HandledException/NRMAHandledExceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ - (void) recordHandledException:(NSException*)exception {

- (void) processAndPublishPersistedReports {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
auto context = _persistenceManager->retrieveStoreReports();
if (context) {
_publisher->publish(context);
}
_persistenceManager->retrieveAndPublishReports();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ - (void)setUp {
auto appLicense = std::make_shared<NewRelic::Hex::Report::ApplicationLicense>("ABCDEF12345");
applicationInfo = std::make_shared<NewRelic::Hex::Report::AppInfo>(appLicense.get(),fbs::Platform_iOS);
attributeValidator = new NewRelic::AttributeValidator([](const char*) {return true;},[](const char*) {return true;},[](const char*) {return true;});
context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator);
auto uploadPublisher = new NewRelic::Hex::HexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator,uploadPublisher);
sessionId = std::string("ABSDFWERQWE");
e = std::make_shared<NewRelic::Hex::Report::HandledException>(sessionId,
1,
Expand All @@ -70,14 +71,14 @@ - (void)testHexUploaderCreation {
auto appLicense = std::make_shared<NewRelic::Hex::Report::ApplicationLicense>("ABCDEF12345");
std::shared_ptr<NewRelic::Hex::Report::AppInfo> applicationInfo = std::make_shared<NewRelic::Hex::Report::AppInfo>(appLicense.get(),fbs::Platform_iOS);
NewRelic::AttributeValidator* attributeValidator = new NewRelic::AttributeValidator([](const char*) {return true;},[](const char*) {return true;},[](const char*) {return true;});
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator);
auto uploadPublisher = new NewRelic::Hex::HexUploadPublisher(".","AppToken", "1.0", "staging-mobile-collector.staging.com");
std::shared_ptr<NewRelic::Hex::HexReportContext> context = std::make_shared<NewRelic::Hex::HexReportContext>(applicationInfo,*attributeValidator,uploadPublisher);
std::shared_ptr<NewRelic::Hex::Report::HandledException> exception = std::make_shared<NewRelic::Hex::Report::HandledException>("sessionId",
1,
"The tea is too hot.",
"HotTeaException",
std::vector<std::shared_ptr<NewRelic::Hex::Report::Thread>>());

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

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

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

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

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

context->finalize();

uploadPublisher->publish(context);
// uploadPublisher->publish(context);

XCTAssertNoThrow([mockWrapper verify]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace NewRelic {

~HexPersistenceManager() = default;

std::shared_ptr<HexContext> retrieveStoreReports();
void retrieveAndPublishReports();

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

Expand Down
7 changes: 5 additions & 2 deletions libMobileAgent/src/Hex/include/Hex/HexReportContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
#define LIBMOBILEAGENT_HEXREPORTCONTEXT_HPP

#include <Hex/HexContext.hpp>
#include <Hex/HexPublisher.hpp>

namespace NewRelic {
namespace Hex {
class HexReportContext : public HexContext {
class HexReportContext : public HexContext, public std::enable_shared_from_this<HexReportContext> {

public:
HexReportContext(const std::shared_ptr<Report::AppInfo>& applicationInfo,
const AttributeValidator& attributeValidator);
const AttributeValidator& attributeValidator,
HexPublisher* publisher);

virtual void finalize();

Expand All @@ -29,6 +31,7 @@ namespace NewRelic {
std::vector<std::shared_ptr<Report::HexReport>> reportList;
const AttributeValidator& _attributeValidator;
const std::shared_ptr<Report::AppInfo>& _applicationInfo;
HexPublisher* _publisher;
};
}
}
Expand Down
10 changes: 5 additions & 5 deletions libMobileAgent/src/Hex/src/HexController.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ HexController::HexController(std::shared_ptr<const AnalyticsController>& analyti
_publisher(publisher),
_store(store),
_sessionId(sessionId),
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator())) {
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), publisher)) {
}

HexController::HexController(std::shared_ptr<const AnalyticsController>&& analytics,
Expand All @@ -33,7 +33,7 @@ HexController::HexController(std::shared_ptr<const AnalyticsController>&& analyt
_publisher(publisher),
_store(store),
_sessionId(sessionId),
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator())) {
_keyContext(std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), publisher)) {
}

// New Event System
Expand Down Expand Up @@ -78,20 +78,20 @@ std::shared_ptr<Report::HexReport> HexController::createReport(uint64_t epochMs,
std::shared_ptr<HexReportContext> HexController::detachKeyContext() {
std::unique_lock<std::mutex> detachLock(_keyContextMutex);
auto context = _keyContext;
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator());
_keyContext = std::make_shared<HexReportContext>(_applicationInfo, _analytics->getAttributeValidator(), _publisher);
return context;
}

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

void HexController::publish() {
auto context = detachKeyContext();
if (context->reports() > 0) {
context->finalize();
_publisher->publish(context);
// _publisher->publish(context);
}
}

Expand Down
37 changes: 17 additions & 20 deletions libMobileAgent/src/Hex/src/HexPersistenceManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "HexPersistenceManager.hpp"
#include "hex-agent-data-bundle_generated.h"
#include "jserror_generated.h"
#include <Utilities/libLogger.hpp>

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

}

std::shared_ptr<NewRelic::Hex::HexContext> NewRelic::Hex::HexPersistenceManager::retrieveStoreReports() {
auto context = std::make_shared<HexContext>();
std::vector<flatbuffers::Offset<fbs::HexAgentData>> agentDataVector;


auto future = _store->readAll([&agentDataVector, &context](uint8_t* buf, std::size_t size) {
void NewRelic::Hex::HexPersistenceManager::retrieveAndPublishReports() {
auto future = _store->readAll([this](uint8_t* buf, std::size_t size) {
auto verifier = flatbuffers::Verifier(buf, size);
if(fbs::VerifyHexAgentDataBuffer(verifier)) {
if (fbs::VerifyHexAgentDataBuffer(verifier)) {
auto agentDataObj = UnPackHexAgentData(buf, nullptr);
flatbuffers::Offset<HexAgentData> agentDataOffset = HexAgentData::Pack(*context->getBuilder(), agentDataObj.get(),
nullptr);
agentDataVector.push_back(agentDataOffset);
}
});

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

if (agentDataVector.empty()) {
return nullptr;
}
auto bundle = fbs::CreateHexAgentDataBundle(*context->getBuilder(),
context->getBuilder()->CreateVector(agentDataVector));
Offset<Vector<Offset<HexAgentData>>> agentDataVector = context->getBuilder()->CreateVector(&agentDataOffset, 1);
auto bundle = fbs::CreateHexAgentDataBundle(*context->getBuilder(), agentDataVector);
FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);

FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);
// Publish the context for this agent data
if (context) {
_publisher->publish(context);
}
}
});

return context;
future.get();
}

void NewRelic::Hex::HexPersistenceManager::publishContext(std::shared_ptr<HexContext>const& context) {
Expand Down
25 changes: 16 additions & 9 deletions libMobileAgent/src/Hex/src/HexReportContext.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@ using namespace NewRelic::Hex;
using namespace com::newrelic::mobile;

HexReportContext::HexReportContext(const std::shared_ptr<Report::AppInfo>& applicationInfo,
const AttributeValidator& attributeValidator)
const AttributeValidator& attributeValidator,
HexPublisher* publisher)
: HexContext::HexContext(),
_attributeValidator(attributeValidator),
_applicationInfo(applicationInfo) {}
_applicationInfo(applicationInfo),
_publisher(publisher){}

void HexReportContext::finalize() {
std::unique_lock<std::mutex> finalizeLock(reportMutex);
std::vector<flatbuffers::Offset<fbs::HexAgentData>> agentDataList;

for (auto& it : reportList) {
try {
agentDataList.push_back(it->finalize(*getBuilder()));
auto agentDataOffset = it->finalize(*getBuilder());
auto agentDataVector = getBuilder()->CreateVector(&agentDataOffset, 1);

auto bundle = fbs::CreateHexAgentDataBundle(*getBuilder(), agentDataVector);
FinishHexAgentDataBundleBuffer(*getBuilder(), bundle);
auto sharedThis = std::static_pointer_cast<HexContext>(shared_from_this());

_publisher->publish(sharedThis);

// Clear the builder for the next report
getBuilder()->Clear();
} catch (std::invalid_argument& e) {
LLOG_AUDIT("Hex report not finalized: %s", e.what());
} catch (...) {
LLOG_AUDIT("Hex report not finalized:");
}
}
getBuilder()->CreateVector(agentDataList);

auto bundle = fbs::CreateHexAgentDataBundle(*getBuilder(), getBuilder()->CreateVector(agentDataList));
FinishHexAgentDataBundleBuffer(*getBuilder(), bundle);

}

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

0 comments on commit 4cd8239

Please sign in to comment.