Skip to content

Commit

Permalink
[Feature] Integrate some simple trace into be HTTP service (StarRocks…
Browse files Browse the repository at this point in the history
  • Loading branch information
xlwh authored May 20, 2022
1 parent d2e666c commit bea2b3c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 40 deletions.
3 changes: 3 additions & 0 deletions be/src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ CONF_Bool(enable_segment_overflow_read_chunk, "true");

CONF_Int32(max_batch_publish_latency_ms, "100");

// Config for opentelemetry tracing.
CONF_String(jaeger_endpoint, "");

} // namespace config

} // namespace starrocks
44 changes: 30 additions & 14 deletions be/src/common/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,50 @@

#include <opentelemetry/exporters/jaeger/jaeger_exporter.h>

#include "common/config.h"

namespace starrocks {

namespace sdktrace = opentelemetry::sdk::trace;
namespace trace = opentelemetry::trace;

Tracer::Tracer(const std::string& service_name, const TracerOptions& tracer_opts) : _tracer_options(tracer_opts) {
init(service_name);
Tracer::~Tracer() {
shutdown();
}

Tracer& Tracer::Instance() {
static Tracer global_tracer;
static std::once_flag oc;
std::call_once(oc, [&]() { global_tracer.init("STARROCKS-BE"); });
return global_tracer;
}

void Tracer::init(const std::string& service_name) {
opentelemetry::exporter::jaeger::JaegerExporterOptions opts;
opts.endpoint = _tracer_options.jaeger_endpoint;
opts.server_port = _tracer_options.jaeger_server_port;
auto jaeger_exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(
new opentelemetry::exporter::jaeger::JaegerExporter(opts));
auto processor = std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>(
new opentelemetry::sdk::trace::SimpleSpanProcessor(std::move(jaeger_exporter)));
const auto jaeger_resource = opentelemetry::sdk::resource::Resource::Create(
std::move(opentelemetry::sdk::resource::ResourceAttributes{{"service.name", service_name}}));
const auto provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(std::move(processor), jaeger_resource));
_tracer = provider->GetTracer(service_name, OPENTELEMETRY_SDK_VERSION);
if (!config::jaeger_endpoint.empty()) {
opentelemetry::exporter::jaeger::JaegerExporterOptions opts;
opts.endpoint = config::jaeger_endpoint;
auto jaeger_exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(
new opentelemetry::exporter::jaeger::JaegerExporter(opts));
auto processor = std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>(
new opentelemetry::sdk::trace::SimpleSpanProcessor(std::move(jaeger_exporter)));
const auto jaeger_resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{{"service.name", service_name}});
const auto provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(std::move(processor), jaeger_resource));
_tracer = provider->GetTracer(service_name, OPENTELEMETRY_SDK_VERSION);
} else {
_tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("no-op", OPENTELEMETRY_SDK_VERSION);
}
}

void Tracer::shutdown() {
_tracer->CloseWithMicroseconds(1);
}

bool Tracer::is_enabled() const {
return !config::jaeger_endpoint.empty();
}

Span Tracer::start_trace(const std::string& trace_name) {
return _tracer->StartSpan(trace_name);
}
Expand Down
33 changes: 16 additions & 17 deletions be/src/common/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
#include <opentelemetry/trace/provider.h>

