Merge remote-tracking branch 'upstream/master' into string_ref
diff --git a/BUILD b/BUILD
index 607e3f4..25b26b7 100644
--- a/BUILD
+++ b/BUILD
@@ -209,6 +209,8 @@
     "src/core/json/json_writer.h",
     "src/core/profiling/timers.h",
     "src/core/profiling/timers_preciseclock.h",
+    "src/core/statistics/census_interface.h",
+    "src/core/statistics/census_rpc_stats.h",
     "src/core/surface/byte_buffer_queue.h",
     "src/core/surface/call.h",
     "src/core/surface/channel.h",
@@ -476,6 +478,8 @@
     "src/core/json/json_writer.h",
     "src/core/profiling/timers.h",
     "src/core/profiling/timers_preciseclock.h",
+    "src/core/statistics/census_interface.h",
+    "src/core/statistics/census_rpc_stats.h",
     "src/core/surface/byte_buffer_queue.h",
     "src/core/surface/call.h",
     "src/core/surface/channel.h",
@@ -1229,6 +1233,8 @@
     "src/core/json/json_writer.h",
     "src/core/profiling/timers.h",
     "src/core/profiling/timers_preciseclock.h",
+    "src/core/statistics/census_interface.h",
+    "src/core/statistics/census_rpc_stats.h",
     "src/core/surface/byte_buffer_queue.h",
     "src/core/surface/call.h",
     "src/core/surface/channel.h",
diff --git a/build.json b/build.json
index caa83ab..c974250 100644
--- a/build.json
+++ b/build.json
@@ -182,6 +182,8 @@
         "src/core/json/json_writer.h",
         "src/core/profiling/timers.h",
         "src/core/profiling/timers_preciseclock.h",
+        "src/core/statistics/census_interface.h",
+        "src/core/statistics/census_rpc_stats.h",
         "src/core/surface/byte_buffer_queue.h",
         "src/core/surface/call.h",
         "src/core/surface/channel.h",
diff --git a/gRPC.podspec b/gRPC.podspec
index 0e826b5..f6d09db 100644
--- a/gRPC.podspec
+++ b/gRPC.podspec
@@ -211,6 +211,8 @@
                       'src/core/json/json_writer.h',
                       'src/core/profiling/timers.h',
                       'src/core/profiling/timers_preciseclock.h',
+                      'src/core/statistics/census_interface.h',
+                      'src/core/statistics/census_rpc_stats.h',
                       'src/core/surface/byte_buffer_queue.h',
                       'src/core/surface/call.h',
                       'src/core/surface/channel.h',
@@ -481,6 +483,8 @@
                               'src/core/json/json_writer.h',
                               'src/core/profiling/timers.h',
                               'src/core/profiling/timers_preciseclock.h',
+                              'src/core/statistics/census_interface.h',
+                              'src/core/statistics/census_rpc_stats.h',
                               'src/core/surface/byte_buffer_queue.h',
                               'src/core/surface/call.h',
                               'src/core/surface/channel.h',
diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m
index 06581e7..09a55e0 100644
--- a/src/objective-c/tests/GRPCClientTests.m
+++ b/src/objective-c/tests/GRPCClientTests.m
@@ -53,6 +53,37 @@
 static ProtoMethod *kEmptyCallMethod;
 static ProtoMethod *kUnaryCallMethod;
 
