FIRHTTPMetric.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRHTTPMetric.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/FIRHTTPMetric+Private.h"
  16. #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  18. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  19. #import "FirebasePerformance/Sources/FPRDataUtils.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  21. @interface FIRHTTPMetric ()
  22. /* A placeholder URLRequest used for SDK metric tracking. */
  23. @property(nonatomic, strong) NSURLRequest *URLRequest;
  24. @end
  25. @implementation FIRHTTPMetric
  26. - (nullable instancetype)initWithURL:(nonnull NSURL *)URL HTTPMethod:(FIRHTTPMethod)httpMethod {
  27. BOOL tracingEnabled = [FPRConfigurations sharedInstance].isDataCollectionEnabled;
  28. if (!tracingEnabled) {
  29. FPRLogInfo(kFPRTraceDisabled, @"Trace feature is disabled. Dropping http metric - %@",
  30. URL.absoluteString);
  31. return nil;
  32. }
  33. BOOL sdkEnabled = [[FPRConfigurations sharedInstance] sdkEnabled];
  34. if (!sdkEnabled) {
  35. FPRLogInfo(kFPRTraceDisabled, @"Dropping event since Performance SDK is disabled.");
  36. return nil;
  37. }
  38. NSMutableURLRequest *URLRequest = [[NSMutableURLRequest alloc] initWithURL:URL];
  39. NSString *HTTPMethodString = nil;
  40. switch (httpMethod) {
  41. case FIRHTTPMethodGET:
  42. HTTPMethodString = @"GET";
  43. break;
  44. case FIRHTTPMethodPUT:
  45. HTTPMethodString = @"PUT";
  46. break;
  47. case FIRHTTPMethodPOST:
  48. HTTPMethodString = @"POST";
  49. break;
  50. case FIRHTTPMethodHEAD:
  51. HTTPMethodString = @"HEAD";
  52. break;
  53. case FIRHTTPMethodDELETE:
  54. HTTPMethodString = @"DELETE";
  55. break;
  56. case FIRHTTPMethodPATCH:
  57. HTTPMethodString = @"PATCH";
  58. break;
  59. case FIRHTTPMethodOPTIONS:
  60. HTTPMethodString = @"OPTIONS";
  61. break;
  62. case FIRHTTPMethodTRACE:
  63. HTTPMethodString = @"TRACE";
  64. break;
  65. case FIRHTTPMethodCONNECT:
  66. HTTPMethodString = @"CONNECT";
  67. break;
  68. }
  69. [URLRequest setHTTPMethod:HTTPMethodString];
  70. if (URLRequest && HTTPMethodString != nil) {
  71. self = [super init];
  72. _networkTrace = [[FPRNetworkTrace alloc] initWithURLRequest:URLRequest];
  73. _URLRequest = [URLRequest copy];
  74. return self;
  75. }
  76. FPRLogError(kFPRInstrumentationInvalidInputs, @"Invalid URL");
  77. return nil;
  78. }
  79. - (void)start {
  80. [self.networkTrace start];
  81. }
  82. - (void)markRequestComplete {
  83. [self.networkTrace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
  84. }
  85. - (void)markResponseStart {
  86. [self.networkTrace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  87. }
  88. - (void)stop {
  89. self.networkTrace.requestSize = self.requestPayloadSize;
  90. self.networkTrace.responseSize = self.responsePayloadSize;
  91. // Create a dummy URL Response that will be used for data extraction.
  92. NSString *responsePayloadSize =
  93. [[NSString alloc] initWithFormat:@"%ld", self.responsePayloadSize];
  94. NSMutableDictionary<NSString *, NSString *> *headerFields =
  95. [[NSMutableDictionary<NSString *, NSString *> alloc] init];
  96. if (self.responseContentType) {
  97. [headerFields setObject:self.responseContentType forKey:@"Content-Type"];
  98. }
  99. [headerFields setObject:responsePayloadSize forKey:@"Content-Length"];
  100. if (self.responseCode == 0) {
  101. FPRLogError(kFPRInstrumentationInvalidInputs, @"Response code not set for request - %@",
  102. self.URLRequest.URL);
  103. return;
  104. }
  105. NSHTTPURLResponse *URLResponse = [[NSHTTPURLResponse alloc] initWithURL:self.URLRequest.URL
  106. statusCode:self.responseCode
  107. HTTPVersion:nil
  108. headerFields:headerFields];
  109. [self.networkTrace didCompleteRequestWithResponse:URLResponse error:nil];
  110. }
  111. #pragma mark - Custom attributes related methods
  112. - (NSDictionary<NSString *, NSString *> *)attributes {
  113. return [self.networkTrace attributes];
  114. }
  115. - (void)setValue:(NSString *)value forAttribute:(nonnull NSString *)attribute {
  116. [self.networkTrace setValue:value forAttribute:attribute];
  117. }
  118. - (NSString *)valueForAttribute:(NSString *)attribute {
  119. return [self.networkTrace valueForAttribute:attribute];
  120. }
  121. - (void)removeAttribute:(NSString *)attribute {
  122. [self.networkTrace removeAttribute:attribute];
  123. }
  124. @end