| 1 |
// |
| 2 |
// Copyright 2022 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
#include "absl/status/status.h" |
| 26 |
#include "absl/status/statusor.h" |
| 27 |
#include "absl/types/optional.h" |
| 28 |
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h" |
| 29 |
#include "google/monitoring/v3/metric_service.grpc.pb.h" |
| 30 |
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" |
| 31 |
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" |
| 32 |
#include "opencensus/stats/stats.h" |
| 33 |
#include "opencensus/trace/sampler.h" |
| 34 |
#include "opencensus/trace/trace_config.h" |
| 35 |
|
| 36 |
#include <grpcpp/ext/gcp_observability.h> |
| 37 |
#include <grpcpp/opencensus.h> |
| 38 |
#include <grpcpp/security/credentials.h> |
| 39 |
#include <grpcpp/support/channel_arguments.h> |
| 40 |
|
| 41 |
#include "src/cpp/ext/filters/census/grpc_plugin.h" |
| 42 |
#include "src/cpp/ext/filters/census/open_census_call_tracer.h" |
| 43 |
#include "src/cpp/ext/gcp/observability_config.h" |
| 44 |
|
| 45 |
namespace grpc { |
| 46 |
namespace experimental { |
| 47 |
|
| 48 |
namespace { |
| 49 |
// TODO(yashykt): These constants are currently derived from the example at |
| 50 |
// https://cloud.google.com/traffic-director/docs/observability-proxyless#c++. |
| 51 |
// We might want these to be configurable. |
| 52 |
constexpr uint32_t kMaxAttributes = 128; |
| 53 |
constexpr uint32_t kMaxAnnotations = 128; |
| 54 |
constexpr uint32_t kMaxMessageEvents = 128; |
| 55 |
constexpr uint32_t kMaxLinks = 128; |
| 56 |
|
| 57 |
constexpr char kGoogleStackdriverTraceAddress[] = "cloudtrace.googleapis.com"; |
| 58 |
constexpr char kGoogleStackdriverStatsAddress[] = "monitoring.googleapis.com"; |
| 59 |
|
| 60 |
void RegisterOpenCensusViewsForGcpObservability() { |
| 61 |
// Register client default views for GCP observability |
| 62 |
ClientStartedRpcsCumulative().RegisterForExport(); |
| 63 |
ClientCompletedRpcsCumulative().RegisterForExport(); |
| 64 |
// Register server default views for GCP observability |
| 65 |
ServerStartedRpcsCumulative().RegisterForExport(); |
| 66 |
ServerCompletedRpcsCumulative().RegisterForExport(); |
| 67 |
} |
| 68 |
|
| 69 |
} // namespace |
| 70 |
|
| 71 |
absl::Status GcpObservabilityInit() { |
| 72 |
auto config = grpc::internal::GcpObservabilityConfig::ReadFromEnv(); |
| 73 |
if (!config.ok()) { |
| 74 |
return config.status(); |
| 75 |
} |
| 76 |
if (!config->cloud_trace.has_value() && |
| 77 |
!config->cloud_monitoring.has_value()) { |
| 78 |
return absl::OkStatus(); |
| 79 |
} |
| 80 |
grpc::RegisterOpenCensusPlugin(); |
| 81 |
RegisterOpenCensusViewsForGcpObservability(); |
| 82 |
ChannelArguments args; |
| 83 |
args.SetInt(GRPC_ARG_ENABLE_OBSERVABILITY, 0); |
| 84 |
if (config->cloud_trace.has_value()) { |
| 85 |
opencensus::trace::TraceConfig::SetCurrentTraceParams( |
| 86 |
{kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks, |
| 87 |
opencensus::trace::ProbabilitySampler( |
| 88 |
config->cloud_trace->sampling_rate)}); |
| 89 |
opencensus::exporters::trace::StackdriverOptions trace_opts; |
| 90 |
trace_opts.project_id = config->project_id; |
| 91 |
trace_opts.trace_service_stub = |
| 92 |
::google::devtools::cloudtrace::v2::TraceService::NewStub( |
| 93 |
CreateCustomChannel(kGoogleStackdriverTraceAddress, |
| 94 |
GoogleDefaultCredentials(), args)); |
| 95 |
opencensus::exporters::trace::StackdriverExporter::Register( |
| 96 |
std::move(trace_opts)); |
| 97 |
} else { |
| 98 |
// Disable OpenCensus tracing |
| 99 |
EnableOpenCensusTracing(false); |
| 100 |
} |
| 101 |
if (config->cloud_monitoring.has_value()) { |
| 102 |
opencensus::exporters::stats::StackdriverOptions stats_opts; |
| 103 |
stats_opts.project_id = config->project_id; |
| 104 |
stats_opts.metric_service_stub = |
| 105 |
google::monitoring::v3::MetricService::NewStub(CreateCustomChannel( |
| 106 |
kGoogleStackdriverStatsAddress, GoogleDefaultCredentials(), args)); |
| 107 |
opencensus::exporters::stats::StackdriverExporter::Register( |
| 108 |
std::move(stats_opts)); |
| 109 |
} else { |
| 110 |
// Disable OpenCensus stats |
| 111 |
EnableOpenCensusStats(false); |
| 112 |
} |
| 113 |
return absl::OkStatus(); |
| 114 |
} |
| 115 |
|
| 116 |
} // namespace experimental |
| 117 |
} // namespace grpc |