This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch cpp
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/cpp by this push:
     new 15a1a7f  Replace weak/strong symbol with TracingSamplerProvider
15a1a7f is described below

commit 15a1a7ff157888ed60b473a8377cc4797e3769a3
Author: Li Zhanhui <[email protected]>
AuthorDate: Mon Jul 4 11:37:33 2022 +0800

    Replace weak/strong symbol with TracingSamplerProvider
---
 cpp/api/rocketmq/Tracing.h                     | 10 ++++++++--
 cpp/src/main/cpp/base/Tracing.cpp              | 26 --------------------------
 cpp/src/main/cpp/client/include/ClientConfig.h |  7 ++++---
 cpp/src/main/cpp/rocketmq/ProducerImpl.cpp     | 13 +++++++------
 cpp/src/main/cpp/rocketmq/include/ClientImpl.h |  6 ++++++
 5 files changed, 25 insertions(+), 37 deletions(-)

diff --git a/cpp/api/rocketmq/Tracing.h b/cpp/api/rocketmq/Tracing.h
index bc12362..cdec455 100644
--- a/cpp/api/rocketmq/Tracing.h
+++ b/cpp/api/rocketmq/Tracing.h
@@ -16,12 +16,18 @@
  */
 #pragma once
 
-#include "opencensus/trace/sampler.h"
+#include <memory>
 
 #include "RocketMQ.h"
+#include "opencensus/trace/sampler.h"
 
 ROCKETMQ_NAMESPACE_BEGIN
 
-opencensus::trace::Sampler* traceSampler() __attribute__((weak));
+class TracingSamplerProvider {
+public:
+  virtual ~TracingSamplerProvider() = default;
+
+  virtual std::unique_ptr<opencensus::trace::Sampler> tracingSampler() = 0;
+};
 
 ROCKETMQ_NAMESPACE_END
diff --git a/cpp/src/main/cpp/base/Tracing.cpp 
b/cpp/src/main/cpp/base/Tracing.cpp
deleted file mode 100644
index 9eb7335..0000000
--- a/cpp/src/main/cpp/base/Tracing.cpp
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "rocketmq/Tracing.h"
-
-ROCKETMQ_NAMESPACE_BEGIN
-
-opencensus::trace::Sampler* traceSampler() {
-  static opencensus::trace::NeverSampler sampler;
-  return &sampler;
-}
-
-ROCKETMQ_NAMESPACE_END
diff --git a/cpp/src/main/cpp/client/include/ClientConfig.h 
b/cpp/src/main/cpp/client/include/ClientConfig.h
index 66ca230..bade9a4 100644
--- a/cpp/src/main/cpp/client/include/ClientConfig.h
+++ b/cpp/src/main/cpp/client/include/ClientConfig.h
@@ -21,12 +21,12 @@
 #include <string>
 #include <vector>
 
-#include "absl/container/flat_hash_map.h"
-#include "absl/time/time.h"
-
 #include "Protocol.h"
 #include "RetryPolicy.h"
+#include "absl/container/flat_hash_map.h"
+#include "absl/time/time.h"
 #include "rocketmq/CredentialsProvider.h"
+#include "rocketmq/Tracing.h"
 
 ROCKETMQ_NAMESPACE_BEGIN
 
@@ -61,6 +61,7 @@ struct ClientConfig {
   PublisherConfig publisher;
   SubscriberConfig subscriber;
   Metric metric;
+  std::unique_ptr<opencensus::trace::Sampler> sampler_;
 };
 
 ROCKETMQ_NAMESPACE_END
