Skip to content

Commit 1bbb9ef

Browse files
committed
add metric collection for unexpected C Quality attribute update
1 parent 63cf878 commit 1bbb9ef

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+68-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#import "MTRError_Internal.h"
3636
#import "MTREventTLVValueDecoder_Internal.h"
3737
#import "MTRLogging_Internal.h"
38+
#import "MTRMetricKeys.h"
39+
#import "MTRMetricsCollector.h"
3840
#import "MTRTimeUtils.h"
3941
#import "MTRUnfairLock.h"
4042
#import "zap-generated/MTRCommandPayloads_Internal.h"
@@ -91,6 +93,39 @@ - (id)strongObject
9193
}
9294
@end
9395

96+
// Stores essential-for-logging attributes immutably for use in logs
97+
@interface MTRDeviceEssentialAttributes : NSObject
98+
@property (readonly) UInt16 vendorID;
99+
@property (readonly) UInt16 productID;
100+
@property (readonly) BOOL usesThread;
101+
102+
- (void)addEssentialAttributesToCurrentMetricScope;
103+
104+
@end
105+
106+
@implementation MTRDeviceEssentialAttributes
107+
108+
- (instancetype)initWithVendorID:(UInt16)vendorID productID:(UInt16)productID usesThread:(BOOL)usesThread {
109+
self = [super init];
110+
111+
if (self) {
112+
_vendorID = vendorID;
113+
_productID = productID;
114+
_usesThread = usesThread;
115+
}
116+
117+
return self;
118+
}
119+
120+
- (void)addEssentialAttributesToCurrentMetricScope {
121+
using namespace chip::Tracing::DarwinFramework;
122+
MATTER_LOG_METRIC(kMetricDeviceVendorID, _vendorID);
123+
MATTER_LOG_METRIC(kMetricDeviceProductID, _productID);
124+
MATTER_LOG_METRIC(kMetricDeviceUsesThread, _usesThread);
125+
}
126+
127+
@end
128+
94129
NSNumber * MTRClampedNumber(NSNumber * aNumber, NSNumber * min, NSNumber * max)
95130
{
96131
if ([aNumber compare:min] == NSOrderedAscending) {
@@ -319,6 +354,10 @@ @interface MTRDevice ()
319354

320355
@property (nonatomic) MTRInternalDeviceState internalDeviceState;
321356

357+
// TODO: cache this once I understand the point in the MTRDevice lifecycle that the relevant attributes will be present.
358+
// kmo 22 may 2024 14h55
359+
// @property (nonatomic) MTRDeviceEssentialAttributes * essentialAttributes;
360+
322361
#define MTRDEVICE_SUBSCRIPTION_ATTEMPT_MIN_WAIT_SECONDS (1)
323362
#define MTRDEVICE_SUBSCRIPTION_ATTEMPT_MAX_WAIT_SECONDS (3600)
324363
@property (nonatomic) uint32_t lastSubscriptionAttemptWait;
@@ -1695,6 +1734,8 @@ - (void)_handleEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventRepor
16951734
BOOL isStartUpEvent = (eventPath.cluster.unsignedLongValue == MTRClusterIDTypeBasicInformationID)
16961735
&& (eventPath.event.unsignedLongValue == MTREventIDTypeClusterBasicInformationEventStartUpID);
16971736
if (isStartUpEvent) {
1737+
// REVIEWERS: this seems like a good place to set up / cache
1738+
// the essential device attributes - is it?
16981739
if (_estimatedStartTimeFromGeneralDiagnosticsUpTime) {
16991740
// If UpTime was received, make use of it as mark of system start time
17001741
MTR_LOG("%@ StartUp event: set estimated start time forward to %@", self,
@@ -1866,9 +1907,18 @@ - (void)_setCachedAttributeValue:(MTRDeviceDataValueDictionary _Nullable)value f
18661907
&& isFromSubscription
18671908
&& !_receivingPrimingReport
18681909
&& AttributeHasChangesOmittedQuality(path)) {
1869-
// Do not persist new values for Changes Omitted Quality attributes unless
1870-
// they're part of a Priming Report or from a read response.
1910+
// Do not persist new values for Changes Omitted Quality (aka C Quality)
1911+
// attributes unless they're part of a Priming Report or from a read response.
18711912
// (removals are OK)
1913+
1914+
// log when a device violates expectations for Changes Omitted Quality attributes.
1915+
MTRDeviceEssentialAttributes * attributes = [self _essentialAttributesForCurrentState];
1916+
1917+
using namespace chip::Tracing::DarwinFramework;
1918+
MATTER_LOG_METRIC_BEGIN(kMetricUnexpectedCQualityUpdate);
1919+
[attributes addEssentialAttributesToCurrentMetricScope];
1920+
MATTER_LOG_METRIC_END(kMetricUnexpectedCQualityUpdate);
1921+
18721922
return;
18731923
}
18741924

@@ -3521,6 +3571,22 @@ - (void)removeClientDataForKey:(NSString *)key endpointID:(NSNumber *)endpointID
35213571
[self.temporaryMetaDataCache removeObjectForKey:[NSString stringWithFormat:@"%@:%@", key, endpointID]];
35223572
}
35233573

3574+
#pragma mark Log Help
3575+
3576+
- (MTRDeviceEssentialAttributes *)_essentialAttributesForCurrentState {
3577+
MTRClusterPath * basicInfoClusterPath = [MTRClusterPath clusterPathWithEndpointID:@(kRootEndpointId) clusterID:@(MTRClusterIDTypeBasicInformationID)];
3578+
MTRDeviceClusterData * basicInfoClusterData = [self _clusterDataForPath:basicInfoClusterPath];
3579+
3580+
NSNumber * vidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeVendorIDID)][MTRValueKey];
3581+
UInt16 vendorID = vidObj.unsignedShortValue;
3582+
NSNumber * pidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeProductIDID)][MTRValueKey];
3583+
UInt16 productID = pidObj.unsignedShortValue;
3584+
3585+
BOOL usesThread = [self _deviceUsesThread];
3586+
3587+
return [[MTRDeviceEssentialAttributes alloc] initWithVendorID:vendorID productID:productID usesThread:usesThread];
3588+
}
3589+
35243590
@end
35253591

35263592
/* BEGIN DRAGONS: Note methods here cannot be renamed, and are used by private callers, do not rename, remove or modify behavior here */

0 commit comments

Comments
 (0)