Skip to content

Commit 0ef0274

Browse files
committed
NR-347591 added a synchronized lock to the deferredMetrics
1 parent bcc8ebf commit 0ef0274

File tree

2 files changed

+45
-48
lines changed

2 files changed

+45
-48
lines changed

Agent/Measurements/NRMASupportMetricHelper.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#import <Foundation/Foundation.h>
1010
#import "NewRelicFeatureFlags.h"
1111

12-
static NSMutableArray *deferredMetrics = NULL;
13-
1412
@interface NRMASupportMetricHelper : NSObject
1513
+ (void) enqueueDataUseMetric:(NSString*)subDestination size:(long)size received:(long)received;
1614
+ (void) enqueueFeatureFlagMetric:(BOOL)enabled features:(NRMAFeatureFlags)features;

Agent/Measurements/NRMASupportMetricHelper.m

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
@implementation NRMASupportMetricHelper
1717

18+
static NSMutableArray<NRMAMetric *> *deferredMetrics;
19+
20+
+ (void) initialize {
21+
deferredMetrics = [NSMutableArray array];
22+
}
23+
1824
// The value _additionalValue being non-nil means that this is a Data Usage Supportability Metric.
1925
+ (void) enqueueDataUseMetric:(NSString*)subDestination size:(long)size received:(long)received {
2026
NSString* nativePlatform = [NewRelicInternalUtils osName];
@@ -31,24 +37,22 @@ + (void) enqueueFeatureFlagMetric:(BOOL)enabled features:(NRMAFeatureFlags)featu
3137
for (NSString *name in [NRMAFlags namesForFlags:features]) {
3238
NSString* featureFlagString = [NSString stringWithFormat:@"Supportability/Mobile/%@/%@/API/%@/%@",
3339
nativePlatform, kPlatformPlaceholder, enabled ? @"enableFeature" : @"disableFeature", name];
34-
if (deferredMetrics == nil) {
35-
deferredMetrics = [NSMutableArray array];
40+
@synchronized (deferredMetrics) {
41+
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:featureFlagString
42+
value:[NSNumber numberWithLongLong:1]
43+
scope:@""
44+
produceUnscoped:YES
45+
additionalValue:nil]];
3646
}
37-
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:featureFlagString
38-
value:[NSNumber numberWithLongLong:1]
39-
scope:@""
40-
produceUnscoped:YES
41-
additionalValue:nil]];
4247
}
4348
}
4449

4550
+ (void) enqueueInstallMetric {
46-
if (deferredMetrics == nil) {
47-
deferredMetrics = [NSMutableArray array];
51+
@synchronized (deferredMetrics) {
52+
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppInstallMetric
53+
value:@1
54+
scope:nil]];
4855
}
49-
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppInstallMetric
50-
value:@1
51-
scope:nil]];
5256
}
5357

5458
+ (void) enqueueMaxPayloadSizeLimitMetric:(NSString*)endpoint {
@@ -68,32 +72,29 @@ + (void) enqueueOfflinePayloadMetric:(long)size {
6872
}
6973

7074
+ (void) enqueueUpgradeMetric {
71-
if (deferredMetrics == nil) {
72-
deferredMetrics = [NSMutableArray array];
75+
@synchronized (deferredMetrics) {
76+
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppUpgradeMetric
77+
value:@1
78+
scope:nil]];
7379
}
74-
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppUpgradeMetric
75-
value:@1
76-
scope:nil]];
7780
}
7881

7982
+ (void) enqueueStopAgentMetric {
80-
if (deferredMetrics == nil) {
81-
deferredMetrics = [NSMutableArray array];
83+
@synchronized (deferredMetrics) {
84+
NSString* metricString = [NSString stringWithFormat:kNRMAStopAgentMetricFormatString, [NewRelicInternalUtils osName], kPlatformPlaceholder];
85+
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
86+
value:@1
87+
scope:nil]];
8288
}
83-
NSString* metricString = [NSString stringWithFormat:kNRMAStopAgentMetricFormatString, [NewRelicInternalUtils osName], kPlatformPlaceholder];
84-
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
85-
value:@1
86-
scope:nil]];
8789
}
8890

8991
+ (void) enqueueConfigurationUpdateMetric {
90-
if (deferredMetrics == nil) {
91-
deferredMetrics = [NSMutableArray array];
92+
@synchronized (deferredMetrics) {
93+
NSString* metricString = [NSString stringWithFormat:kNRMAConfigurationUpdated, [NewRelicInternalUtils osName], kPlatformPlaceholder];
94+
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
95+
value:@1
96+
scope:nil]];
9297
}
93-
NSString* metricString = [NSString stringWithFormat:kNRMAConfigurationUpdated, [NewRelicInternalUtils osName], kPlatformPlaceholder];
94-
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
95-
value:@1
96-
scope:nil]];
9798
}
9899

99100
// Logging
@@ -140,26 +141,24 @@ + (void) processDeferredMetrics {
140141
}
141142

142143
// Handle any deferred supportability metrics.
143-
if (deferredMetrics == nil) { return; }
144-
145-
for (NRMAMetric *metric in deferredMetrics) {
146-
147-
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
148-
NSString *deferredMetricName = metric.name;
149-
150-
if ([metric.name containsString:kPlatformPlaceholder]) {
151-
deferredMetricName = [metric.name stringByReplacingOccurrencesOfString:kPlatformPlaceholder withString:platform];
144+
@synchronized (deferredMetrics) {
145+
for (NRMAMetric *metric in deferredMetrics) {
146+
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
147+
NSString *deferredMetricName = metric.name;
148+
149+
if ([metric.name containsString:kPlatformPlaceholder]) {
150+
deferredMetricName = [metric.name stringByReplacingOccurrencesOfString:kPlatformPlaceholder withString:platform];
151+
}
152+
153+
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:deferredMetricName
154+
value:[NSNumber numberWithLongLong:1]
155+
scope:@""
156+
produceUnscoped:YES
157+
additionalValue:nil]];
152158
}
153159

154-
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:deferredMetricName
155-
value:[NSNumber numberWithLongLong:1]
156-
scope:@""
157-
produceUnscoped:YES
158-
additionalValue:nil]];
160+
[deferredMetrics removeAllObjects];
159161
}
160-
161-
[deferredMetrics removeAllObjects];
162-
deferredMetrics = nil;
163162
}
164163

165164
@end

0 commit comments

Comments
 (0)