[Python Otel] Fix Segfault caused by callTracer method lifetime issue

pull/37329/head
Xuan Wang 6 months ago
parent b056bc41d3
commit 60dd7835a5
  1. 12
      src/python/grpcio_observability/grpc_observability/client_call_tracer.cc
  2. 4
      src/python/grpcio_observability/grpc_observability/client_call_tracer.h
  3. 8
      src/python/grpcio_observability/grpc_observability/python_observability_context.h

@ -88,7 +88,7 @@ void PythonOpenCensusCallTracer::RecordAnnotation(
PythonOpenCensusCallTracer::~PythonOpenCensusCallTracer() { PythonOpenCensusCallTracer::~PythonOpenCensusCallTracer() {
if (PythonCensusStatsEnabled()) { if (PythonCensusStatsEnabled()) {
context_.Labels().emplace_back(kClientMethod, std::string(method_)); context_.Labels().emplace_back(kClientMethod, method_);
RecordIntMetric(kRpcClientRetriesPerCallMeasureName, retries_ - 1, RecordIntMetric(kRpcClientRetriesPerCallMeasureName, retries_ - 1,
context_.Labels(), identifier_, registered_method_, context_.Labels(), identifier_, registered_method_,
/*include_exchange_labels=*/true); // exclude first attempt /*include_exchange_labels=*/true); // exclude first attempt
@ -159,8 +159,8 @@ PythonOpenCensusCallTracer::PythonOpenCensusCallAttemptTracer::
if (!PythonCensusStatsEnabled()) { if (!PythonCensusStatsEnabled()) {
return; return;
} }
context_.Labels().emplace_back(kClientMethod, std::string(parent_->method_)); context_.Labels().emplace_back(kClientMethod, parent_->method_);
context_.Labels().emplace_back(kClientTarget, std::string(parent_->target_)); context_.Labels().emplace_back(kClientTarget, parent_->target_);
RecordIntMetric(kRpcClientStartedRpcsMeasureName, 1, context_.Labels(), RecordIntMetric(kRpcClientStartedRpcsMeasureName, 1, context_.Labels(),
parent_->identifier_, parent_->registered_method_, parent_->identifier_, parent_->registered_method_,
/*include_exchange_labels=*/false); /*include_exchange_labels=*/false);
@ -264,8 +264,8 @@ void PythonOpenCensusCallTracer::PythonOpenCensusCallAttemptTracer::
} }
std::string final_status = absl::StatusCodeToString(status_code_); std::string final_status = absl::StatusCodeToString(status_code_);
context_.Labels().emplace_back(kClientMethod, std::string(parent_->method_)); context_.Labels().emplace_back(kClientMethod, parent_->method_);
context_.Labels().emplace_back(kClientTarget, std::string(parent_->target_)); context_.Labels().emplace_back(kClientTarget, parent_->target_);
context_.Labels().emplace_back(kClientStatus, final_status); context_.Labels().emplace_back(kClientStatus, final_status);
if (parent_->add_csm_optional_labels_) { if (parent_->add_csm_optional_labels_) {
parent_->labels_injector_.AddXdsOptionalLabels( parent_->labels_injector_.AddXdsOptionalLabels(
@ -323,7 +323,7 @@ void PythonOpenCensusCallTracer::PythonOpenCensusCallAttemptTracer::RecordEnd(
const gpr_timespec& /*latency*/) { const gpr_timespec& /*latency*/) {
if (PythonCensusStatsEnabled()) { if (PythonCensusStatsEnabled()) {
context_.Labels().emplace_back(kClientMethod, context_.Labels().emplace_back(kClientMethod,
std::string(parent_->method_)); parent_->method_);
context_.Labels().emplace_back(kClientStatus, context_.Labels().emplace_back(kClientStatus,
StatusCodeToString(status_code_)); StatusCodeToString(status_code_));
RecordIntMetric(kRpcClientSentMessagesPerRpcMeasureName, RecordIntMetric(kRpcClientSentMessagesPerRpcMeasureName,

@ -140,9 +140,9 @@ class PythonOpenCensusCallTracer : public grpc_core::ClientCallTracer {
PythonCensusContext CreateCensusContextForCallAttempt(); PythonCensusContext CreateCensusContextForCallAttempt();
// Client method. // Client method.
absl::string_view method_; std::string method_;
// Client target. // Client target.
absl::string_view target_; std::string target_;
PythonCensusContext context_; PythonCensusContext context_;
bool tracing_enabled_; bool tracing_enabled_;
bool add_csm_optional_labels_; bool add_csm_optional_labels_;

@ -272,16 +272,16 @@ void GenerateClientContext(absl::string_view method, absl::string_view trace_id,
void GenerateServerContext(absl::string_view header, absl::string_view method, void GenerateServerContext(absl::string_view header, absl::string_view method,
PythonCensusContext* context); PythonCensusContext* context);
inline absl::string_view GetMethod(const char* method) { inline std::string GetMethod(const char* method) {
if (std::string(method).empty()) { if (std::string(method).empty()) {
return ""; return "";
} }
// Check for leading '/' and trim it if present. // Check for leading '/' and trim it if present.
return absl::StripPrefix(absl::string_view(method), "/"); return std::string(absl::StripPrefix(method, "/"));
} }
inline absl::string_view GetTarget(const char* target) { inline std::string GetTarget(const char* target) {
return absl::string_view(target); return std::string(target);
} }
// Fills a pre-allocated buffer with the value for the grpc-trace-bin header. // Fills a pre-allocated buffer with the value for the grpc-trace-bin header.

Loading…
Cancel
Save