Skip to content

Commit 2a7f7da

Browse files
committed
Add service name to otel example.
1 parent b87de85 commit 2a7f7da

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

examples/hello-cpp-server-otel/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <boost/beast/core.hpp>
22
#include <boost/beast/http.hpp>
3-
#include <boost/beast/version.hpp>
43
#include <boost/asio/ip/tcp.hpp>
54
#include <iostream>
65
#include <memory>
@@ -58,8 +57,9 @@ void InitTracer(char const* sdk_key) {
5857
opts.url = "https://otel.observability.app.launchdarkly.com:4318/v1/traces";
5958
}
6059

61-
// Create resource with highlight.project_id attribute
60+
// Create resource with service name and highlight.project_id attribute
6261
auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes{
62+
{"service.name", "weather-server"},
6363
{"highlight.project_id", sdk_key}
6464
};
6565
auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
@@ -171,6 +171,7 @@ class session : public std::enable_shared_from_this<session> {
171171
tcp::socket socket_;
172172
beast::flat_buffer buffer_;
173173
http::request<http::string_body> req_;
174+
std::shared_ptr<http::response<http::string_body>> res_;
174175
std::shared_ptr<launchdarkly::server_side::Client> ld_client_;
175176

176177
public:
@@ -197,11 +198,11 @@ class session : public std::enable_shared_from_this<session> {
197198

198199
void do_write() {
199200
auto self = shared_from_this();
200-
auto res = std::make_shared<http::response<http::string_body>>(
201+
res_ = std::make_shared<http::response<http::string_body>>(
201202
handle_request(std::move(req_), ld_client_));
202203

203-
http::async_write(socket_, *res,
204-
[self, res](beast::error_code ec, std::size_t) {
204+
http::async_write(socket_, *res_,
205+
[self](beast::error_code ec, std::size_t) {
205206
self->socket_.shutdown(
206207
tcp::socket::shutdown_send, ec);
207208
});
@@ -356,4 +357,4 @@ char const* get_with_env_fallback(char const* source_val,
356357

357358
std::cout << "*** " << error_msg << std::endl;
358359
std::exit(1);
359-
}
360+
}

0 commit comments

Comments
 (0)