• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2024 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 // Explicitly define HAVE_ABSEIL to avoid conflict with OTel's Abseil
20 // version. Refer
21 // https://github.com/open-telemetry/opentelemetry-cpp/issues/1042.
22 #ifndef HAVE_ABSEIL
23 #define HAVE_ABSEIL
24 #endif
25 
26 #include <grpcpp/grpcpp.h>
27 
28 #include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h"
29 #include "opentelemetry/sdk/metrics/view/meter_selector_factory.h"
30 #include "opentelemetry/sdk/metrics/view/view_factory.h"
31 
32 #ifdef BAZEL_BUILD
33 #include "examples/cpp/otel/codelab/util.h"
34 #include "examples/protos/helloworld.grpc.pb.h"
35 #else
36 #include "helloworld.grpc.pb.h"
37 #include "util.h"
38 #endif
39 
40 using grpc::CallbackServerContext;
41 using grpc::Channel;
42 using grpc::ClientContext;
43 using grpc::Server;
44 using grpc::ServerBuilder;
45 using grpc::ServerUnaryReactor;
46 using grpc::Status;
47 using helloworld::Greeter;
48 using helloworld::HelloReply;
49 using helloworld::HelloRequest;
50 
AddLatencyView(opentelemetry::sdk::metrics::MeterProvider * provider,const std::string & name,const std::string & unit)51 void AddLatencyView(opentelemetry::sdk::metrics::MeterProvider* provider,
52                     const std::string& name, const std::string& unit) {
53   auto histogram_config = std::make_shared<
54       opentelemetry::sdk::metrics::HistogramAggregationConfig>();
55   histogram_config->boundaries_ = {
56       0,     0.00001, 0.00005, 0.0001, 0.0003, 0.0006, 0.0008, 0.001, 0.002,
57       0.003, 0.004,   0.005,   0.006,  0.008,  0.01,   0.013,  0.016, 0.02,
58       0.025, 0.03,    0.04,    0.05,   0.065,  0.08,   0.1,    0.13,  0.16,
59       0.2,   0.25,    0.3,     0.4,    0.5,    0.65,   0.8,    1,     2,
60       5,     10,      20,      50,     100};
61   provider->AddView(
62       opentelemetry::sdk::metrics::InstrumentSelectorFactory::Create(
63           opentelemetry::sdk::metrics::InstrumentType::kHistogram, name, unit),
64       opentelemetry::sdk::metrics::MeterSelectorFactory::Create(
65           "grpc-c++", grpc::Version(), ""),
66       opentelemetry::sdk::metrics::ViewFactory::Create(
67           name, "", unit,
68           opentelemetry::sdk::metrics::AggregationType::kHistogram,
69           std::move(histogram_config)));
70 }
71