Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -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 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 34 | #include <limits.h> |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 35 | #include <string.h> |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 36 | |
| 37 | #include <grpc/census.h> |
| 38 | |
Craig Tiller | 8f8e9f9 | 2016-03-29 09:41:28 -0700 | [diff] [blame] | 39 | #include "src/core/ext/census/grpc_filter.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 40 | #include "src/core/lib/channel/channel_stack_builder.h" |
| 41 | #include "src/core/lib/surface/channel_init.h" |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 42 | |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 43 | static 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 Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 54 | static 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 Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 58 | if (is_census_enabled(args)) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 59 | return grpc_channel_stack_builder_prepend_filter( |
| 60 | builder, &grpc_client_census_filter, NULL, NULL); |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void 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 Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 76 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, |
| 77 | maybe_add_census_filter, NULL); |
| 78 | } |
| 79 | |
Craig Tiller | 8f8e9f9 | 2016-03-29 09:41:28 -0700 | [diff] [blame] | 80 | void census_grpc_plugin_shutdown(void) { census_shutdown(); } |