blob: d6c51223bcd25caad81a7092b83d5de9e664dd81 [file] [log] [blame]
Craig Tiller178edfa2016-02-17 20:54:46 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller178edfa2016-02-17 20:54:46 -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 Tiller178edfa2016-02-17 20:54:46 -080034#include <limits.h>
Craig Tillerf82ddc42016-04-05 17:15:07 -070035#include <string.h>
Craig Tiller178edfa2016-02-17 20:54:46 -080036
37#include <grpc/census.h>
38
Craig Tiller8f8e9f92016-03-29 09:41:28 -070039#include "src/core/ext/census/grpc_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/channel/channel_stack_builder.h"
41#include "src/core/lib/surface/channel_init.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080042
Craig Tillerd1697d92016-04-05 16:05:46 -070043static bool is_census_enabled(const grpc_channel_args *a) {
44 size_t i;
45 if (a == NULL) return 0;
46 for (i = 0; i < a->num_args; i++) {
47 if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_CENSUS)) {
48 return a->args[i].value.integer != 0 && census_enabled();
49 }
50 }
51 return census_enabled();
52}
53
Craig Tiller178edfa2016-02-17 20:54:46 -080054static bool maybe_add_census_filter(grpc_channel_stack_builder *builder,
55 void *arg_must_be_null) {
56 const grpc_channel_args *args =
57 grpc_channel_stack_builder_get_channel_arguments(builder);
Craig Tillerd1697d92016-04-05 16:05:46 -070058 if (is_census_enabled(args)) {
Craig Tiller178edfa2016-02-17 20:54:46 -080059 return grpc_channel_stack_builder_prepend_filter(
60 builder, &grpc_client_census_filter, NULL, NULL);
61 }
62 return true;
63}
64
65void census_grpc_plugin_init(void) {
66 /* Only initialize census if no one else has and some features are
67 * available. */
68 if (census_enabled() == CENSUS_FEATURE_NONE &&
69 census_supported() != CENSUS_FEATURE_NONE) {
70 if (census_initialize(census_supported())) { /* enable all features. */
71 gpr_log(GPR_ERROR, "Could not initialize census.");
72 }
73 }
74 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
75 maybe_add_census_filter, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080076 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
77 maybe_add_census_filter, NULL);
78}
79
Craig Tiller8f8e9f92016-03-29 09:41:28 -070080void census_grpc_plugin_shutdown(void) { census_shutdown(); }