namespace starrocks {

using Span = opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>;
using SpanContext = opentelemetry::trace::SpanContext;

// The tracer options.
struct TracerOptions {
std::string jaeger_endpoint;
int jaeger_server_port;
};
namespace trace = opentelemetry::trace;
using Span = opentelemetry::nostd::shared_ptr<trace::Span>;
using SpanContext = trace::SpanContext;

/**
* Handles span creation and provides a compatible interface to `opentelemetry::trace::Tracer`.
Expand All @@ -27,13 +21,13 @@ struct TracerOptions {
*
* Here is an example on how to create spans and retrieve traces:
* ```
* std::shared_ptr<Tracer> tracer;
* const Tracer& tracer = Tracer::Instance();
*
* void f1(std::shared_ptr<Tracer> tracer) {
* auto root = tracer->start_trace("root");
* void f1(const Tracer& tracer) {
* auto root = tracer.start_trace("root");
* sleepFor(Milliseconds(1));
* {
* auto child = tracer->add_span("child");
* auto child = tracer.add_span("child", root);
* sleepFor(Milliseconds(2));
* }
* }
Expand All @@ -42,10 +36,13 @@ struct TracerOptions {
*/
class Tracer {
public:
Tracer(const std::string& service_name, const TracerOptions& tracer_opts = {"localhost", 6381});
~Tracer();

// Shutdown the tracer.
void shutdown();
// Get the global tracer instance.
static Tracer& Instance();

// Return true if trace is enabled.
bool is_enabled() const;

// Creates and returns a new span with `trace_name`
// this span represents a trace, since it has no parent.
Expand All @@ -62,9 +59,11 @@ class Tracer {
private:
// Init the tracer.
void init(const std::string& service_name);
// Shutdown the tracer.
void shutdown();

// The global tracer.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> _tracer;
TracerOptions _tracer_options;
};

} // namespace starrocks
6 changes: 2 additions & 4 deletions be/src/http/action/compaction_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

#include "common/logging.h"
#include "common/status.h"
#include "common/tracer.h"
#include "fmt/core.h"
#include "gutil/strings/substitute.h"
#include "http/http_channel.h"
#include "http/http_headers.h"
#include "http/http_request.h"
#include "http/http_response.h"
#include "http/http_status.h"
#include "runtime/exec_env.h"
#include "storage/base_compaction.h"
Expand Down Expand Up @@ -96,8 +96,8 @@ Status CompactionAction::_handle_compaction(HttpRequest* req, std::string* json_
if (_running) {
return Status::TooManyTasks("Manual compaction task is running");
}

_running = true;
auto scoped_span = trace::Scope(Tracer::Instance().start_trace("http_handle_compaction"));
DeferOp defer([&]() { _running = false; });

uint64_t tablet_id;
Expand All @@ -115,7 +115,6 @@ Status CompactionAction::_handle_compaction(HttpRequest* req, std::string* json_
}

StarRocksMetrics::instance()->cumulative_compaction_request_total.increment(1);

auto* mem_tracker = ExecEnv::GetInstance()->compaction_mem_tracker();

if (compaction_type == to_string(CompactionType::CUMULATIVE_COMPACTION)) {
Expand Down Expand Up @@ -154,7 +153,6 @@ Status CompactionAction::_handle_compaction(HttpRequest* req, std::string* json_
}
_running = false;
*json_result = R"({"status": "Success", "msg": "compaction task executed successful"})";

return Status::OK();
}

Expand Down
3 changes: 2 additions & 1 deletion be/src/http/action/health_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include <sstream>
#include <string>

#include "common/tracer.h"
#include "http/http_channel.h"
#include "http/http_headers.h"
#include "http/http_request.h"
#include "http/http_response.h"
#include "http/http_status.h"

namespace starrocks {
Expand All @@ -37,6 +37,7 @@ const static std::string HEADER_JSON = "application/json";
HealthAction::HealthAction(ExecEnv* exec_env) : _exec_env(exec_env) {}

void HealthAction::handle(HttpRequest* req) {
auto scoped_span = trace::Scope(Tracer::Instance().start_trace("http_handle_health"));
std::stringstream ss;
ss << "{";
ss << R"("status": "OK",)";
Expand Down
2 changes: 2 additions & 0 deletions be/src/http/action/metrics_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <string>

#include "common/tracer.h"
#include "http/http_channel.h"
#include "http/http_headers.h"
#include "http/http_request.h"
Expand Down Expand Up @@ -190,6 +191,7 @@ void JsonMetricsVisitor::visit(const std::string& prefix, const std::string& nam
}

void MetricsAction::handle(HttpRequest* req) {
auto scoped_span = trace::Scope(Tracer::Instance().start_trace("http_handle_metrics"));
const std::string& type = req->param("type");
std::string str;
if (type == "core") {
Expand Down
2 changes: 2 additions & 0 deletions be/src/http/action/pprof_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "common/config.h"
#include "common/status.h"
#include "common/tracer.h"
#include "http/ev_http_server.h"
#include "http/http_channel.h"
#include "http/http_handler.h"
Expand Down Expand Up @@ -101,6 +102,7 @@ void ProfileAction::handle(HttpRequest* req) {
HttpChannel::send_reply(req, str);
#else
std::lock_guard<std::mutex> lock(kPprofActionMutex);
auto scoped_span = trace::Scope(Tracer::Instance().start_trace("http_handle_profile"));

int seconds = kPprofDefaultSampleSecs;
const std::string& seconds_str = req->param(SECOND_KEY);
Expand Down
7 changes: 3 additions & 4 deletions be/test/common/tracer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ namespace starrocks {
class TracerTest : public testing::Test {};

TEST_F(TracerTest, BasicTest) {
std::unique_ptr<Tracer> tracer = std::make_unique<Tracer>("TracerTest");
auto span = tracer->start_trace("test");
EXPECT_TRUE(span->IsRecording());
EXPECT_FALSE(Tracer::Instance().is_enabled());
auto span = Tracer::Instance().start_trace("test");
EXPECT_FALSE(span->IsRecording());
span->End();
EXPECT_FALSE(span->IsRecording());
tracer->shutdown();
}

} // namespace starrocks

0 comments on commit bea2b3c

Please sign in to comment.