+// This is an observer class for testing that responseMetadata is KVO-compliant
+
+@interface PassthroughObserver : NSObject
+
+- (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback;
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
+                       context:(void *)context;
+@end
+
+@implementation PassthroughObserver {
+  void (^_callback)(NSString*, id, NSDictionary*);
+}
+
+- (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback {
+  self = [super init];
+  if (self) {
+    _callback = callback;
+  }
+  return self;
+  
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
+{
+  _callback(keyPath, object, change);
+  [object removeObserver:self forKeyPath:keyPath];
+}
+
+@end
+
 @interface GRPCClientTests : XCTestCase
 @end
 
@@ -183,4 +214,35 @@
   [self waitForExpectationsWithTimeout:4 handler:nil];
 }
 
+- (void)testResponseMetadataKVO {
+  __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
+  __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
+  __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."];
+  
+  GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
+                                             path:kEmptyCallMethod.HTTPPath
+                                   requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
+  
+  PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) {
+    if ([keypath isEqual: @"responseHeaders"]) {
+      [metadata fulfill];
+    }
+  }];
+  
+  [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL];
+  
+  id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
+    XCTAssertNotNil(value, @"nil value received as response.");
+    XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
+    [response fulfill];
+  } completionHandler:^(NSError *errorOrNil) {
+    XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
+    [completion fulfill];
+  }];
+  
+  [call startWithWriteable:responsesWriteable];
+  
+  [self waitForExpectationsWithTimeout:8 handler:nil];
+}
+
 @end
diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal
index d27c5d9..06f0f4e 100644
--- a/tools/doxygen/Doxyfile.core.internal
+++ b/tools/doxygen/Doxyfile.core.internal
@@ -846,6 +846,8 @@
 src/core/json/json_writer.h \
 src/core/profiling/timers.h \
 src/core/profiling/timers_preciseclock.h \
+src/core/statistics/census_interface.h \
+src/core/statistics/census_rpc_stats.h \
 src/core/surface/byte_buffer_queue.h \
 src/core/surface/call.h \
 src/core/surface/channel.h \
diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json
index d7cf38c..a89e534 100644
--- a/tools/run_tests/sources_and_headers.json
+++ b/tools/run_tests/sources_and_headers.json
@@ -12340,6 +12340,8 @@
       "src/core/security/secure_transport_setup.h", 
       "src/core/security/security_connector.h", 
       "src/core/security/security_context.h", 
+      "src/core/statistics/census_interface.h", 
+      "src/core/statistics/census_rpc_stats.h", 
       "src/core/surface/byte_buffer_queue.h", 
       "src/core/surface/call.h", 
       "src/core/surface/channel.h", 
@@ -12556,6 +12558,8 @@
       "src/core/security/security_context.h", 
       "src/core/security/server_auth_filter.c", 
       "src/core/security/server_secure_chttp2.c", 
+      "src/core/statistics/census_interface.h", 
+      "src/core/statistics/census_rpc_stats.h", 
       "src/core/surface/byte_buffer.c", 
       "src/core/surface/byte_buffer_queue.c", 
       "src/core/surface/byte_buffer_queue.h", 
@@ -12806,6 +12810,8 @@
       "src/core/json/json_writer.h", 
       "src/core/profiling/timers.h", 
       "src/core/profiling/timers_preciseclock.h", 
+      "src/core/statistics/census_interface.h", 
+      "src/core/statistics/census_rpc_stats.h", 
       "src/core/surface/byte_buffer_queue.h", 
       "src/core/surface/call.h", 
       "src/core/surface/channel.h", 
@@ -12992,6 +12998,8 @@
       "src/core/profiling/stap_timers.c", 
       "src/core/profiling/timers.h", 
       "src/core/profiling/timers_preciseclock.h", 
+      "src/core/statistics/census_interface.h", 
+      "src/core/statistics/census_rpc_stats.h", 
       "src/core/surface/byte_buffer.c", 
       "src/core/surface/byte_buffer_queue.c", 
       "src/core/surface/byte_buffer_queue.h", 
diff --git a/vsprojects/grpc/grpc.vcxproj b/vsprojects/grpc/grpc.vcxproj
index 500cf9f..b17eea9 100644
--- a/vsprojects/grpc/grpc.vcxproj
+++ b/vsprojects/grpc/grpc.vcxproj
@@ -308,6 +308,8 @@
     <ClInclude Include="..\..\src\core\json\json_writer.h" />
     <ClInclude Include="..\..\src\core\profiling\timers.h" />
     <ClInclude Include="..\..\src\core\profiling\timers_preciseclock.h" />
