Skip to content

Commit 47ff808

Browse files
committed
NR-359870 moved the attribute validator to utils so it can be static and used anywhere it's needed
1 parent 9b5f166 commit 47ff808

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

Agent/Analytics/NRMAAnalytics.mm

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -116,59 +116,9 @@ - (id) initWithSessionStartTimeMS:(long long) sessionStartTime {
116116
PersistentEventStore *eventStore = [[PersistentEventStore alloc] initWithFilename:filename andMinimumDelay:.025];
117117

118118
_eventManager = [[NRMAEventManager alloc] initWithPersistentStore:eventStore];
119-
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
120-
if ([name length] == 0) {
121-
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
122-
return false;
123-
}
124-
if ([name hasPrefix:@" "]) {
125-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
126-
return false;
127-
}
128-
// check if attribute name is reserved or attribute name matches reserved prefix.
129-
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
130-
if ([key isEqualToString:name]) {
131-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
132-
return false;
133-
}
134-
}
135-
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
136-
if ([name hasPrefix:key]) {
137-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
138-
return false;
139-
}
140-
}
141-
142-
// check if attribute name exceeds max length.
143-
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
144-
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
145-
return false;
146-
}
147-
return true;
148-
149-
} valueValidator:^BOOL(id value) {
150-
if ([value isKindOfClass:[NSString class]]) {
151-
if ([(NSString*)value length] == 0) {
152-
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
153-
return false;
154-
}
155-
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
156-
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
157-
return false;
158-
}
159-
}
160-
if (value == nil || [value isKindOfClass:[NSNull class]]) {
161-
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
162-
return false;
163-
}
164-
165-
return true;
166-
} andEventTypeValidator:^BOOL(NSString *eventType) {
167-
return YES;
168-
}];
119+
_attributeValidator = [NewRelicInternalUtils attributeValidator];
169120
_sessionAttributeManager = [[NRMASAM alloc] initWithAttributeValidator:_attributeValidator];
170121

171-
172122
NSString* attributes = [self sessionAttributeJSONString];
173123
if (attributes != nil && [attributes length] > 0) {
174124
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding]
@@ -257,7 +207,6 @@ - (void) dealloc {
257207
[__eventTypeRegex release];
258208
[_eventManager dealloc];
259209
[_sessionAttributeManager dealloc];
260-
[_attributeValidator release];
261210
[_sessionStartTime release];
262211

263212
[super dealloc];

Agent/HandledException/NRMAHandledExceptions.mm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ - (void) recordError:(NSError * _Nonnull)error
198198
resultMap,
199199
[self createThreadVector:callstack length:frames]
200200
);
201-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
201+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
202202

203203
if (attributes != nil) {
204204
[contextAdapter addAttributesNewValidation:attributes];
@@ -218,7 +218,7 @@ - (void) recordError:(NSError * _Nonnull)error
218218
[self createThreadVector:callstack length:frames]
219219
);
220220

221-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
221+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
222222

223223
if (attributes != nil) {
224224
[contextAdapter addAttributes:attributes];
@@ -268,7 +268,7 @@ - (void) recordHandledException:(NSException*)exception
268268

269269
[self checkOffline:report];
270270

271-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
271+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
272272

273273
if (attributes != nil) {
274274
[contextAdapter addAttributesNewValidation:attributes];
@@ -287,7 +287,7 @@ - (void) recordHandledException:(NSException*)exception
287287

288288
[self checkOffline:report];
289289

290-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
290+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
291291

292292
if (attributes != nil) {
293293
[contextAdapter addAttributes:attributes];
@@ -371,7 +371,7 @@ - (void) recordHandledExceptionWithStackTrace:(NSDictionary*)exceptionDictionary
371371
report->setAttributeNoValidation("timeSinceLoad", [[[NSDate new] autorelease] timeIntervalSinceDate:self.sessionStartDate]);
372372
[self checkOffline:report];
373373

374-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
374+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
375375

376376
if (exceptionDictionary != nil) {
377377
[contextAdapter addAttributesNewValidation:exceptionDictionary];
@@ -388,7 +388,7 @@ - (void) recordHandledExceptionWithStackTrace:(NSDictionary*)exceptionDictionary
388388
report->setAttribute("timeSinceLoad", [[[NSDate new] autorelease] timeIntervalSinceDate:self.sessionStartDate]);
389389
[self checkOffline:report];
390390

391-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
391+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
392392

393393
if (exceptionDictionary != nil) {
394394
[contextAdapter addAttributes:exceptionDictionary];

Agent/Utilities/NewRelicInternalUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "NRMAReachability.h"
1010
#import "NRConstants.h"
1111
#import "NRMANetworkMonitor.h"
12+
#import "BlockAttributeValidator.h"
1213

1314
#if __LP64__
1415
#define NRMA_NSI "ld"
@@ -104,5 +105,7 @@ NSTimeInterval NRMAMillisecondTimestamp(void);
104105

105106
+ (NRMAReachability*) reachability;
106107

108+
+ (id<AttributeValidatorProtocol>) attributeValidator;
109+
107110
@end
108111

Agent/Utilities/NewRelicInternalUtils.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,64 @@ + (BOOL) isSimulator {
689689
return simulator != nil;
690690
}
691691

692+
static id<AttributeValidatorProtocol> _attributeValidator;
693+
+ (id<AttributeValidatorProtocol>) attributeValidator
694+
{
695+
static dispatch_once_t onceToken;
696+
dispatch_once(&onceToken, ^{
697+
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
698+
if ([name length] == 0) {
699+
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
700+
return false;
701+
}
702+
if ([name hasPrefix:@" "]) {
703+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
704+
return false;
705+
}
706+
// check if attribute name is reserved or attribute name matches reserved prefix.
707+
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
708+
if ([key isEqualToString:name]) {
709+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
710+
return false;
711+
}
712+
}
713+
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
714+
if ([name hasPrefix:key]) {
715+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
716+
return false;
717+
}
718+
}
719+
720+
// check if attribute name exceeds max length.
721+
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
722+
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
723+
return false;
724+
}
725+
return true;
726+
727+
} valueValidator:^BOOL(id value) {
728+
if ([value isKindOfClass:[NSString class]]) {
729+
if ([(NSString*)value length] == 0) {
730+
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
731+
return false;
732+
}
733+
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
734+
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
735+
return false;
736+
}
737+
}
738+
if (value == nil || [value isKindOfClass:[NSNull class]]) {
739+
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
740+
return false;
741+
}
742+
743+
return true;
744+
} andEventTypeValidator:^BOOL(NSString *eventType) {
745+
return YES;
746+
}];
747+
});
748+
749+
return _attributeValidator;
750+
}
751+
692752
@end

0 commit comments

Comments
 (0)