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

Nr 350676 changing handled exceptions to send one per request #338

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 1 addition & 4 deletions Agent/HandledException/NRMAHandledExceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://en.cppreference.com/w/cpp/memory/enable_shared_from_this
The std::enable_shared_from_this is used to safely obtain a std::shared_ptr to *this. This is what enables the ability to cast it as a HexContext and send it to the publisher.

HexReportContext::finalize() uses the shared_from_this() function to create a std::shared_ptr cast as a HexContext to send to publish.


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
35 changes: 15 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,26 @@ 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));
auto agentDataVector = context->getBuilder()->CreateVector(&agentDataOffset, 1);
cdillard-NewRelic marked this conversation as resolved.
Show resolved Hide resolved
auto bundle = fbs::CreateHexAgentDataBundle(*context->getBuilder(), agentDataVector);
FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);

FinishHexAgentDataBundleBuffer(*context->getBuilder(), bundle);
// Publish the context for this agent data
_publisher->publish(context);
mbruin-NR marked this conversation as resolved.
Show resolved Hide resolved
}
});

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
Loading