Skip to content

Commit

Permalink
NR-347591 added a synchronized lock to the deferredMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Feb 20, 2025
1 parent bcc8ebf commit 0ef0274
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
2 changes: 0 additions & 2 deletions Agent/Measurements/NRMASupportMetricHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import <Foundation/Foundation.h>
#import "NewRelicFeatureFlags.h"

static NSMutableArray *deferredMetrics = NULL;

@interface NRMASupportMetricHelper : NSObject
+ (void) enqueueDataUseMetric:(NSString*)subDestination size:(long)size received:(long)received;
+ (void) enqueueFeatureFlagMetric:(BOOL)enabled features:(NRMAFeatureFlags)features;
Expand Down
91 changes: 45 additions & 46 deletions Agent/Measurements/NRMASupportMetricHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

@implementation NRMASupportMetricHelper

static NSMutableArray<NRMAMetric *> *deferredMetrics;

+ (void) initialize {
deferredMetrics = [NSMutableArray array];
}

// The value _additionalValue being non-nil means that this is a Data Usage Supportability Metric.
+ (void) enqueueDataUseMetric:(NSString*)subDestination size:(long)size received:(long)received {
NSString* nativePlatform = [NewRelicInternalUtils osName];
Expand All @@ -31,24 +37,22 @@ + (void) enqueueFeatureFlagMetric:(BOOL)enabled features:(NRMAFeatureFlags)featu
for (NSString *name in [NRMAFlags namesForFlags:features]) {
NSString* featureFlagString = [NSString stringWithFormat:@"Supportability/Mobile/%@/%@/API/%@/%@",
nativePlatform, kPlatformPlaceholder, enabled ? @"enableFeature" : @"disableFeature", name];
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
@synchronized (deferredMetrics) {
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:featureFlagString
value:[NSNumber numberWithLongLong:1]
scope:@""
produceUnscoped:YES
additionalValue:nil]];
}
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:featureFlagString
value:[NSNumber numberWithLongLong:1]
scope:@""
produceUnscoped:YES
additionalValue:nil]];
}
}

+ (void) enqueueInstallMetric {
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
@synchronized (deferredMetrics) {
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppInstallMetric
value:@1
scope:nil]];
}
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppInstallMetric
value:@1
scope:nil]];
}

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

+ (void) enqueueUpgradeMetric {
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
@synchronized (deferredMetrics) {
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppUpgradeMetric
value:@1
scope:nil]];
}
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:kNRMAAppUpgradeMetric
value:@1
scope:nil]];
}

+ (void) enqueueStopAgentMetric {
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
@synchronized (deferredMetrics) {
NSString* metricString = [NSString stringWithFormat:kNRMAStopAgentMetricFormatString, [NewRelicInternalUtils osName], kPlatformPlaceholder];
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
value:@1
scope:nil]];
}
NSString* metricString = [NSString stringWithFormat:kNRMAStopAgentMetricFormatString, [NewRelicInternalUtils osName], kPlatformPlaceholder];
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
value:@1
scope:nil]];
}

+ (void) enqueueConfigurationUpdateMetric {
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
@synchronized (deferredMetrics) {
NSString* metricString = [NSString stringWithFormat:kNRMAConfigurationUpdated, [NewRelicInternalUtils osName], kPlatformPlaceholder];
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
value:@1
scope:nil]];
}
NSString* metricString = [NSString stringWithFormat:kNRMAConfigurationUpdated, [NewRelicInternalUtils osName], kPlatformPlaceholder];
[deferredMetrics addObject:[[NRMAMetric alloc] initWithName:metricString
value:@1
scope:nil]];
}

// Logging
Expand Down Expand Up @@ -140,26 +141,24 @@ + (void) processDeferredMetrics {
}

// Handle any deferred supportability metrics.
if (deferredMetrics == nil) { return; }

for (NRMAMetric *metric in deferredMetrics) {

NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
NSString *deferredMetricName = metric.name;

if ([metric.name containsString:kPlatformPlaceholder]) {
deferredMetricName = [metric.name stringByReplacingOccurrencesOfString:kPlatformPlaceholder withString:platform];
@synchronized (deferredMetrics) {
for (NRMAMetric *metric in deferredMetrics) {
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
NSString *deferredMetricName = metric.name;

if ([metric.name containsString:kPlatformPlaceholder]) {
deferredMetricName = [metric.name stringByReplacingOccurrencesOfString:kPlatformPlaceholder withString:platform];
}

[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:deferredMetricName
value:[NSNumber numberWithLongLong:1]
scope:@""
produceUnscoped:YES
additionalValue:nil]];
}

[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:deferredMetricName
value:[NSNumber numberWithLongLong:1]
scope:@""
produceUnscoped:YES
additionalValue:nil]];
[deferredMetrics removeAllObjects];
}

[deferredMetrics removeAllObjects];
deferredMetrics = nil;
}

@end

0 comments on commit 0ef0274

Please sign in to comment.