+    <ClInclude Include="..\..\src\core\statistics\census_interface.h" />
+    <ClInclude Include="..\..\src\core\statistics\census_rpc_stats.h" />
     <ClInclude Include="..\..\src\core\surface\byte_buffer_queue.h" />
     <ClInclude Include="..\..\src\core\surface\call.h" />
     <ClInclude Include="..\..\src\core\surface\channel.h" />
diff --git a/vsprojects/grpc/grpc.vcxproj.filters b/vsprojects/grpc/grpc.vcxproj.filters
index 02060b7..a955e9e 100644
--- a/vsprojects/grpc/grpc.vcxproj.filters
+++ b/vsprojects/grpc/grpc.vcxproj.filters
@@ -683,6 +683,12 @@
     <ClInclude Include="..\..\src\core\profiling\timers_preciseclock.h">
       <Filter>src\core\profiling</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\core\statistics\census_interface.h">
+      <Filter>src\core\statistics</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\src\core\statistics\census_rpc_stats.h">
+      <Filter>src\core\statistics</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\core\surface\byte_buffer_queue.h">
       <Filter>src\core\surface</Filter>
     </ClInclude>
@@ -845,6 +851,9 @@
     <Filter Include="src\core\security">
       <UniqueIdentifier>{1d850ac6-e639-4eab-5338-4ba40272fcc9}</UniqueIdentifier>
     </Filter>
+    <Filter Include="src\core\statistics">
+      <UniqueIdentifier>{0ef49896-2313-4a3f-1ce2-716fa0e5c6ca}</UniqueIdentifier>
+    </Filter>
     <Filter Include="src\core\surface">
       <UniqueIdentifier>{aeb18e82-5d25-0aad-8b02-a0a3470073ce}</UniqueIdentifier>
     </Filter>
diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj
index 13c018c..a692c48 100644
--- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj
+++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj
@@ -291,6 +291,8 @@
     <ClInclude Include="..\..\src\core\json\json_writer.h" />
     <ClInclude Include="..\..\src\core\profiling\timers.h" />
     <ClInclude Include="..\..\src\core\profiling\timers_preciseclock.h" />
+    <ClInclude Include="..\..\src\core\statistics\census_interface.h" />
+    <ClInclude Include="..\..\src\core\statistics\census_rpc_stats.h" />
     <ClInclude Include="..\..\src\core\surface\byte_buffer_queue.h" />
     <ClInclude Include="..\..\src\core\surface\call.h" />
     <ClInclude Include="..\..\src\core\surface\channel.h" />
diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters
index 5adcdd6..1c4036d 100644
--- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters
+++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters
@@ -581,6 +581,12 @@
     <ClInclude Include="..\..\src\core\profiling\timers_preciseclock.h">
       <Filter>src\core\profiling</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\core\statistics\census_interface.h">
+      <Filter>src\core\statistics</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\src\core\statistics\census_rpc_stats.h">
+      <Filter>src\core\statistics</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\core\surface\byte_buffer_queue.h">
       <Filter>src\core\surface</Filter>
     </ClInclude>
@@ -740,6 +746,9 @@
     <Filter Include="src\core\profiling">
       <UniqueIdentifier>{7f91d9bf-c9de-835a-d74d-b16f843b89a9}</UniqueIdentifier>
     </Filter>
+    <Filter Include="src\core\statistics">
+      <UniqueIdentifier>{e084164c-a069-00e3-db35-4e0b1cd6f0b7}</UniqueIdentifier>
+    </Filter>
     <Filter Include="src\core\surface">
       <UniqueIdentifier>{6cd0127e-c24b-d43c-38f5-198db8d4322a}</UniqueIdentifier>
     </Filter>