blob: 11120a28d194405df44fa4791536e71e2fe9946c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, 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
Hongyu Chene09dc782015-08-21 11:28:33 -070034#include "src/core/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 Tillered43f512015-11-19 08:53:23 -080045#include "src/core/channel/channel_stack.h"
Craig Tillered43f512015-11-19 08:53:23 -080046#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 Tillera82950e2015-09-22 12:33:20 -070050typedef struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051 census_op_id op_id;
Craig Tiller45724b32015-09-22 10:42:19 -070052 census_context *ctxt;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053 gpr_timespec start_ts;
Hongyu Chen3c9b8732015-08-13 17:01:02 -070054 int error;
Craig Tiller83f88d92015-04-21 16:02:05 -070055
56 /* recv callback */
Craig Tiller577c9b22015-11-02 14:11:15 -080057 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller45724b32015-09-22 10:42:19 -070058 grpc_closure *on_done_recv;
Craig Tiller577c9b22015-11-02 14:11:15 -080059 grpc_closure finish_recv;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060} call_data;
61
Craig Tiller7536af02015-12-22 13:49:30 -080062typedef struct channel_data { uint8_t unused; } channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tiller577c9b22015-11-02 14:11:15 -080064static void extract_and_annotate_method_tag(grpc_metadata_batch *md,
Craig Tillera82950e2015-09-22 12:33:20 -070065 call_data *calld,
66 channel_data *chand) {
Craig Tiller45724b32015-09-22 10:42:19 -070067 grpc_linked_mdelem *m;
Craig Tiller577c9b22015-11-02 14:11:15 -080068 for (m = md->list.head; m != NULL; m = m->next) {
Craig Tillered43f512015-11-19 08:53:23 -080069 if (m->md->key == GRPC_MDSTR_PATH) {
Craig Tiller577c9b22015-11-02 14:11:15 -080070 gpr_log(GPR_DEBUG, "%s",
71 (const char *)GPR_SLICE_START_PTR(m->md->value->slice));
72 /* Add method tag here */
Craig Tiller6902ad22015-04-16 08:01:49 -070073 }
Craig Tillera82950e2015-09-22 12:33:20 -070074 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075}
76
Craig Tillera82950e2015-09-22 12:33:20 -070077static void client_mutate_op(grpc_call_element *elem,
78 grpc_transport_stream_op *op) {
Craig Tiller45724b32015-09-22 10:42:19 -070079 call_data *calld = elem->call_data;
80 channel_data *chand = elem->channel_data;
Craig Tiller577c9b22015-11-02 14:11:15 -080081 if (op->send_initial_metadata) {
82 extract_and_annotate_method_tag(op->send_initial_metadata, calld, chand);
Craig Tillera82950e2015-09-22 12:33:20 -070083 }
Craig Tiller3f2c2212015-04-23 07:56:33 -070084}
85
Craig Tillera82950e2015-09-22 12:33:20 -070086static 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 Nobleb7ebd3b2014-11-26 16:33:03 -080091}
92
Craig Tillera82950e2015-09-22 12:33:20 -070093static void server_on_done_recv(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tiller6c396862016-01-28 13:53:40 -080094 bool success) {
Craig Tiller45724b32015-09-22 10:42:19 -070095 grpc_call_element *elem = ptr;
96 call_data *calld = elem->call_data;
97 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -070098 if (success) {
Craig Tiller577c9b22015-11-02 14:11:15 -080099 extract_and_annotate_method_tag(calld->recv_initial_metadata, calld, chand);
Craig Tillera82950e2015-09-22 12:33:20 -0700100 }
101 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
Craig Tiller83f88d92015-04-21 16:02:05 -0700102}
103
Craig Tillera82950e2015-09-22 12:33:20 -0700104static void server_mutate_op(grpc_call_element *elem,
105 grpc_transport_stream_op *op) {
Craig Tiller45724b32015-09-22 10:42:19 -0700106 call_data *calld = elem->call_data;
Craig Tiller577c9b22015-11-02 14:11:15 -0800107 if (op->recv_initial_metadata) {
Craig Tillera82950e2015-09-22 12:33:20 -0700108 /* substitute our callback for the op callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800109 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800110 calld->on_done_recv = op->recv_initial_metadata_ready;
111 op->recv_initial_metadata_ready = &calld->finish_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700112 }
Craig Tiller3f2c2212015-04-23 07:56:33 -0700113}
114
Craig Tillera82950e2015-09-22 12:33:20 -0700115static void server_start_transport_op(grpc_exec_ctx *exec_ctx,
116 grpc_call_element *elem,
117 grpc_transport_stream_op *op) {
Craig Tiller7908f162015-12-10 11:31:49 -0800118 /* 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 Tiller5a95c342015-12-10 10:59:38 -0800122 GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); */
Craig Tillera82950e2015-09-22 12:33:20 -0700123 server_mutate_op(elem, op);
124 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125}
126
Craig Tillera82950e2015-09-22 12:33:20 -0700127static void client_init_call_elem(grpc_exec_ctx *exec_ctx,
128 grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800129 grpc_call_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700130 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700131 GPR_ASSERT(d != NULL);
Hongyu Chen738e91e2015-10-13 15:05:00 -0700132 memset(d, 0, sizeof(*d));
Craig Tillera82950e2015-09-22 12:33:20 -0700133 d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134}
135
Craig Tillera82950e2015-09-22 12:33:20 -0700136static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
137 grpc_call_element *elem) {
Craig Tiller45724b32015-09-22 10:42:19 -0700138 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700139 GPR_ASSERT(d != NULL);
Hongyu Chen58c927c2015-08-14 15:19:13 -0700140 /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141}
142
Craig Tillera82950e2015-09-22 12:33:20 -0700143static void server_init_call_elem(grpc_exec_ctx *exec_ctx,
144 grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800145 grpc_call_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700146 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700147 GPR_ASSERT(d != NULL);
Hongyu Chen738e91e2015-10-13 15:05:00 -0700148 memset(d, 0, sizeof(*d));
Craig Tillera82950e2015-09-22 12:33:20 -0700149 d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
Hongyu Chen3c9b8732015-08-13 17:01:02 -0700150 /* TODO(hongyu): call census_tracing_start_op here. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800151 grpc_closure_init(&d->finish_recv, server_on_done_recv, elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152}
153
Craig Tillera82950e2015-09-22 12:33:20 -0700154static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
155 grpc_call_element *elem) {
Craig Tiller45724b32015-09-22 10:42:19 -0700156 call_data *d = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700157 GPR_ASSERT(d != NULL);
Hongyu Chen58c927c2015-08-14 15:19:13 -0700158 /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159}
160
Craig Tillera82950e2015-09-22 12:33:20 -0700161static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800162 grpc_channel_element *elem,
163 grpc_channel_element_args *args) {
Craig Tiller45724b32015-09-22 10:42:19 -0700164 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700165 GPR_ASSERT(chand != NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166}
167
Craig Tillera82950e2015-09-22 12:33:20 -0700168static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
169 grpc_channel_element *elem) {
Craig Tiller45724b32015-09-22 10:42:19 -0700170 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700171 GPR_ASSERT(chand != NULL);
hongyu24200d32015-01-08 15:13:49 -0800172}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173
174const grpc_channel_filter grpc_client_census_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700175 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800186
187const grpc_channel_filter grpc_server_census_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700188 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"};