Skip to content

Commit e37708d

Browse files
p-sunfacebook-github-bot
authored andcommitted
Allow image loaders to enable/disable image telemetry
Summary: When shouldEnableLoggingForRequestUrl is false, ImageTelemetry is not initialized, and no logging is done. * Replace `- (NSString *)loaderModuleNameForRequestUrl:(NSURL *)url` with `- (BOOL)shouldEnableLoggingForRequestUrl:(NSURL *)url` * Rename RCTImageLoaderInstrumentableProtocol.h -> RCTImageLoaderLoggableProtocol.h Reviewed By: fkgozali Differential Revision: D24523984 fbshipit-source-id: a5463eceea1c40f9452b0ad2ee6bf047f71a02c1
1 parent 1b71ec4 commit e37708d

9 files changed

+48
-45
lines changed

Libraries/Image/RCTImageLoader.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#import <React/RCTImageURLLoader.h>
1616
#import <React/RCTImageCache.h>
1717
#import <React/RCTImageLoaderProtocol.h>
18-
#import <React/RCTImageLoaderInstrumentableProtocol.h>
18+
#import <React/RCTImageLoaderLoggable.h>
1919

20-
@interface RCTImageLoader : NSObject <RCTBridgeModule, RCTImageLoaderProtocol, RCTImageLoaderInstrumentableProtocol>
20+
@interface RCTImageLoader : NSObject <RCTBridgeModule, RCTImageLoaderProtocol, RCTImageLoaderLoggableProtocol>
2121
- (instancetype)init;
2222
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate NS_DESIGNATED_INITIALIZER;
2323
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate

Libraries/Image/RCTImageLoader.mm

+4-4
Original file line numberDiff line numberDiff line change
@@ -839,12 +839,12 @@ - (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRe
839839
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:loaderRequest.requestId imageURL:imageURLRequest.URL cancellationBlock:cancellationBlock];
840840
}
841841

