Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Hongyu Chen | e09dc78 | 2015-08-21 11:28:33 -0700 | [diff] [blame] | 34 | #include "src/core/census/grpc_filter.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
murgatroid99 | 8685459 | 2015-08-27 10:27:19 -0700 | [diff] [blame] | 39 | #include <grpc/census.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/slice.h> |
| 43 | #include <grpc/support/time.h> |
| 44 | |
Craig Tiller | ed43f51 | 2015-11-19 08:53:23 -0800 | [diff] [blame] | 45 | #include "src/core/channel/channel_stack.h" |
Craig Tiller | ed43f51 | 2015-11-19 08:53:23 -0800 | [diff] [blame] | 46 | #include "src/core/statistics/census_interface.h" |
| 47 | #include "src/core/statistics/census_rpc_stats.h" |
| 48 | #include "src/core/transport/static_metadata.h" |
| 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | typedef struct call_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | census_op_id op_id; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 52 | census_context *ctxt; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | gpr_timespec start_ts; |
Hongyu Chen | 3c9b873 | 2015-08-13 17:01:02 -0700 | [diff] [blame] | 54 | int error; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 55 | |
| 56 | /* recv callback */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 57 | grpc_metadata_batch *recv_initial_metadata; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 58 | grpc_closure *on_done_recv; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 59 | grpc_closure finish_recv; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | } call_data; |
| 61 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 62 | typedef struct channel_data { uint8_t unused; } channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 64 | static void extract_and_annotate_method_tag(grpc_metadata_batch *md, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | call_data *calld, |
| 66 | channel_data *chand) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 67 | grpc_linked_mdelem *m; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 68 | for (m = md->list.head; m != NULL; m = m->next) { |
Craig Tiller | ed43f51 | 2015-11-19 08:53:23 -0800 | [diff] [blame] | 69 | if (m->md->key == GRPC_MDSTR_PATH) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 70 | gpr_log(GPR_DEBUG, "%s", |
| 71 | (const char *)GPR_SLICE_START_PTR(m->md->value->slice)); |
| 72 | /* Add method tag here */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 73 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | static void client_mutate_op(grpc_call_element *elem, |
| 78 | grpc_transport_stream_op *op) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 79 | call_data *calld = elem->call_data; |
| 80 | channel_data *chand = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 81 | if (op->send_initial_metadata) { |
| 82 | extract_and_annotate_method_tag(op->send_initial_metadata, calld, chand); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | } |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | static void client_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 87 | grpc_call_element *elem, |
| 88 | grpc_transport_stream_op *op) { |
| 89 | client_mutate_op(elem, op); |
| 90 | grpc_call_next_op(exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | static void server_on_done_recv(grpc_exec_ctx *exec_ctx, void *ptr, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 94 | bool success) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 95 | grpc_call_element *elem = ptr; |
| 96 | call_data *calld = elem->call_data; |
| 97 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | if (success) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 99 | extract_and_annotate_method_tag(calld->recv_initial_metadata, calld, chand); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | } |
| 101 | calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | static void server_mutate_op(grpc_call_element *elem, |
| 105 | grpc_transport_stream_op *op) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 106 | call_data *calld = elem->call_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 107 | if (op->recv_initial_metadata) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | /* substitute our callback for the op callback */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 109 | calld->recv_initial_metadata = op->recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 110 | calld->on_done_recv = op->recv_initial_metadata_ready; |
| 111 | op->recv_initial_metadata_ready = &calld->finish_recv; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | } |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | static void server_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 116 | grpc_call_element *elem, |
| 117 | grpc_transport_stream_op *op) { |
Craig Tiller | 7908f16 | 2015-12-10 11:31:49 -0800 | [diff] [blame] | 118 | /* TODO(ctiller): this code fails. I don't know why. I expect it's |
| 119 | incomplete, and someone should look at it soon. |
| 120 | |
| 121 | call_data *calld = elem->call_data; |
Craig Tiller | 5a95c34 | 2015-12-10 10:59:38 -0800 | [diff] [blame] | 122 | GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | server_mutate_op(elem, op); |
| 124 | grpc_call_next_op(exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | static void client_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 128 | grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 129 | grpc_call_element_args *args) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 130 | call_data *d = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | GPR_ASSERT(d != NULL); |
Hongyu Chen | 738e91e | 2015-10-13 15:05:00 -0700 | [diff] [blame] | 132 | memset(d, 0, sizeof(*d)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | d->start_ts = gpr_now(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 137 | grpc_call_element *elem) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 138 | call_data *d = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | GPR_ASSERT(d != NULL); |
Hongyu Chen | 58c927c | 2015-08-14 15:19:13 -0700 | [diff] [blame] | 140 | /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | static void server_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 144 | grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 145 | grpc_call_element_args *args) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 146 | call_data *d = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | GPR_ASSERT(d != NULL); |
Hongyu Chen | 738e91e | 2015-10-13 15:05:00 -0700 | [diff] [blame] | 148 | memset(d, 0, sizeof(*d)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | d->start_ts = gpr_now(GPR_CLOCK_REALTIME); |
Hongyu Chen | 3c9b873 | 2015-08-13 17:01:02 -0700 | [diff] [blame] | 150 | /* TODO(hongyu): call census_tracing_start_op here. */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 151 | grpc_closure_init(&d->finish_recv, server_on_done_recv, elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 155 | grpc_call_element *elem) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 156 | call_data *d = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | GPR_ASSERT(d != NULL); |
Hongyu Chen | 58c927c | 2015-08-14 15:19:13 -0700 | [diff] [blame] | 158 | /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 162 | grpc_channel_element *elem, |
| 163 | grpc_channel_element_args *args) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 164 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | GPR_ASSERT(chand != NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 169 | grpc_channel_element *elem) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 170 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | GPR_ASSERT(chand != NULL); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 172 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | |
| 174 | const grpc_channel_filter grpc_client_census_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 175 | client_start_transport_op, |
| 176 | grpc_channel_next_op, |
| 177 | sizeof(call_data), |
| 178 | client_init_call_elem, |
| 179 | grpc_call_stack_ignore_set_pollset, |
| 180 | client_destroy_call_elem, |
| 181 | sizeof(channel_data), |
| 182 | init_channel_elem, |
| 183 | destroy_channel_elem, |
| 184 | grpc_call_next_get_peer, |
| 185 | "census-client"}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | |
| 187 | const grpc_channel_filter grpc_server_census_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 188 | server_start_transport_op, |
| 189 | grpc_channel_next_op, |
| 190 | sizeof(call_data), |
| 191 | server_init_call_elem, |
| 192 | grpc_call_stack_ignore_set_pollset, |
| 193 | server_destroy_call_elem, |
| 194 | sizeof(channel_data), |
| 195 | init_channel_elem, |
| 196 | destroy_channel_elem, |
| 197 | grpc_call_next_get_peer, |
| 198 | "census-server"}; |