blob: 9dacc17eb4af954b61ce971e0b90a25a816ef552 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tiller8f8e9f92016-03-29 09:41:28 -070034#include "src/core/ext/census/grpc_filter.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <stdio.h>
37#include <string.h>
38
murgatroid9986854592015-08-27 10:27:19 -070039#include <grpc/census.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#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 Tiller8f8e9f92016-03-29 09:41:28 -070045#include "src/core/ext/census/census_interface.h"
46#include "src/core/ext/census/census_rpc_stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/channel/channel_stack.h"
Craig Tillerbfc9adc2016-06-27 13:16:22 -070048#include "src/core/lib/profiling/timers.h"
Craig Tiller9533d042016-03-25 17:11:06 -070049#include "src/core/lib/transport/static_metadata.h"
Craig Tillered43f512015-11-19 08:53:23 -080050
Craig Tillera82950e2015-09-22 12:33:20 -070051typedef struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052 census_op_id op_id;
Craig Tiller45724b32015-09-22 10:42:19 -070053 census_context *ctxt;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054 gpr_timespec start_ts;
Hongyu Chen3c9b8732015-08-13 17:01:02 -070055 int error;
Craig Tiller83f88d92015-04-21 16:02:05 -070056
57 /* recv callback */
Craig Tiller577c9b22015-11-02 14:11:15 -080058 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller45724b32015-09-22 10:42:19 -070059 grpc_closure *on_done_recv;
Craig Tiller577c9b22015-11-02 14:11:15 -080060 grpc_closure finish_recv;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061} call_data;
62
Craig Tiller7536af02015-12-22 13:49:30 -080063typedef struct channel_data { uint8_t unused; } channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
Craig Tiller577c9b22015-11-02 14:11:15 -080065static void extract_and_annotate_method_tag(grpc_metadata_batch *md,
Craig Tillera82950e2015-09-22 12:33:20 -070066 call_data *calld,
67 channel_data *chand) {
Craig Tiller45724b32015-09-22 10:42:19 -070068 grpc_linked_mdelem *m;
Craig Tiller577c9b22015-11-02 14:11:15 -080069 for (m = md->list.head; m != NULL; m = m->next) {
Craig Tillered43f512015-11-19 08:53:23 -080070 if (m->md->key == GRPC_MDSTR_PATH) {
Craig Tiller577c9b22015-11-02 14:11:15 -080071 gpr_log(GPR_DEBUG, "%s",
72 (const char *)GPR_SLICE_START_PTR(m->md->value->slice));
73 /* Add method tag here */
Craig Tiller6902ad22015-04-16 08:01:49 -070074 }
Craig Tillera82950e2015-09-22 12:33:20 -070075 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076}
77
Craig Tillera82950e2015-09-22 12:33:20 -070078static void client_mutate_op(grpc_call_element *elem,
79 grpc_transport_stream_op *op) {
Craig Tiller45724b32015-09-22 10:42:19 -070080 call_data *calld = elem->call_data;
81 channel_data *chand = elem->channel_data;
Craig Tiller577c9b22015-11-02 14:11:15 -080082 if (op->send_initial_metadata) {
83 extract_and_annotate_method_tag(op->send_initial_metadata, calld, chand);
Craig Tillera82950e2015-09-22 12:33:20 -070084 }
Craig Tiller3f2c2212015-04-23 07:56:33 -070085}
86
Craig Tillera82950e2015-09-22 12:33:20 -070087static void client_start_transport_op(grpc_exec_ctx *exec_ctx,
88 grpc_call_element *elem,
89 grpc_transport_stream_op *op) {
90 client_mutate_op(elem, op);
91 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092}
93
Craig Tillera82950e2015-09-22 12:33:20 -070094static void server_on_done_recv(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tiller804ff712016-05-05 16:25:40 -070095 grpc_error *error) {
Craig Tillerbfc9adc2016-06-27 13:16:22 -070096 GPR_TIMER_BEGIN("census-server:server_on_done_recv", 0);
Craig Tiller45724b32015-09-22 10:42:19 -070097 grpc_call_element *elem = ptr;
98 call_data *calld = elem->call_data;
99 channel_data *chand = elem->channel_data;
Craig Tiller804ff712016-05-05 16:25:40 -0700100 if (error == GRPC_ERROR_NONE) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800101 extract_and_annotate_method_tag(calld->recv_initial_metadata, calld, chand);
Craig Tillera82950e2015-09-22 12:33:20 -0700102 }
Craig Tiller804ff712016-05-05 16:25:40 -0700103 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, error);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700104 GPR_TIMER_END("census-server:server_on_done_recv", 0);
Craig Tiller83f88d92015-04-21 16:02:05 -0700105}
106
Craig Tillera82950e2015-09-22 12:33:20 -0700107static void server_mutate_op(grpc_call_element *elem,
108 grpc_transport_stream_op *op) {
Craig Tiller45724b32015-09-22 10:42:19 -0700109 call_data *calld = elem->call_data;
Craig Tiller577c9b22015-11-02 14:11:15 -0800110 if (op->recv_initial_metadata) {
Craig Tillera82950e2015-09-22 12:33:20 -0700111 /* substitute our callback for the op callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800112 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800113 calld->on_done_recv = op->recv_initial_metadata_ready;
114 op->recv_initial_metadata_ready = &calld->finish_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700115 }
Craig Tiller3f2c2212015-04-23 07:56:33 -0700116}
117
Craig Tillera82950e2015-09-22 12:33:20 -0700118static void server_start_transport_op(grpc_exec_ctx *exec_ctx,
119 grpc_call_element *elem,
120 grpc_transport_stream_op *op) {
Craig Tiller7908f162015-12-10 11:31:49 -0800121 /* TODO(ctiller): this code fails. I don't know why. I expect it's
122 incomplete, and someone should look at it soon.
123
124 call_data *calld = elem->call_data;
Craig Tiller5a95c342015-12-10 10:59:38 -0800125 GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); */
Craig Tillera82950e2015-09-22 12:33:20 -0700126 server_mutate_op(elem, op);
127 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128}
129
Mark D. Roth76d24422016-06-23 13:22:10 -0700130static grpc_error *client_init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700131 grpc_call_element *elem,
132 grpc_call_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700133 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700134 GPR_ASSERT(d != NULL);
Hongyu Chen738e91e2015-10-13 15:05:00 -0700135 memset(d, 0, sizeof(*d));
Craig Tillera82950e2015-09-22 12:33:20 -0700136 d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700137 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138}
139
Craig Tillera82950e2015-09-22 12:33:20 -0700140static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
David Garcia Quintas9ef0e1c2016-04-14 12:44:30 -0700141 grpc_call_element *elem,
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700142 const grpc_call_final_info *final_info,
David Garcia Quintas580987a2016-04-29 17:26:33 -0700143 void *ignored) {
Craig Tiller45724b32015-09-22 10:42:19 -0700144 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700145 GPR_ASSERT(d != NULL);
Hongyu Chen58c927c2015-08-14 15:19:13 -0700146 /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147}
148
Mark D. Roth76d24422016-06-23 13:22:10 -0700149static grpc_error *server_init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700150 grpc_call_element *elem,
151 grpc_call_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700152 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700153 GPR_ASSERT(d != NULL);
Hongyu Chen738e91e2015-10-13 15:05:00 -0700154 memset(d, 0, sizeof(*d));
Craig Tillera82950e2015-09-22 12:33:20 -0700155 d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
Hongyu Chen3c9b8732015-08-13 17:01:02 -0700156 /* TODO(hongyu): call census_tracing_start_op here. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800157 grpc_closure_init(&d->finish_recv, server_on_done_recv, elem);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700158 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159}
160
Craig Tillera82950e2015-09-22 12:33:20 -0700161static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
David Garcia Quintas9ef0e1c2016-04-14 12:44:30 -0700162 grpc_call_element *elem,
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700163 const grpc_call_final_info *final_info,
David Garcia Quintas580987a2016-04-29 17:26:33 -0700164 void *ignored) {
Craig Tiller45724b32015-09-22 10:42:19 -0700165 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700166 GPR_ASSERT(d != NULL);
Hongyu Chen58c927c2015-08-14 15:19:13 -0700167 /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168}
169
Craig Tillera82950e2015-09-22 12:33:20 -0700170static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800171 grpc_channel_element *elem,
172 grpc_channel_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700173 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700174 GPR_ASSERT(chand != NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175}
176
Craig Tillera82950e2015-09-22 12:33:20 -0700177static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
178 grpc_channel_element *elem) {
Craig Tiller45724b32015-09-22 10:42:19 -0700179 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700180 GPR_ASSERT(chand != NULL);
hongyu24200d32015-01-08 15:13:49 -0800181}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182
183const grpc_channel_filter grpc_client_census_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700184 client_start_transport_op,
185 grpc_channel_next_op,
186 sizeof(call_data),
187 client_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700188 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700189 client_destroy_call_elem,
190 sizeof(channel_data),
191 init_channel_elem,
192 destroy_channel_elem,
193 grpc_call_next_get_peer,
194 "census-client"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195
196const grpc_channel_filter grpc_server_census_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700197 server_start_transport_op,
198 grpc_channel_next_op,
199 sizeof(call_data),
200 server_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700201 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700202 server_destroy_call_elem,
203 sizeof(channel_data),
204 init_channel_elem,
205 destroy_channel_elem,
206 grpc_call_next_get_peer,
207 "census-server"};