Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework for emitting metrics data #32223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7678be9
Rerouted tracing macros to Darwin signposts
anush-apple Feb 9, 2024
8e20096
Initial framework for logging scalar data event
anush-apple Feb 13, 2024
69c839f
Handled the new metrics event changes in collector
anush-apple Feb 17, 2024
3d9a940
Modified VerifyOrExit macro to accept an optional metric key as third…
anush-apple Feb 17, 2024
d37aab1
Removed direct use of chrono in metrics_event.h
anush-apple Feb 18, 2024
960a86e
Modified SuccessOrExit to optionally accept metric key
anush-apple Feb 18, 2024
cd9bc86
Moved metric keys to separate header
anush-apple Feb 19, 2024
30aeb07
Switched MATTER_TRACE_METRIC usage to MATTER_LOG_METRIC
anush-apple Feb 20, 2024
d188f63
Restyle fixes
anush-apple Feb 20, 2024
a7177e6
Fixed unit tests
anush-apple Feb 20, 2024
8219ee6
Fixed build failure due to MetricEvent hidden inside tracing enabled
anush-apple Feb 20, 2024
a692f1c
Fixing one source of build error
anush-apple Feb 21, 2024
0bce632
Fixing darwin build failure
anush-apple Feb 21, 2024
a4a892a
Code Review: Rename LogMetric to LogMetricEvent
anush-apple Feb 21, 2024
ad11da8
Code Review Suggestions:
anush-apple Feb 22, 2024
39b9544
Code Review Feedback #2:
anush-apple Feb 22, 2024
db7f81b
Code Review Feedback #3:
anush-apple Feb 22, 2024
ddccc46
Code Review #4:
anush-apple Feb 22, 2024
5e2e29d
Added MTRMetricData to description as per review comment
anush-apple Feb 22, 2024
76b0fd5
Restyler fixes
anush-apple Feb 22, 2024
a568e8d
Sample code of how Begin and End log metrics can be used
anush-apple Feb 22, 2024
b39d1f1
Fixed compilation error when tracing is disabled
anush-apple Feb 22, 2024
e1cd4d2
Fixes for build failures when tracing is disabled
anush-apple Feb 23, 2024
9e0c64d
Picked up code review suggestion accidently dropped
anush-apple Feb 23, 2024
13103b8
Code Review Feedback:
anush-apple Feb 23, 2024
3dcda78
Handle undefined value and error value
anush-apple Feb 23, 2024
3f96ebf
Revert a comment change
anush-apple Feb 23, 2024
c041601
Review Feedback: Changed ScopedMetricEvent to capture error by reference
anush-apple Feb 23, 2024
f0c2061
Fixed another build failure
anush-apple Feb 23, 2024
764a998
Reverting usage of LOG_METRICS
anush-apple Feb 23, 2024
8c651cf
Review feedback: Fix incorrect documentation
anush-apple Feb 23, 2024
ecdb5bd
Code Review Feedback: Remove access to Value in MetricEvent to avoid …
anush-apple Feb 23, 2024
cbdbffa
Restyler fixes
anush-apple Feb 23, 2024
5759bb1
Unregistering backend in Darwin shutdown
anush-apple Feb 24, 2024
7c0c093
Resytler fixes...
anush-apple Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <lib/core/Optional.h>
#include <platform/DiagnosticDataProvider.h>
#include <tracing/macros.h>
#include <tracing/metric_event.h>
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

using namespace chip;
using namespace chip::app;
Expand Down Expand Up @@ -163,7 +164,7 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiRssi(AttributeValueEncoder & aEncod
{
rssi.SetNonNull(value);
ChipLogProgress(Zcl, "The current RSSI of the Node’s Wi-Fi radio in dB: %d", value);
MATTER_TRACE_METRIC("wifi_rssi", value);
MATTER_LOG_METRIC(chip::Tracing::kMetricWiFiRSSI, value);
}
else
{
Expand Down
14 changes: 5 additions & 9 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

#import "MTRDeviceControllerDelegateBridge.h"
#import "MTRDeviceController.h"
#import "MTRDeviceController_Internal.h"
#import "MTRError_Internal.h"
#import "MTRLogging_Internal.h"
#import "MTRMetrics_Internal.h"
#import "MTRMetricsCollector.h"

MTRDeviceControllerDelegateBridge::MTRDeviceControllerDelegateBridge(void)
: mDelegate(nil)
Expand Down Expand Up @@ -130,15 +131,10 @@
nodeID = @(nodeId);
}

// If the client implements the metrics delegate, prefer that over others
if ([strongDelegate respondsToSelector:@selector(controller:commissioningComplete:nodeID:metrics:)]) {
MTRMetrics * metrics = [MTRMetrics new];

if (nsError) {
[metrics setValue:nsError forKey:MTRMetricCommissioningStatusKey];
} else {
auto * error = [NSError errorWithDomain:MTRErrorDomain code:0 userInfo:nil];
[metrics setValue:error forKey:MTRMetricCommissioningStatusKey];
}
// Create a snapshot and clear for next operation
MTRMetrics * metrics = [[MTRMetricsCollector sharedInstance] metricSnapshot:TRUE];
[strongDelegate controller:strongController commissioningComplete:nsError nodeID:nodeID metrics:metrics];
} else {
[strongDelegate controller:strongController commissioningComplete:nsError nodeID:nodeID];
Expand Down
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#import "MTRFabricInfo_Internal.h"
#import "MTRFramework.h"
#import "MTRLogging_Internal.h"
#import "MTRMetricsCollector.h"
#import "MTROTAProviderDelegateBridge.h"
#import "MTROperationalBrowser.h"
#import "MTRP256KeypairBridge.h"
Expand Down Expand Up @@ -349,6 +350,8 @@ - (void)cleanupStartupObjects
}

_diagnosticLogsDownloader = nil;

ShutdownMetricsCollection();
}

