mirror of https://github.com/grpc/grpc.git
commit
53077db9e1
169 changed files with 2206 additions and 1356 deletions
@ -0,0 +1,47 @@ |
||||
# Copyright 2023 the 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. |
||||
|
||||
licenses(["notice"]) |
||||
|
||||
cc_binary( |
||||
name = "greeter_callback_client", |
||||
srcs = ["greeter_callback_client.cc"], |
||||
defines = ["BAZEL_BUILD"], |
||||
deps = [ |
||||
"//:grpc++", |
||||
"//:grpcpp_otel_plugin", |
||||
"//examples/protos:helloworld_cc_grpc", |
||||
"@com_google_absl//absl/flags:flag", |
||||
"@com_google_absl//absl/flags:parse", |
||||
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_exporter", |
||||
"@io_opentelemetry_cpp//sdk/src/metrics", |
||||
], |
||||
) |
||||
|
||||
cc_binary( |
||||
name = "greeter_callback_server", |
||||
srcs = ["greeter_callback_server.cc"], |
||||
defines = ["BAZEL_BUILD"], |
||||
deps = [ |
||||
"//:grpc++", |
||||
"//:grpc++_reflection", |
||||
"//:grpcpp_otel_plugin", |
||||
"//examples/protos:helloworld_cc_grpc", |
||||
"@com_google_absl//absl/flags:flag", |
||||
"@com_google_absl//absl/flags:parse", |
||||
"@com_google_absl//absl/strings:str_format", |
||||
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_exporter", |
||||
"@io_opentelemetry_cpp//sdk/src/metrics", |
||||
], |
||||
) |
@ -0,0 +1,45 @@ |
||||
# gRPC C++ OpenTelemetry Example |
||||
|
||||
The opentelemetry example builds on the |
||||
[Hello World Example](https://github.com/grpc/grpc/tree/master/examples/cpp/helloworld) |
||||
and changes the gRPC client and server to show a sample way of configuring the |
||||
gRPC OpenTelemetry plugin with a prometheus exporter. |
||||
|
||||
For more information on the gRPC OpenTelemetry plugin, please refer to - * |
||||
[A66: OpenTelemetry Metrics](https://github.com/grpc/proposal/blob/master/A66-otel-stats.md) |
||||
* [https://opentelemetry.io/]() |
||||
|
||||
## Running the example |
||||
|
||||
To run the server - |
||||
|
||||
``` |
||||
$ tools/bazel run examples/cpp/otel:greeter_callback_server |
||||
``` |
||||
|
||||
To run the client - |
||||
|
||||
``` |
||||
$ tools/bazel run examples/cpp/otel:greeter_callback_client |
||||
``` |
||||
|
||||
The client continuously sends an RPC to the server every second. |
||||
|
||||
To make sure that the server and client metrics are being exported properly, in |
||||
a separate terminal, run the following - |
||||
|
||||
``` |
||||
$ curl localhost:9464/metrics |
||||
``` |
||||
|
||||
``` |
||||
$ curl localhost:9465/metrics |
||||
``` |
||||
|
||||
> ***NOTE:*** If the prometheus endpoint configured is overridden, please update |
||||
> the target in the above curl command. |
||||
|
||||
You can find a complete set of instructions for building gRPC and running the |
||||
Hello World app in the [C++ Quick Start][]. |
||||
|
||||
[C++ Quick Start]: https://grpc.io/docs/languages/cpp/quickstart |
@ -0,0 +1,138 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2021 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. |
||||
* |
||||
*/ |
||||
|
||||
#include <condition_variable> |
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <mutex> |
||||
#include <string> |
||||
#include <thread> |
||||
|
||||
#include "absl/flags/flag.h" |
||||
#include "absl/flags/parse.h" |
||||
#include "opentelemetry/exporters/prometheus/exporter_factory.h" |
||||
#include "opentelemetry/exporters/prometheus/exporter_options.h" |
||||
#include "opentelemetry/sdk/metrics/meter_provider.h" |
||||
|
||||
#include <grpcpp/ext/otel_plugin.h> |
||||
#include <grpcpp/grpcpp.h> |
||||
|
||||
#ifdef BAZEL_BUILD |
||||
#include "examples/protos/helloworld.grpc.pb.h" |
||||
#else |
||||
#include "helloworld.grpc.pb.h" |
||||
#endif |
||||
|
||||
ABSL_FLAG(std::string, target, "localhost:50051", "Server address"); |
||||
ABSL_FLAG(std::string, prometheus_endpoint, "localhost:9465", |
||||
"Prometheus exporter endpoint"); |
||||
|
||||
using grpc::Channel; |
||||
using grpc::ClientContext; |
||||
using grpc::Status; |
||||
using helloworld::Greeter; |
||||
using helloworld::HelloReply; |
||||
using helloworld::HelloRequest; |
||||
|
||||
class GreeterClient { |
||||
public: |
||||
GreeterClient(std::shared_ptr<Channel> channel) |
||||
: stub_(Greeter::NewStub(channel)) {} |
||||
|
||||
// Assembles the client's payload, sends it and presents the response back
|
||||
// from the server.
|
||||
std::string SayHello(const std::string& user) { |
||||
// Data we are sending to the server.
|
||||
HelloRequest request; |
||||
request.set_name(user); |
||||
|
||||
// Container for the data we expect from the server.
|
||||
HelloReply reply; |
||||
|
||||
// Context for the client. It could be used to convey extra information to
|
||||
// the server and/or tweak certain RPC behaviors.
|
||||
ClientContext context; |
||||
|
||||
// The actual RPC.
|
||||
std::mutex mu; |
||||
std::condition_variable cv; |
||||
bool done = false; |
||||
Status status; |
||||
stub_->async()->SayHello(&context, &request, &reply, |
||||
[&mu, &cv, &done, &status](Status s) { |
||||
status = std::move(s); |
||||
std::lock_guard<std::mutex> lock(mu); |
||||
done = true; |
||||
cv.notify_one(); |
||||
}); |
||||
|
||||
std::unique_lock<std::mutex> lock(mu); |
||||
while (!done) { |
||||
cv.wait(lock); |
||||
} |
||||
|
||||
// Act upon its status.
|
||||
if (status.ok()) { |
||||
return reply.message(); |
||||
} else { |
||||
std::cout << status.error_code() << ": " << status.error_message() |
||||
<< std::endl; |
||||
return "RPC failed"; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
std::unique_ptr<Greeter::Stub> stub_; |
||||
}; |
||||
|
||||
int main(int argc, char** argv) { |
||||
absl::ParseCommandLine(argc, argv); |
||||
// Register a global gRPC OpenTelemetry plugin configured with a prometheus
|
||||
// exporter.
|
||||
opentelemetry::exporter::metrics::PrometheusExporterOptions opts; |
||||
opts.url = absl::GetFlag(FLAGS_prometheus_endpoint); |
||||
auto prometheus_exporter = |
||||
opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts); |
||||
auto meter_provider = |
||||
std::make_shared<opentelemetry::sdk::metrics::MeterProvider>(); |
||||
meter_provider->AddMetricReader(std::move(prometheus_exporter)); |
||||
auto status = grpc::OpenTelemetryPluginBuilder() |
||||
.SetMeterProvider(std::move(meter_provider)) |
||||
.BuildAndRegisterGlobal(); |
||||
if (!status.ok()) { |
||||
std::cerr << "Failed to register gRPC OpenTelemetry Plugin: " |
||||
<< status.ToString() << std::endl; |
||||
return static_cast<int>(status.code()); |
||||
} |
||||
// Instantiate the client. It requires a channel, out of which the actual RPCs
|
||||
// are created. This channel models a connection to an endpoint specified by
|
||||
// the argument "--target=" which is the only expected argument.
|
||||
std::string target_str = absl::GetFlag(FLAGS_target); |
||||
grpc::ChannelArguments args; |
||||
// Continuously send RPCs every second.
|
||||
while (true) { |
||||
GreeterClient greeter(grpc::CreateCustomChannel( |
||||
target_str, grpc::InsecureChannelCredentials(), args)); |
||||
std::string user("world"); |
||||
std::string reply = greeter.SayHello(user); |
||||
std::cout << "Greeter received: " << reply << std::endl; |
||||
std::this_thread::sleep_for(std::chrono::seconds(1)); |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,110 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2021 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. |
||||
* |
||||
*/ |
||||
|
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include "absl/flags/flag.h" |
||||
#include "absl/flags/parse.h" |
||||
#include "absl/strings/str_format.h" |
||||
#include "opentelemetry/exporters/prometheus/exporter_factory.h" |
||||
#include "opentelemetry/exporters/prometheus/exporter_options.h" |
||||
#include "opentelemetry/sdk/metrics/meter_provider.h" |
||||
|
||||
#include <grpcpp/ext/otel_plugin.h> |
||||
#include <grpcpp/ext/proto_server_reflection_plugin.h> |
||||
#include <grpcpp/grpcpp.h> |
||||
#include <grpcpp/health_check_service_interface.h> |
||||
|
||||
#ifdef BAZEL_BUILD |
||||
#include "examples/protos/helloworld.grpc.pb.h" |
||||
#else |
||||
#include "helloworld.grpc.pb.h" |
||||
#endif |
||||
|
||||
ABSL_FLAG(uint16_t, port, 50051, "Server port for the service"); |
||||
ABSL_FLAG(std::string, prometheus_endpoint, "localhost:9464", |
||||
"Prometheus exporter endpoint"); |
||||
|
||||
using grpc::CallbackServerContext; |
||||
using grpc::Server; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerUnaryReactor; |
||||
using grpc::Status; |
||||
using helloworld::Greeter; |
||||
using helloworld::HelloReply; |
||||
using helloworld::HelloRequest; |
||||
|
||||
// Logic and data behind the server's behavior.
|
||||
class GreeterServiceImpl final : public Greeter::CallbackService { |
||||
ServerUnaryReactor* SayHello(CallbackServerContext* context, |
||||
const HelloRequest* request, |
||||
HelloReply* reply) override { |
||||
std::string prefix("Hello "); |
||||
reply->set_message(prefix + request->name()); |
||||
|
||||
ServerUnaryReactor* reactor = context->DefaultReactor(); |
||||
reactor->Finish(Status::OK); |
||||
return reactor; |
||||
} |
||||
}; |
||||
|
||||
void RunServer(uint16_t port) { |
||||
std::string server_address = absl::StrFormat("0.0.0.0:%d", port); |
||||
GreeterServiceImpl service; |
||||
|
||||
grpc::EnableDefaultHealthCheckService(true); |
||||
grpc::reflection::InitProtoReflectionServerBuilderPlugin(); |
||||
ServerBuilder builder; |
||||
// Listen on the given address without any authentication mechanism.
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
||||
// Register "service" as the instance through which we'll communicate with
|
||||
// clients. In this case it corresponds to an *synchronous* service.
|
||||
builder.RegisterService(&service); |
||||
// Finally assemble the server.
|
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
std::cout << "Server listening on " << server_address << std::endl; |
||||
|
||||
// Wait for the server to shutdown. Note that some other thread must be
|
||||
// responsible for shutting down the server for this call to ever return.
|
||||
server->Wait(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
absl::ParseCommandLine(argc, argv); |
||||
// Register a global gRPC OpenTelemetry plugin configured with a prometheus
|
||||
// exporter.
|
||||
opentelemetry::exporter::metrics::PrometheusExporterOptions opts; |
||||
opts.url = absl::GetFlag(FLAGS_prometheus_endpoint); |
||||
auto prometheus_exporter = |
||||
opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts); |
||||
auto meter_provider = |
||||
std::make_shared<opentelemetry::sdk::metrics::MeterProvider>(); |
||||
meter_provider->AddMetricReader(std::move(prometheus_exporter)); |
||||
auto status = grpc::OpenTelemetryPluginBuilder() |
||||
.SetMeterProvider(std::move(meter_provider)) |
||||
.BuildAndRegisterGlobal(); |
||||
if (!status.ok()) { |
||||
std::cerr << "Failed to register gRPC OpenTelemetry Plugin: " |
||||
<< status.ToString() << std::endl; |
||||
return static_cast<int>(status.code()); |
||||
} |
||||
RunServer(absl::GetFlag(FLAGS_port)); |
||||
return 0; |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue