Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
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 :
34 : #include "src/core/census/grpc_filter.h"
35 :
36 : #include <stdio.h>
37 : #include <string.h>
38 :
39 : #include <grpc/census.h>
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 :
45 : #include "src/core/channel/channel_stack.h"
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 :
50 : typedef struct call_data {
51 : census_op_id op_id;
52 : census_context *ctxt;
53 : gpr_timespec start_ts;
54 : int error;
55 :
56 : /* recv callback */
57 : grpc_metadata_batch *recv_initial_metadata;
58 : grpc_closure *on_done_recv;
59 : grpc_closure finish_recv;
60 : } call_data;
61 :
62 : typedef struct channel_data { gpr_uint8 unused; } channel_data;
63 :
64 19 : static void extract_and_annotate_method_tag(grpc_metadata_batch *md,
65 : call_data *calld,
66 : channel_data *chand) {
67 : grpc_linked_mdelem *m;
68 57 : for (m = md->list.head; m != NULL; m = m->next) {
69 38 : if (m->md->key == GRPC_MDSTR_PATH) {
70 38 : gpr_log(GPR_DEBUG, "%s",
71 38 : (const char *)GPR_SLICE_START_PTR(m->md->value->slice));
72 : /* Add method tag here */
73 : }
74 : }
75 19 : }
76 :
77 19 : static void client_mutate_op(grpc_call_element *elem,
78 : grpc_transport_stream_op *op) {
79 19 : call_data *calld = elem->call_data;
80 19 : channel_data *chand = elem->channel_data;
81 19 : if (op->send_initial_metadata) {
82 19 : extract_and_annotate_method_tag(op->send_initial_metadata, calld, chand);
83 : }
84 19 : }
85 :
86 19 : static void client_start_transport_op(grpc_exec_ctx *exec_ctx,
87 : grpc_call_element *elem,
88 : grpc_transport_stream_op *op) {
89 19 : client_mutate_op(elem, op);
90 19 : grpc_call_next_op(exec_ctx, elem, op);
91 19 : }
92 :
93 0 : static void server_on_done_recv(grpc_exec_ctx *exec_ctx, void *ptr,
94 : int success) {
95 0 : grpc_call_element *elem = ptr;
96 0 : call_data *calld = elem->call_data;
97 0 : channel_data *chand = elem->channel_data;
98 0 : if (success) {
99 0 : extract_and_annotate_method_tag(calld->recv_initial_metadata, calld, chand);
100 : }
101 0 : calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
102 0 : }
103 :
104 0 : static void server_mutate_op(grpc_call_element *elem,
105 : grpc_transport_stream_op *op) {
106 0 : call_data *calld = elem->call_data;
107 0 : if (op->recv_initial_metadata) {
108 : /* substitute our callback for the op callback */
109 0 : calld->recv_initial_metadata = op->recv_initial_metadata;
110 0 : calld->on_done_recv = op->on_complete;
111 0 : op->on_complete = &calld->finish_recv;
112 : }
113 0 : }
114 :
115 0 : static void server_start_transport_op(grpc_exec_ctx *exec_ctx,
116 : grpc_call_element *elem,
117 : grpc_transport_stream_op *op) {
118 0 : call_data *calld = elem->call_data;
119 0 : GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0));
120 0 : server_mutate_op(elem, op);
121 0 : grpc_call_next_op(exec_ctx, elem, op);
122 0 : }
123 :
124 19 : static void client_init_call_elem(grpc_exec_ctx *exec_ctx,
125 : grpc_call_element *elem,
126 : grpc_call_element_args *args) {
127 19 : call_data *d = elem->call_data;
128 19 : GPR_ASSERT(d != NULL);
129 19 : memset(d, 0, sizeof(*d));
130 19 : d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
131 19 : }
132 :
133 19 : static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
134 : grpc_call_element *elem) {
135 19 : call_data *d = elem->call_data;
136 19 : GPR_ASSERT(d != NULL);
137 : /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */
138 19 : }
139 :
140 0 : static void server_init_call_elem(grpc_exec_ctx *exec_ctx,
141 : grpc_call_element *elem,
142 : grpc_call_element_args *args) {
143 0 : call_data *d = elem->call_data;
144 0 : GPR_ASSERT(d != NULL);
145 0 : memset(d, 0, sizeof(*d));
146 0 : d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
147 : /* TODO(hongyu): call census_tracing_start_op here. */
148 0 : grpc_closure_init(&d->finish_recv, server_on_done_recv, elem);
149 0 : }
150 :
151 0 : static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
152 : grpc_call_element *elem) {
153 0 : call_data *d = elem->call_data;
154 0 : GPR_ASSERT(d != NULL);
155 : /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */
156 0 : }
157 :
158 19 : static void init_channel_elem(grpc_exec_ctx *exec_ctx,
159 : grpc_channel_element *elem,
160 : grpc_channel_element_args *args) {
161 19 : channel_data *chand = elem->channel_data;
162 19 : GPR_ASSERT(chand != NULL);
163 19 : }
164 :
165 19 : static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
166 : grpc_channel_element *elem) {
167 19 : channel_data *chand = elem->channel_data;
168 19 : GPR_ASSERT(chand != NULL);
169 19 : }
170 :
171 : const grpc_channel_filter grpc_client_census_filter = {
172 : client_start_transport_op, grpc_channel_next_op, sizeof(call_data),
173 : client_init_call_elem, grpc_call_stack_ignore_set_pollset,
174 : client_destroy_call_elem, sizeof(channel_data), init_channel_elem,
175 : destroy_channel_elem, grpc_call_next_get_peer, "census-client"};
176 :
177 : const grpc_channel_filter grpc_server_census_filter = {
178 : server_start_transport_op, grpc_channel_next_op, sizeof(call_data),
179 : server_init_call_elem, grpc_call_stack_ignore_set_pollset,
180 : server_destroy_call_elem, sizeof(channel_data), init_channel_elem,
181 : destroy_channel_elem, grpc_call_next_get_peer, "census-server"};
|