From 0ef0274bc3927a121e25a067ddb05a00164f0450 Mon Sep 17 00:00:00 2001 From: Mike Bruin Date: Thu, 20 Feb 2025 11:37:33 -0500 Subject: [PATCH 1/2] NR-347591 added a synchronized lock to the deferredMetrics --- Agent/Measurements/NRMASupportMetricHelper.h | 2 - Agent/Measurements/NRMASupportMetricHelper.m | 91 ++++++++++---------- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Agent/Measurements/NRMASupportMetricHelper.h b/Agent/Measurements/NRMASupportMetricHelper.h index 9c31d8fc..d2457f3d 100644 --- a/Agent/Measurements/NRMASupportMetricHelper.h +++ b/Agent/Measurements/NRMASupportMetricHelper.h @@ -9,8 +9,6 @@ #import #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; diff --git a/Agent/Measurements/NRMASupportMetricHelper.m b/Agent/Measurements/NRMASupportMetricHelper.m index a2783370..7676fb6e 100644 --- a/Agent/Measurements/NRMASupportMetricHelper.m +++ b/Agent/Measurements/NRMASupportMetricHelper.m @@ -15,6 +15,12 @@ @implementation NRMASupportMetricHelper +static NSMutableArray *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]; @@ -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 { @@ -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 @@ -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 From bb0adafc9fe18cdc88e11e3b0023ed21634b55de Mon Sep 17 00:00:00 2001 From: Mike Bruin Date: Thu, 20 Feb 2025 11:58:16 -0500 Subject: [PATCH 2/2] Changes missed in the unit tests --- Agent/Measurements/NRMASupportMetricHelper.h | 2 ++ .../Uncategorized/NRMASupportMetricHelperTests.m | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Agent/Measurements/NRMASupportMetricHelper.h b/Agent/Measurements/NRMASupportMetricHelper.h index d2457f3d..1927d1b2 100644 --- a/Agent/Measurements/NRMASupportMetricHelper.h +++ b/Agent/Measurements/NRMASupportMetricHelper.h @@ -9,6 +9,8 @@ #import #import "NewRelicFeatureFlags.h" +static NSMutableArray *deferredMetrics; + @interface NRMASupportMetricHelper : NSObject + (void) enqueueDataUseMetric:(NSString*)subDestination size:(long)size received:(long)received; + (void) enqueueFeatureFlagMetric:(BOOL)enabled features:(NRMAFeatureFlags)features; diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRMASupportMetricHelperTests.m b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRMASupportMetricHelperTests.m index 6f0ab505..5e2ed106 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRMASupportMetricHelperTests.m +++ b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRMASupportMetricHelperTests.m @@ -27,7 +27,6 @@ - (void) setUp { if (deferredMetrics != nil) { [deferredMetrics removeAllObjects]; - deferredMetrics = nil; } helper = [[NRMAMeasurementConsumerHelper alloc] initWithType:NRMAMT_NamedValue]; [NRMAMeasurements initializeMeasurements];