\ No newline at end of file
diff --git a/cpp/src/main/cpp/rocketmq/ProducerImpl.cpp 
b/cpp/src/main/cpp/rocketmq/ProducerImpl.cpp
index 4b6c3f0..65cc5ba 100644
--- a/cpp/src/main/cpp/rocketmq/ProducerImpl.cpp
+++ b/cpp/src/main/cpp/rocketmq/ProducerImpl.cpp
@@ -290,16 +290,17 @@ void ProducerImpl::sendImpl(std::shared_ptr<SendContext> 
context) {
 
   {
     // Trace Send RPC
-    if (context->message_->traceContext().has_value()) {
+    if (context->message_->traceContext().has_value() && 
client_config_.sampler_) {
       auto span_context =
           
opencensus::trace::propagation::FromTraceParentHeader(context->message_->traceContext().value());
       auto span = opencensus::trace::Span::BlankSpan();
       std::string span_name = resourceNamespace() + "/" + 
context->message_->topic() + " " +
                               
MixAll::SPAN_ATTRIBUTE_VALUE_ROCKETMQ_SEND_OPERATION;
       if (span_context.IsValid()) {
-        span = opencensus::trace::Span::StartSpanWithRemoteParent(span_name, 
span_context, {traceSampler()});
+        span = opencensus::trace::Span::StartSpanWithRemoteParent(span_name, 
span_context,
+                                                                  
{client_config_.sampler_.get()});
       } else {
-        span = opencensus::trace::Span::StartSpan(span_name, nullptr, 
{traceSampler()});
+        span = opencensus::trace::Span::StartSpan(span_name, nullptr, 
{client_config_.sampler_.get()});
       }
       span.AddAttribute(MixAll::SPAN_ATTRIBUTE_KEY_MESSAGING_OPERATION,
                         MixAll::SPAN_ATTRIBUTE_VALUE_ROCKETMQ_SEND_OPERATION);
@@ -380,7 +381,7 @@ bool ProducerImpl::endTransaction0(const Transaction& 
transaction, TransactionSt
   bool completed = false;
   bool success = false;
   auto span = opencensus::trace::Span::BlankSpan();
-  if (!transaction.traceContext().empty()) {
+  if (!transaction.traceContext().empty() && client_config_.sampler_) {
     // Trace transactional message
     opencensus::trace::SpanContext span_context =
         
opencensus::trace::propagation::FromTraceParentHeader(transaction.traceContext());
@@ -389,9 +390,9 @@ bool ProducerImpl::endTransaction0(const Transaction& 
transaction, TransactionSt
                                            : 
MixAll::SPAN_ATTRIBUTE_VALUE_ROCKETMQ_ROLLBACK_OPERATION;
     std::string span_name = resourceNamespace() + "/" + transaction.topic() + 
" " + trace_operation_name;
     if (span_context.IsValid()) {
-      span = opencensus::trace::Span::StartSpanWithRemoteParent(span_name, 
span_context, {traceSampler()});
+      span = opencensus::trace::Span::StartSpanWithRemoteParent(span_name, 
span_context, {client_config_.sampler_.get()});
     } else {
-      span = opencensus::trace::Span::StartSpan(span_name, nullptr, 
{traceSampler()});
+      span = opencensus::trace::Span::StartSpan(span_name, nullptr, 
{client_config_.sampler_.get()});
     }
     span.AddAttribute(MixAll::SPAN_ATTRIBUTE_KEY_MESSAGING_OPERATION, 
trace_operation_name);
     span.AddAttribute(MixAll::SPAN_ATTRIBUTE_KEY_ROCKETMQ_OPERATION, 
trace_operation_name);
diff --git a/cpp/src/main/cpp/rocketmq/include/ClientImpl.h 
b/cpp/src/main/cpp/rocketmq/include/ClientImpl.h
index ff61af3..c136cc9 100644
--- a/cpp/src/main/cpp/rocketmq/include/ClientImpl.h
+++ b/cpp/src/main/cpp/rocketmq/include/ClientImpl.h
@@ -110,6 +110,12 @@ public:
   virtual void buildClientSettings(rmq::Settings& settings) {
   }
 
+  void registerTracingSampler(TracingSamplerProvider *provider) {
+    if (provider) {
+      client_config_.sampler_ = provider->tracingSampler();
+    }
+  }
+
 protected:
   ClientConfig client_config_;
 

Reply via email to