aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYash Tibrewal <yashkt@google.com>2024-05-24 12:04:12 -0700
committerGitHub <noreply@github.com>2024-05-24 12:04:12 -0700
commitbb99d4a69a8e236d4879ba913ea685be65b219c2 (patch)
tree75f3c8fda99ce412c7c7be6456507245b93f7c0f
parent31470bfe656e49c32c26a66c94983cf3960e9ce8 (diff)
downloadgrpc-grpc-bb99d4a69a8e236d4879ba913ea685be65b219c2.tar.gz
[OTel] Add latency view to example (#36601) (#36719)
Backport #36601 to v1.63. Needed to backport #36686
-rw-r--r--examples/cpp/otel/BUILD12
-rw-r--r--examples/cpp/otel/CMakeLists.txt9
-rw-r--r--examples/cpp/otel/greeter_callback_client.cc6
-rw-r--r--examples/cpp/otel/greeter_callback_server.cc6
-rw-r--r--examples/cpp/otel/util.cc50
-rw-r--r--examples/cpp/otel/util.h31
6 files changed, 114 insertions, 0 deletions
diff --git a/examples/cpp/otel/BUILD b/examples/cpp/otel/BUILD
index 0a9b75c546..561ce270df 100644
--- a/examples/cpp/otel/BUILD
+++ b/examples/cpp/otel/BUILD
@@ -14,11 +14,22 @@
licenses(["notice"])
+cc_library(
+ name = "util",
+ srcs = ["util.cc"],
+ hdrs = ["util.h"],
+ deps = [
+ "//:grpc++",
+ "@io_opentelemetry_cpp//sdk/src/metrics",
+ ],
+)
+
cc_binary(
name = "greeter_callback_client",
srcs = ["greeter_callback_client.cc"],
defines = ["BAZEL_BUILD"],
deps = [
+ "util",
"//:grpc++",
"//:grpcpp_otel_plugin",
"//examples/protos:helloworld_cc_grpc",
@@ -34,6 +45,7 @@ cc_binary(
srcs = ["greeter_callback_server.cc"],
defines = ["BAZEL_BUILD"],
deps = [
+ "util",
"//:grpc++",
"//:grpc++_reflection",
"//:grpcpp_otel_plugin",
diff --git a/examples/cpp/otel/CMakeLists.txt b/examples/cpp/otel/CMakeLists.txt
index 5a3ecaa176..e4028a214f 100644
--- a/examples/cpp/otel/CMakeLists.txt
+++ b/examples/cpp/otel/CMakeLists.txt
@@ -50,6 +50,7 @@ add_custom_command(
# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
+include_directories("${CMAKE_SOURCE_DIR}")
# hw_grpc_proto
add_library(hw_grpc_proto
@@ -62,6 +63,13 @@ target_link_libraries(hw_grpc_proto
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
+# util
+add_library(util
+ "util.cc")
+target_link_libraries(util
+ opentelemetry-cpp::metrics
+ ${_GRPC_GRPCPP})
+
# Targets greeter_callback_(client|server)
foreach(_target
greeter_callback_client greeter_callback_server)
@@ -75,5 +83,6 @@ foreach(_target
${_REFLECTION}
${_GRPC_GRPCPP}
gRPC::grpcpp_otel_plugin
+ util
${_PROTOBUF_LIBPROTOBUF})
endforeach()
diff --git a/examples/cpp/otel/greeter_callback_client.cc b/examples/cpp/otel/greeter_callback_client.cc
index 653d68971c..32a2049b4c 100644
--- a/examples/cpp/otel/greeter_callback_client.cc
+++ b/examples/cpp/otel/greeter_callback_client.cc
@@ -33,9 +33,11 @@
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
+#include "examples/cpp/otel/util.h"
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
+#include "util.h"
#endif
ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
@@ -110,6 +112,10 @@ int main(int argc, char** argv) {
opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
auto meter_provider =
std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
+ // The default histogram boundaries are not granular enough for RPCs. Override
+ // the "grpc.client.attempt.duration" view as recommended by
+ // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
+ AddLatencyView(meter_provider.get(), "grpc.client.attempt.duration", "s");
meter_provider->AddMetricReader(std::move(prometheus_exporter));
auto status = grpc::OpenTelemetryPluginBuilder()
.SetMeterProvider(std::move(meter_provider))
diff --git a/examples/cpp/otel/greeter_callback_server.cc b/examples/cpp/otel/greeter_callback_server.cc
index 0e5f1a1b9e..8e7e7cda05 100644
--- a/examples/cpp/otel/greeter_callback_server.cc
+++ b/examples/cpp/otel/greeter_callback_server.cc
@@ -33,9 +33,11 @@
#include <grpcpp/health_check_service_interface.h>
#ifdef BAZEL_BUILD
+#include "examples/cpp/otel/util.h"
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
+#include "util.h"
#endif
ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
@@ -96,6 +98,10 @@ int main(int argc, char** argv) {
opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
auto meter_provider =
std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
+ // The default histogram boundaries are not granular enough for RPCs. Override
+ // the "grpc.server.call.duration" view as recommended by
+ // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
+ AddLatencyView(meter_provider.get(), "grpc.server.call.duration", "s");
meter_provider->AddMetricReader(std::move(prometheus_exporter));
auto status = grpc::OpenTelemetryPluginBuilder()
.SetMeterProvider(std::move(meter_provider))
diff --git a/examples/cpp/otel/util.cc b/examples/cpp/otel/util.cc
new file mode 100644
index 0000000000..8777481ea0
--- /dev/null
+++ b/examples/cpp/otel/util.cc
@@ -0,0 +1,50 @@
+//
+//
+// Copyright 2024 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+#ifdef BAZEL_BUILD
+#include "examples/cpp/otel/util.h"
+#else
+#include "util.h"
+#endif
+
+#include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h"
+#include "opentelemetry/sdk/metrics/view/meter_selector_factory.h"
+#include "opentelemetry/sdk/metrics/view/view_factory.h"
+
+#include <grpcpp/grpcpp.h>
+
+void AddLatencyView(opentelemetry::sdk::metrics::MeterProvider* provider,
+ const std::string& name, const std::string& unit) {
+ auto histogram_config = std::make_shared<
+ opentelemetry::sdk::metrics::HistogramAggregationConfig>();
+ histogram_config->boundaries_ = {
+ 0, 0.00001, 0.00005, 0.0001, 0.0003, 0.0006, 0.0008, 0.001, 0.002,
+ 0.003, 0.004, 0.005, 0.006, 0.008, 0.01, 0.013, 0.016, 0.02,
+ 0.025, 0.03, 0.04, 0.05, 0.065, 0.08, 0.1, 0.13, 0.16,
+ 0.2, 0.25, 0.3, 0.4, 0.5, 0.65, 0.8, 1, 2,
+ 5, 10, 20, 50, 100};
+ provider->AddView(
+ opentelemetry::sdk::metrics::InstrumentSelectorFactory::Create(
+ opentelemetry::sdk::metrics::InstrumentType::kHistogram, name, unit),
+ opentelemetry::sdk::metrics::MeterSelectorFactory::Create(
+ "grpc-c++", grpc::Version(), ""),
+ opentelemetry::sdk::metrics::ViewFactory::Create(
+ name, "", unit,
+ opentelemetry::sdk::metrics::AggregationType::kHistogram,
+ std::move(histogram_config)));
+}
diff --git a/examples/cpp/otel/util.h b/examples/cpp/otel/util.h
new file mode 100644
index 0000000000..4fe1681b4f
--- /dev/null
+++ b/examples/cpp/otel/util.h
@@ -0,0 +1,31 @@
+//
+//
+// Copyright 2024 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+#ifndef GRPCPP_EXAMPLES_CPP_OTEL_UTIL_H
+#define GRPCPP_EXAMPLES_CPP_OTEL_UTIL_H
+
+#include <string>
+
+#include "opentelemetry/sdk/metrics/meter_provider.h"
+
+// Helper function that adds view for gRPC latency instrument \a name with unit
+// \a unit with bucket boundaries that are more useful for RPCs.
+void AddLatencyView(opentelemetry::sdk::metrics::MeterProvider* provider,
+ const std::string& name, const std::string& unit);
+
+#endif // GRPCPP_EXAMPLES_CPP_OTEL_UTIL_H