summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorFabian Guerra Soto <fabian.guerra@mapbox.com>2019-04-09 16:10:14 -0700
committerGitHub <noreply@github.com>2019-04-09 16:10:14 -0700
commit8389e746b6745a68fcd58ece8e398bde0a85b57f (patch)
tree5ee65928c8636317265018c20bd7f11b2d3e3e4b /platform/darwin
parent2ee342448bc6679ef6735ea0deb23f21dcf7c78b (diff)
downloadqtlocation-mapboxgl-8389e746b6745a68fcd58ece8e398bde0a85b57f.tar.gz
[ios, macos] Add Mapbox Maps SDK metrics manager. (#13997)
Added MGLSDKMetricsManager to keep track of SDK related events such as tile download times. * [ios, macos] Add tile download performance event. * [ios, macos] Modify the performance event format. * [ios, macos] Add Mapbox Maps SDK events manager. * [ios, macos] Remove events delegate. * [ios, macos] Remove deprecated macro. * [ios, macos] Rename MGLEventsManager to MGLMetricsManager. * [ios, macos] Make MGLMetricsDelegate follow delegate convention. * [ios, macos] Remove mbgl-filesource target dependency on MGLMetricsManager. * [ios, macos] Update metrics manager documentation. * [ios, macos] Add device metadata. * [ios, macos] Rename MGLMetricsManager to MGLSDKMetricsManager.
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration.m67
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration_Private.h25
-rw-r--r--platform/darwin/src/MGLSDKMetricsManager.h79
-rw-r--r--platform/darwin/src/MGLSDKMetricsManager.m99
-rw-r--r--platform/darwin/src/MGLSDKMetricsManager_Private.h11
-rw-r--r--platform/darwin/src/http_file_source.mm11
6 files changed, 287 insertions, 5 deletions
diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m
index 8262e7eea1..bac4d12ee5 100644
--- a/platform/darwin/src/MGLNetworkConfiguration.m
+++ b/platform/darwin/src/MGLNetworkConfiguration.m
@@ -1,19 +1,33 @@
-#import "MGLNetworkConfiguration.h"
+#import "MGLNetworkConfiguration_Private.h"
+
+static NSString * const MGLStartTime = @"start_time";
+static NSString * const MGLResourceType = @"resource_type";
+NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace";
@interface MGLNetworkConfiguration ()
@property (strong) NSURLSessionConfiguration *sessionConfig;
+@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDictionary*> *events;
+@property (nonatomic, weak) id<MGLNetworkConfigurationMetricsDelegate> metricsDelegate;
@end
@implementation MGLNetworkConfiguration
+- (instancetype)init {
+ if (self = [super init]) {
+ self.sessionConfiguration = nil;
+ _events = [NSMutableDictionary dictionary];
+ }
+
+ return self;
+}
+
+ (instancetype)sharedManager {
static dispatch_once_t onceToken;
static MGLNetworkConfiguration *_sharedManager;
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
- _sharedManager.sessionConfiguration = nil;
});
return _sharedManager;
@@ -48,4 +62,53 @@
return sessionConfiguration;
}
+- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType {
+ if (urlString && ![self.events objectForKey:urlString]) {
+ [self.events setObject:@{ MGLStartTime: [NSDate date], MGLResourceType: resourceType } forKey:urlString];
+ }
+}
+
+- (void)stopDownloadEvent:(NSString *)urlString {
+ [self sendEventForURL:urlString withAction:nil];
+}
+
+- (void)cancelDownloadEvent:(NSString *)urlString {
+ [self sendEventForURL:urlString withAction:@"cancel"];
+}
+
+- (void)sendEventForURL:(NSString *)urlString withAction:(NSString *)action
+{
+ if (urlString && [self.events objectForKey:urlString]) {
+ NSDictionary *eventAttributes = [self eventAttributesForURL:urlString withAction:action];
+ [self.metricsDelegate networkConfiguration:self didGenerateMetricEvent:eventAttributes];
+ [self.events removeObjectForKey:urlString];
+ }
+}
+
+- (NSDictionary *)eventAttributesForURL:(NSString *)urlString withAction:(NSString *)action
+{
+ NSDictionary *parameters = [self.events objectForKey:urlString];
+ NSDate *startDate = [parameters objectForKey:MGLStartTime];
+ NSDate *endDate = [NSDate date];
+ NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate];
+ NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init];
+ iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
+ NSString *createdDate = [iso8601Formatter stringFromDate:[NSDate date]];
+
+ NSMutableArray *attributes = [NSMutableArray array];
+ [attributes addObject:@{ @"name" : @"resource" , @"value" : urlString }];
+ [attributes addObject:@{ @"name" : MGLResourceType , @"value" : [parameters objectForKey:MGLResourceType] }];
+ if (action) {
+ [attributes addObject:@{ @"name" : @"action" , @"value" : action }];
+ }
+
+ return @{
+ @"event" : kMGLDownloadPerformanceEvent,
+ @"created" : createdDate,
+ @"sessionId" : [NSUUID UUID].UUIDString,
+ @"counters" : @[ @{ @"name" : @"elapsed_time" , @"value" : @(elapsedTime) } ],
+ @"attributes" : attributes
+ };
+}
+
@end
diff --git a/platform/darwin/src/MGLNetworkConfiguration_Private.h b/platform/darwin/src/MGLNetworkConfiguration_Private.h
new file mode 100644
index 0000000000..18c78a004a
--- /dev/null
+++ b/platform/darwin/src/MGLNetworkConfiguration_Private.h
@@ -0,0 +1,25 @@
+#import "MGLNetworkConfiguration.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@class MGLNetworkConfiguration;
+@protocol MGLNetworkConfigurationMetricsDelegate <NSObject>
+
+- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didGenerateMetricEvent:(NSDictionary *)metricEvent;
+
+@end
+
+extern NSString * const kMGLDownloadPerformanceEvent;
+
+@interface MGLNetworkConfiguration (Private)
+
+@property (nonatomic, strong) NSMutableDictionary<NSString*, NSDictionary*> *events;
+@property (nonatomic, weak) id<MGLNetworkConfigurationMetricsDelegate> metricsDelegate;
+
+- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType;
+- (void)stopDownloadEvent:(NSString *)urlString;
+- (void)cancelDownloadEvent:(NSString *)urlString;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLSDKMetricsManager.h b/platform/darwin/src/MGLSDKMetricsManager.h
new file mode 100644
index 0000000000..49b3391a72
--- /dev/null
+++ b/platform/darwin/src/MGLSDKMetricsManager.h
@@ -0,0 +1,79 @@
+#import <Foundation/Foundation.h>
+#import "MGLFoundation.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ :nodoc:
+ The metrics type used to handle metrics events.
+ */
+typedef NS_ENUM(NSUInteger, MGLMetricType) {
+ /** :nodoc:
+ Metric that measures performance.
+ */
+ MGLMetricTypePerformance = 0,
+};
+
+FOUNDATION_EXTERN MGL_EXPORT NSString* MGLStringFromMetricType(MGLMetricType metricType);
+
+@class MGLMetricsManager;
+
+/**
+ :nodoc:
+ The `MGLMetricsManagerDelegate` protocol defines a set of methods that you
+ can use to receive metric events.
+ */
+@protocol MGLMetricsManagerDelegate <NSObject>
+
+/**
+ :nodoc:
+ Asks the delegate whether the metrics manager should handle metric events.
+
+ @param metricsManager The metrics manager object.
+ @param metricType The metric type event.
+ */
+- (BOOL)metricsManager:(MGLMetricsManager *)metricsManager shouldHandleMetric:(MGLMetricType)metricType;
+
+/**
+ :nodoc:
+ Asks the delegate to handle metric events.
+
+ @param metricsManager The metrics manager object.
+ @param metricType The metric type event.
+ @param attributes The metric attributes.
+ */
+- (void)metricsManager:(MGLMetricsManager *)metricsManager didCollectMetric:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes;
+
+@end
+
+/**
+ :nodoc:
+ The `MGLMetricsManager` object provides a single poin to collect SDK metrics
+ such as tile download latency.
+ */
+MGL_EXPORT
+@interface MGLMetricsManager : NSObject
+
+/**
+ :nodoc:
+ Returns the shared metrics manager object.
+ */
+@property (class, nonatomic, readonly) MGLMetricsManager *sharedManager;
+
+/**
+ :nodoc:
+ The metrics manager delegate that will recieve metric events.
+ */
+@property (nonatomic, weak) id<MGLMetricsManagerDelegate> delegate;
+
+#if TARGET_OS_IOS
+/**
+ :nodoc:
+ Sends metric events to Mapbox.
+ */
+- (void)pushMetric:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes;
+#endif
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLSDKMetricsManager.m b/platform/darwin/src/MGLSDKMetricsManager.m
new file mode 100644
index 0000000000..ed48eaf0c1
--- /dev/null
+++ b/platform/darwin/src/MGLSDKMetricsManager.m
@@ -0,0 +1,99 @@
+#import "MGLSDKMetricsManager_Private.h"
+#import "MGLNetworkConfiguration_Private.h"
+#if TARGET_OS_IOS
+#import "MGLMapboxEvents.h"
+#import <mach-o/arch.h>
+#import <sys/utsname.h>
+#import <UIKit/UIKit.h>
+#endif
+
+NSString* MGLStringFromMetricType(MGLMetricType metricType) {
+ NSString *eventName;
+
+ switch (metricType) {
+ case MGLMetricTypePerformance:
+ eventName = kMGLDownloadPerformanceEvent;
+ break;
+ }
+ return eventName;
+}
+
+@interface MGLMetricsManager() <MGLNetworkConfigurationMetricsDelegate>
+
+@property (strong, nonatomic) NSDictionary *metadata;
+
+@end
+
+@implementation MGLMetricsManager
+
++ (instancetype)sharedManager
+{
+ static dispatch_once_t once;
+ static MGLMetricsManager *sharedConfiguration;
+ dispatch_once(&once, ^{
+ sharedConfiguration = [[self alloc] init];
+ [MGLNetworkConfiguration sharedManager].metricsDelegate = sharedConfiguration;
+#if TARGET_OS_IOS
+ UIDevice *currentDevice = [UIDevice currentDevice];
+
+ NSString *osVersion = currentDevice.systemVersion;
+
+ NSString *screenSize = [NSString stringWithFormat:@"%.fx%.f", [UIScreen mainScreen].bounds.size.width,
+ [UIScreen mainScreen].bounds.size.height];
+
+ NSLocale *currentLocale = [NSLocale currentLocale];
+ NSString *country = [currentLocale objectForKey:NSLocaleCountryCode];
+
+ NSString *device = deviceName();
+
+ const NXArchInfo localArchInfo = *NXGetLocalArchInfo();
+ NSString *abi = [NSString stringWithUTF8String:localArchInfo.description];
+
+ NSString *ram = [NSString stringWithFormat:@"%llu", [NSProcessInfo processInfo].physicalMemory];
+
+ NSString *os = currentDevice.systemName;
+
+ sharedConfiguration.metadata = @{ @"version" : osVersion,
+ @"screenSize" : screenSize,
+ @"country" : country,
+ @"device" : device,
+ @"abi" : abi,
+ @"brand" : @"Apple",
+ @"ram" : ram,
+ @"os" : os
+ };
+#endif
+ });
+ return sharedConfiguration;
+}
+
+- (void)handleMetricsEvent:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes {
+ if ([self.delegate metricsManager:self shouldHandleMetric:metricType]) {
+ [self.delegate metricsManager:self didCollectMetric:metricType withAttributes:attributes];
+ }
+}
+
+#if TARGET_OS_IOS
+- (void)pushMetric:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes {
+ NSString *eventName = MGLStringFromMetricType(metricType);
+ NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
+ [mutableAttributes setObject:self.metadata forKey:@"metadata"];
+
+ [MGLMapboxEvents pushEvent:eventName withAttributes:mutableAttributes];
+}
+
+NSString* deviceName()
+{
+ struct utsname systemInfo;
+ uname(&systemInfo);
+
+ return [NSString stringWithCString:systemInfo.machine
+ encoding:NSUTF8StringEncoding];
+}
+#endif
+
+- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didGenerateMetricEvent:(NSDictionary *)metricEvent {
+ [self handleMetricsEvent:MGLMetricTypePerformance withAttributes:metricEvent];
+}
+
+@end
diff --git a/platform/darwin/src/MGLSDKMetricsManager_Private.h b/platform/darwin/src/MGLSDKMetricsManager_Private.h
new file mode 100644
index 0000000000..a3cdfca794
--- /dev/null
+++ b/platform/darwin/src/MGLSDKMetricsManager_Private.h
@@ -0,0 +1,11 @@
+#import "MGLSDKMetricsManager.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface MGLMetricsManager (Private)
+
+- (void)handleMetricsEvent:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm
index c3918ad62b..c26568d8bc 100644
--- a/platform/darwin/src/http_file_source.mm
+++ b/platform/darwin/src/http_file_source.mm
@@ -8,7 +8,7 @@
#import <Foundation/Foundation.h>
#import "MGLLoggingConfiguration_Private.h"
-#import "MGLNetworkConfiguration.h"
+#import "MGLNetworkConfiguration_Private.h"
#include <mutex>
#include <chrono>
@@ -215,14 +215,19 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
}
[req addValue:impl->userAgent forHTTPHeaderField:@"User-Agent"];
-
+
+ if (resource.kind == mbgl::Resource::Kind::Tile) {
+ [[MGLNetworkConfiguration sharedManager] startDownloadEvent:url.relativePath type:@"tile"];
+ }
+
request->task = [impl->session
dataTaskWithRequest:req
completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) {
if (error && [error code] == NSURLErrorCancelled) {
+ [[MGLNetworkConfiguration sharedManager] cancelDownloadEvent:res.URL.relativePath];
return;
}
-
+ [[MGLNetworkConfiguration sharedManager] stopDownloadEvent:res.URL.relativePath];
Response response;
using Error = Response::Error;