forked from trpc-ecosystem/cpp-telemetry-opentelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_server.cc
More file actions
69 lines (55 loc) · 2.16 KB
/
Copy pathforward_server.cc
File metadata and controls
69 lines (55 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 Tencent.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include <memory>
#include <string>
#include "fmt/format.h"
#include "trpc/common/trpc_app.h"
#include "examples/proxy/forward_service.h"
#include "trpc/telemetry/opentelemetry/opentelemetry_telemetry_api.h"
namespace examples::forward {
void ServerTraceAttributes(const trpc::ServerContextPtr& context, const void* req,
std::unordered_map<std::string, std::string>& attributes) {
if (context->GetFuncName() == "/trpc.test.route.Forward/Route") {
auto hello_request = static_cast<const ::trpc::test::helloworld::HelloRequest*>(req);
if (hello_request->msg() == "force") {
attributes[::trpc::opentelemetry::kForceSampleKey] = "sample";
}
}
}
class ForwardServer : public ::trpc::TrpcApp {
public:
int Initialize() override {
const auto& config = ::trpc::TrpcConfig::GetInstance()->GetServerConfig();
// Set the service name, which must be the same as the value of the `server:service:name` configuration item
// in the framework configuration file, otherwise the framework cannot receive requests normally
std::string service_name = fmt::format("{}.{}.{}.{}", "trpc", config.app, config.server, "Forward");
TRPC_FMT_INFO("service name:{}", service_name);
RegisterService(service_name, std::make_shared<ForwardServiceImpl>());
return 0;
}
void Destroy() override {}
int RegisterPlugins() override {
// Initializes the OpenTelemetry plugin and filters in RegisterPlugins
::trpc::opentelemetry::Init();
// Sets the SetServerTraceAttrsFunc
::trpc::opentelemetry::SetServerTraceAttrsFunc(ServerTraceAttributes);
return 0;
}
};
} // namespace examples::forward
int main(int argc, char** argv) {
examples::forward::ForwardServer forward_server;
forward_server.Main(argc, argv);
forward_server.Wait();
return 0;
}