- (CHIP_ERROR)_initFabricTable:(FabricTable &)fabricTable
Expand Down Expand Up @@ -414,6 +417,10 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
return YES;
}

// Register any tracing backends. This has to be done before starting the event loop to run registering
// the tracing backend in the right queue context
StartupMetricsCollection();

DeviceLayer::PlatformMgrImpl().StartEventLoopTask();

__block CHIP_ERROR errorCode = CHIP_NO_ERROR;
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRFramework.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "MTRFramework.h"
#import "MTRMetricsCollector.h"

#include <dispatch/dispatch.h>
#include <lib/support/CHIPMem.h>
Expand All @@ -34,5 +35,8 @@ void MTRFrameworkInit()
// Suppress CHIP logging until we actually need it for redirection
// (see MTRSetLogCallback()). Logging to os_log is always enabled.
chip::Logging::SetLogFilter(chip::Logging::kLogCategory_None);

// Startup metrics collection and tracing framework
StartupMetricsCollection();
});
}
16 changes: 15 additions & 1 deletion src/darwin/Framework/CHIP/MTRMetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,28 @@
NS_ASSUME_NONNULL_BEGIN

/**
* A representation of metrics data for an operation.
* A representation of a collection of metrics data for an operation.
*/
MTR_NEWLY_AVAILABLE
@interface MTRMetrics : NSObject

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

/**
* @brief Returns the names of all the metrics data items collected.
*/
@property (nonatomic, readonly, copy) NSArray<NSString *> * allKeys;

/**
* @brief Returns metric object corresponding to the metric identified by its key
*
* @param [in] key Name of the metric
*
* @return An object containing the metric data, nil if key is invalid
*/
- (nullable id)valueForKey:(NSString *)key;

@end

NS_ASSUME_NONNULL_END
10 changes: 9 additions & 1 deletion src/darwin/Framework/CHIP/MTRMetrics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
*/
#import "MTRLogging_Internal.h"
#import "MTRMetrics_Internal.h"
#include <Foundation/Foundation.h>
#import <Matter/MTRDefines.h>
#include <Matter/MTRMetrics.h>

@implementation MTRMetrics {
NSMutableDictionary<NSString *, id> * _metricsData;
}

- (instancetype)init
{
NSAssert(false, @"'init' unavailable, use initWithCapacity: instead");
return nil;
}

- (instancetype)initWithCapacity:(NSUInteger)numItems
{
if (self = [super init]) {
_metricsData = [NSMutableDictionary dictionary];
_metricsData = [NSMutableDictionary dictionaryWithCapacity:numItems];
}
return self;
}
Expand Down
59 changes: 59 additions & 0 deletions src/darwin/Framework/CHIP/MTRMetricsCollector.h
anush-apple marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <MTRDefines.h>
#import <Matter/MTRMetrics.h>

NS_ASSUME_NONNULL_BEGIN

/**
* This function initializes any backend required to collect metrics data.
*/
void StartupMetricsCollection();

/**
* This function shuts down any backend created to collect metrics data.
*/
void ShutdownMetricsCollection();

/**
* A representation of metrics data for an operation.
*/
@interface MTRMetricsCollector : NSObject

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

/**
* Return the singleton MTRMetricsCollector to vend MTRMetrics snapshots
*/
+ (instancetype)sharedInstance;

/**
* @brief This method creates a snapshot of the metrics collected until the current point in time
* and returns an object with the stats.
*
* @param [in] resetCollection Boolean that specifies whether or not to clear the stats collected after
* creating the snapshot.
*
* @return MTRMetric object representing the metric data.
*/
- (MTRMetrics *)metricSnapshot:(BOOL)resetCollection;

@end

NS_ASSUME_NONNULL_END
Loading
Loading