842-
- (NSString *)loaderModuleNameForRequestUrl:(NSURL *)url {
842+
- (BOOL)shouldEnablePerfLoggingForRequestUrl:(NSURL *)url {
843843
id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:url];
844-
if ([loadHandler respondsToSelector:@selector(loaderModuleNameForRequestUrl:)]) {
845-
return [(id<RCTImageURLLoaderWithAttribution>)loadHandler loaderModuleNameForRequestUrl:url];
844+
if ([loadHandler respondsToSelector:@selector(shouldEnablePerfLogging)]) {
845+
return [(id<RCTImageURLLoaderWithAttribution>)loadHandler shouldEnablePerfLogging];
846846
}
847-
return nil;
847+
return NO;
848848
}
849849

850850
- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView

Libraries/Image/RCTImageLoaderInstrumentableProtocol.h

-15
This file was deleted.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* The image loader (i.e. RCTImageLoader) implement this to declare whether image performance should be logged.
10+
*/
11+
@protocol RCTImageLoaderLoggableProtocol
12+
13+
/**
14+
* Image instrumentation - declares whether its caller should log images
15+
*/
16+
- (BOOL)shouldEnablePerfLoggingForRequestUrl:(NSURL *)url;
17+
18+
@end
19+
20+
/**
21+
* Image handlers in the image loader implement this to declare whether image performance should be logged.
22+
*/
23+
@protocol RCTImageLoaderLoggable
24+
25+
/**
26+
* Image instrumentation - declares whether its caller should log images
27+
*/
28+
- (BOOL)shouldEnablePerfLogging;
29+
30+
@end

Libraries/Image/RCTImageLoaderWithAttributionProtocol.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#import <React/RCTImageLoaderProtocol.h>
1111
#import <React/RCTImageURLLoaderWithAttribution.h>
12-
#import <React/RCTImageLoaderInstrumentableProtocol.h>
1312

1413
RCT_EXTERN BOOL RCTImageLoadingInstrumentationEnabled(void);
1514
RCT_EXTERN BOOL RCTImageLoadingPerfInstrumentationEnabled(void);
@@ -19,7 +18,7 @@ RCT_EXTERN void RCTEnableImageLoadingPerfInstrumentation(BOOL enabled);
1918
RCT_EXTERN BOOL RCTGetImageLoadingPerfInstrumentationForFabricEnabled();
2019
RCT_EXTERN void RCTSetImageLoadingPerfInstrumentationForFabricEnabledBlock(BOOL (^getEnabled)());
2120

22-
@protocol RCTImageLoaderWithAttributionProtocol<RCTImageLoaderProtocol, RCTImageLoaderInstrumentableProtocol>
21+
@protocol RCTImageLoaderWithAttributionProtocol<RCTImageLoaderProtocol, RCTImageLoaderLoggableProtocol>
2322

2423
// TODO (T61325135): Remove C++ checks
2524
#ifdef __cplusplus

Libraries/Image/RCTImageURLLoaderWithAttribution.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#import <React/RCTImageURLLoader.h>
99
#import <React/RCTImageLoaderProtocol.h>
10-
#import <React/RCTImageLoaderInstrumentableProtocol.h>
10+
#import <React/RCTImageLoaderLoggable.h>
1111

1212
// TODO (T61325135): Remove C++ checks
1313
#ifdef __cplusplus
@@ -39,7 +39,7 @@ struct ImageURLLoaderAttribution {
3939
* Same as the RCTImageURLLoader interface, but allows passing in optional `attribution` information.
4040
* This is useful for per-app logging and other instrumentation.
4141
*/
42-
@protocol RCTImageURLLoaderWithAttribution <RCTImageURLLoader, RCTImageLoaderInstrumentableProtocol>
42+
@protocol RCTImageURLLoaderWithAttribution <RCTImageURLLoader, RCTImageLoaderLoggable>
4343

4444
// TODO (T61325135): Remove C++ checks
4545
#ifdef __cplusplus

ReactCommon/react/renderer/imagemanager/ImageTelemetry.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ SurfaceId ImageTelemetry::getSurfaceId() const {
1919
return surfaceId_;
2020
}
2121

22-
std::string ImageTelemetry::getLoaderModuleName() const {
23-
return loaderModuleName_;
24-
}
25-
26-
void ImageTelemetry::setLoaderModuleName(std::string const &loaderModuleName) {
27-
loaderModuleName_ = loaderModuleName;
28-
}
29-
3022
TelemetryTimePoint ImageTelemetry::getWillRequestUrlTime() const {
3123
assert(willRequestUrlTime_ != kTelemetryUndefinedTimePoint);
3224
return willRequestUrlTime_;

ReactCommon/react/renderer/imagemanager/ImageTelemetry.h

-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ class ImageTelemetry final {
3131
TelemetryTimePoint getWillRequestUrlTime() const;
3232

3333
SurfaceId getSurfaceId() const;
34-
std::string getLoaderModuleName() const;
35-
void setLoaderModuleName(std::string const &loaderModuleName);
3634

3735
private:
3836
TelemetryTimePoint willRequestUrlTime_{kTelemetryUndefinedTimePoint};
3937

4038
const SurfaceId surfaceId_;
41-
std::string loaderModuleName_{""};
4239
};
4340

4441
} // namespace react

ReactCommon/react/renderer/imagemanager/platform/ios/RCTImageManager.mm

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ - (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfa
4040
{
4141
SystraceSection s("RCTImageManager::requestImage");
4242

43-
auto telemetry = std::make_shared<ImageTelemetry>(surfaceId);
44-
telemetry->willRequestUrl();
43+
NSURLRequest *request = NSURLRequestFromImageSource(imageSource);
44+
std::shared_ptr<ImageTelemetry> telemetry;
45+
if ([self->_imageLoader shouldEnablePerfLoggingForRequestUrl:request.URL]) {
46+
telemetry = std::make_shared<ImageTelemetry>(surfaceId);
47+
telemetry->willRequestUrl();
48+
} else {
49+
telemetry = nullptr;
50+
}
51+
4552
auto imageRequest = ImageRequest(imageSource, telemetry);
4653
auto weakObserverCoordinator =
4754
(std::weak_ptr<const ImageResponseObserverCoordinator>)imageRequest.getSharedObserverCoordinator();
4855

4956
auto sharedCancelationFunction = SharedFunction<>();
5057
imageRequest.setCancelationFunction(sharedCancelationFunction);
5158

52-
NSURLRequest *request = NSURLRequestFromImageSource(imageSource);
53-
BOOL hasModuleName = [self->_imageLoader respondsToSelector:@selector(loaderModuleNameForRequestUrl:)];
54-
NSString *moduleName = hasModuleName ? [self->_imageLoader loaderModuleNameForRequestUrl:request.URL] : nil;
55-
std::string moduleCString =
56-
std::string([moduleName UTF8String], [moduleName lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
57-
telemetry->setLoaderModuleName(moduleCString);
58-
5959
/*
6060
* Even if an image is being loaded asynchronously on some other background thread, some other preparation
6161
* work (such as creating an `NSURLRequest` object and some obscure logic inside `RCTImageLoader`) can take a couple

0 commit comments

Comments
 (0)