@@ -32,7 +32,10 @@ SimpleOtlpHttpSender g_otlp_sender;
3232} // namespace
3333
3434SimpleOtlpHttpSender::SimpleOtlpHttpSender ()
35- : service_name_(" yugabyte" ), enabled_(false ) {}
35+ : enabled_(false ) {
36+ const char * service_name_env = std::getenv (" OTEL_SERVICE_NAME" );
37+ service_name_ = (service_name_env && service_name_env[0 ] != ' \0 ' ) ? service_name_env : " yugabyte" ;
38+ }
3639
3740SimpleOtlpHttpSender::~SimpleOtlpHttpSender () = default ;
3841
@@ -56,15 +59,10 @@ bool SimpleOtlpHttpSender::SendSpan(const SimpleSpanData& span) {
5659}
5760
5861bool SimpleOtlpHttpSender::SendSpans (const std::vector<SimpleSpanData>& spans) {
59- if (!IsEnabled ()) {
60- VLOG (2 ) << " [OTEL] HTTP sender not enabled, skipping export" ;
62+ if (!IsEnabled () || spans.empty ()) {
6163 return true ;
6264 }
6365
64- if (spans.empty ()) {
65- return true ; // Nothing to do
66- }
67-
6866 std::string json_payload = SpansToJson (spans);
6967
7068 LOG (INFO) << " [OTEL] Sending " << spans.size () << " span(s) to " << endpoint_;
@@ -124,7 +122,6 @@ std::string SimpleOtlpHttpSender::JsonEscape(const std::string& str) {
124122std::string SimpleOtlpHttpSender::SpansToJson (const std::vector<SimpleSpanData>& spans) const {
125123 std::ostringstream json;
126124
127- // OTLP JSON structure
128125 json << R"( {"resourceSpans":[{"resource":{"attributes":[)" ;
129126 json << R"( {"key":"service.name","value":{"stringValue":")" << JsonEscape (service_name_) << R"( "}},)" ;
130127 json << R"( {"key":"telemetry.sdk.language","value":{"stringValue":"cpp"}},)" ;
@@ -153,7 +150,6 @@ std::string SimpleOtlpHttpSender::SpansToJson(const std::vector<SimpleSpanData>&
153150 json << R"( "startTimeUnixNano":")" << span.start_time_ns << R"( ",)" ;
154151 json << R"( "endTimeUnixNano":")" << span.end_time_ns << R"( ",)" ;
155152
156- // Attributes
157153 json << R"( "attributes":[)" ;
158154 bool first_attr = true ;
159155 for (size_t j = 0 ; j < span.string_attributes .size (); ++j) {
@@ -170,7 +166,6 @@ std::string SimpleOtlpHttpSender::SpansToJson(const std::vector<SimpleSpanData>&
170166 }
171167 json << " ]," ;
172168
173- // Status
174169 json << R"( "status":{"code":)" << span.status_code ;
175170 if (!span.status_message .empty ()) {
176171 json << R"( ,"message":")" << JsonEscape (span.status_message ) << R"( ")" ;
@@ -187,33 +182,29 @@ SimpleOtlpHttpSender& GetGlobalOtlpSender() {
187182}
188183
189184void InitGlobalOtlpSenderFromEnv (const std::string& service_name) {
190- // Read endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable
191185 std::string endpoint;
192186 const char * endpoint_env = std::getenv (" OTEL_EXPORTER_OTLP_ENDPOINT" );
193-
187+
194188 if (endpoint_env && endpoint_env[0 ] != ' \0 ' ) {
195- // Per OTLP spec, OTEL_EXPORTER_OTLP_ENDPOINT is the base URL
196- // Append /v1/traces for the traces signal
197189 endpoint = endpoint_env;
198- // Only append /v1/traces if it's not already part of the URL
199190 if (endpoint.find (" /v1/traces" ) == std::string::npos) {
200- // Remove trailing slash if present before appending
201191 if (!endpoint.empty () && endpoint.back () == ' /' ) {
202192 endpoint.pop_back ();
203193 }
204194 endpoint += " /v1/traces" ;
205195 }
206196 } else {
207- // Default endpoint for local development
208197 endpoint = " http://localhost:4318/v1/traces" ;
209198 }
210-
199+
211200 g_otlp_sender.SetEndpoint (endpoint);
212201 LOG (INFO) << " [OTEL] HTTP exporter endpoint: " << endpoint;
213202
214- // Set the service name (already resolved by caller)
215- g_otlp_sender.SetServiceName (service_name);
216- LOG (INFO) << " [OTEL] HTTP exporter service name: " << service_name;
203+ const char * service_name_env = std::getenv (" OTEL_SERVICE_NAME" );
204+ std::string resolved_service_name = (service_name_env && service_name_env[0 ] != ' \0 ' )
205+ ? service_name_env : " yugabyte" ;
206+ g_otlp_sender.SetServiceName (resolved_service_name);
207+ LOG (INFO) << " [OTEL] HTTP exporter service name: " << resolved_service_name;
217208}
218209
219210} // namespace yb
0 commit comments