Skip to content

Commit 5502660

Browse files
committed
pass query traceparent
1 parent 631f9e6 commit 5502660

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

src/postgres/src/backend/tcop/pquery.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,6 @@ PortalRunMulti(Portal portal,
12571257
* process a plannable query.
12581258
*/
12591259
TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1260-
YBCOtelExecuteStart();
12611260

12621261
if (log_executor_stats)
12631262
ResetUsage();
@@ -1322,7 +1321,6 @@ PortalRunMulti(Portal portal,
13221321
if (log_executor_stats)
13231322
ShowUsage("EXECUTOR STATISTICS");
13241323

1325-
YBCOtelExecuteDone();
13261324
TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
13271325
}
13281326
else

src/yb/rpc/outbound_call.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,10 @@ OutboundCall::OutboundCall(const RemoteMethod& remote_method,
243243
IncrementCounter(rpc_metrics_->outbound_calls_created);
244244
IncrementGauge(rpc_metrics_->outbound_calls_alive);
245245

246-
if (OtelTracing::HasActiveContext()) {
247-
otel_span_ = OtelTracing::StartSpan(Format("rpc.client $0", remote_method_.ToString()));
246+
const auto& traceparent = controller_->traceparent();
247+
if (!traceparent.empty()) {
248+
otel_span_ = OtelTracing::StartSpanFromTraceparent(
249+
Format("rpc.client $0", remote_method_.ToString()), traceparent);
248250
if (otel_span_.IsActive()) {
249251
otel_span_.SetAttribute("rpc.system", "yugabytedb");
250252
otel_span_.SetAttribute("rpc.service", remote_method_.service_name());

src/yb/rpc/rpc_controller.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#pragma once
3333

3434
#include <memory>
35+
#include <string>
3536

3637
#include "yb/util/logging.h"
3738

@@ -139,6 +140,11 @@ class RpcController {
139140

140141
InvokeCallbackMode invoke_callback_mode() { return invoke_callback_mode_; }
141142

143+
// Set the traceparent for distributed tracing. If set, the RPC will create
144+
// a child span under this traceparent. If not set, no tracing span is created.
145+
void set_traceparent(const std::string& traceparent) { traceparent_ = traceparent; }
146+
const std::string& traceparent() const { return traceparent_; }
147+
142148
// Return the configured timeout.
143149
MonoDelta timeout() const;
144150

@@ -183,6 +189,7 @@ class RpcController {
183189

184190
std::unique_ptr<Sidecars> outbound_sidecars_;
185191
bool TEST_disable_outbound_call_response_processing = false;
192+
std::string traceparent_;
186193

187194
DISALLOW_COPY_AND_ASSIGN(RpcController);
188195
};

src/yb/util/otel_tracing.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,21 @@ bool OtelTracing::HasActiveContext() {
418418
return current_span && current_span->GetContext().IsValid();
419419
}
420420

421+
std::string OtelTracing::GetCurrentTraceparent() {
422+
if (!IsEnabled()) {
423+
return "";
424+
}
425+
auto current_span = trace_api::Tracer::GetCurrentSpan();
426+
if (!current_span || !current_span->GetContext().IsValid()) {
427+
return "";
428+
}
429+
430+
auto span_context = current_span->GetContext();
431+
std::string trace_id = TraceIdToHex(span_context.trace_id());
432+
std::string span_id = SpanIdToHex(span_context.span_id());
433+
std::string flags = span_context.IsSampled() ? "01" : "00";
434+
435+
return "00-" + trace_id + "-" + span_id + "-" + flags;
436+
}
437+
421438
} // namespace yb

src/yb/util/otel_tracing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class OtelTracing {
6565
static void AdoptSpan(const OtelSpanHandle& span);
6666
static void EndCurrentSpan();
6767
static bool HasActiveContext();
68+
static std::string GetCurrentTraceparent();
6869

6970
private:
7071
OtelTracing() = delete;

src/yb/yql/pggate/pg_client.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,7 @@ class PgClient::Impl : public BigDataFetcher {
16561656
} else {
16571657
controller->set_timeout(timeout_);
16581658
}
1659+
controller->set_traceparent(OtelTracing::GetCurrentTraceparent());
16591660
return controller;
16601661
}
16611662

0 commit comments

Comments
 (0)