From: Omri Zeneva Date: Sun, 20 Mar 2022 19:10:34 +0000 (-0400) Subject: rgw: support conditional tracing using lua script X-Git-Tag: v18.0.0~885^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6ec1565b14aa4aad1263cb4c08ecf8ccec097930;p=ceph-ci.git rgw: support conditional tracing using lua script before lua script is being executed, we keep the tracer runtime configuration value, and then decides whether to trace or not the request based on the value that maybe changed during lua exeuction, so we can disable/enable tracing for request even if the tracer is in the opposite state at the same time Signed-off-by: Omri Zeneva --- diff --git a/doc/radosgw/lua-scripting.rst b/doc/radosgw/lua-scripting.rst index d970455a520..daf77ca0be7 100644 --- a/doc/radosgw/lua-scripting.rst +++ b/doc/radosgw/lua-scripting.rst @@ -290,6 +290,10 @@ Request Fields +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ | ``Request.User.Id`` | string | triggering user id | no | no | no | +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ +| ``Request.Trace`` | table | info on trace | no | no | no | ++----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ +| ``Request.Trace.Enable`` | boolean | tracing is enabled | no | yes | no | ++----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ Request Functions -------------------- @@ -424,3 +428,26 @@ Then, do a restart for the radosgw and upload the following script to the `postR assert(s:close()) end + +- Trace only requests of specific bucket + +Tracing is disabled by default, so we should enable tracing for this specific bucket + +.. code-block:: lua + + if Request.Bucket.Name == "my-bucket" then + Request.Trace.Enable = true + end + + +If `tracing is enabled `_ on the RGW, the value of Request.Trace.Enable is true, so we should disable tracing for all other requests that do not match the bucket name. +In the `preRequest` context: + +.. code-block:: lua + + if Request.Bucket.Name ~= "my-bucket" then + Request.Trace.Enable = false + end + +Note that changing `Request.Trace.Enable` does not change the tracer's state, but disables or enables the tracing for the request only. + diff --git a/src/common/tracer.cc b/src/common/tracer.cc index f10e0598a93..c9891726044 100644 --- a/src/common/tracer.cc +++ b/src/common/tracer.cc @@ -42,8 +42,15 @@ jspan Tracer::start_trace(opentelemetry::nostd::string_view trace_name) { return noop_tracer->StartSpan(trace_name); } +jspan Tracer::start_trace(opentelemetry::nostd::string_view trace_name, bool trace_is_enabled) { + if (trace_is_enabled) { + return tracer->StartSpan(trace_name); + } + return noop_tracer->StartSpan(trace_name); +} + jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, const jspan& parent_span) { - if (is_enabled() && parent_span) { + if (is_enabled() && parent_span->IsRecording()) { const auto parent_ctx = parent_span->GetContext(); return add_span(span_name, parent_ctx); } diff --git a/src/common/tracer.h b/src/common/tracer.h index 3d0e28f9b85..bd095f310f6 100644 --- a/src/common/tracer.h +++ b/src/common/tracer.h @@ -34,6 +34,11 @@ class Tracer { // creates and returns a new span with `trace_name` // this span represents a trace, since it has no parent. jspan start_trace(opentelemetry::nostd::string_view trace_name); + + // creates and returns a new span with `trace_name` + // if false is given to `trace_is_enabled` param, noop span will be returned + jspan start_trace(opentelemetry::nostd::string_view trace_name, bool trace_is_enabled); + // creates and returns a new span with `span_name` which parent span is `parent_span' jspan add_span(opentelemetry::nostd::string_view span_name, const jspan& parent_span); // creates and return a new span with `span_name` @@ -90,7 +95,7 @@ namespace tracing { struct Tracer { bool is_enabled() const { return false; } - jspan start_trace(std::string_view) { return {}; } + jspan start_trace(std::string_view, bool enabled = true) { return {}; } jspan add_span(std::string_view, const jspan&) { return {}; } jspan add_span(std::string_view span_name, const jspan_context& parent_ctx) { return {}; } void init(std::string_view service_name) {} diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index ddb455f041c..c7587115003 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -1690,6 +1690,7 @@ struct req_state : DoutPrefixProvider { std::vector session_policies; jspan trace; + bool trace_enabled = false; //Principal tags that come in as part of AssumeRoleWithWebIdentity std::vector> principal_tags; diff --git a/src/rgw/rgw_lua_request.cc b/src/rgw/rgw_lua_request.cc index 2e971c329c8..1efd71f1484 100644 --- a/src/rgw/rgw_lua_request.cc +++ b/src/rgw/rgw_lua_request.cc @@ -146,6 +146,37 @@ struct UserMetaTable : public EmptyMetaTable { } }; +struct TraceMetaTable : public EmptyMetaTable { + static std::string TableName() {return "Trace";} + static std::string Name() {return TableName() + "Meta";} + + static int IndexClosure(lua_State* L) { + const auto s = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + + const char* index = luaL_checkstring(L, 2); + + if (strcasecmp(index, "Enable") == 0) { + lua_pushboolean(L, s->trace_enabled); + } else { + return error_unknown_field(L, index, TableName()); + } + return ONE_RETURNVAL; + } + + static int NewIndexClosure(lua_State* L) { + const auto s = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + + const char* index = luaL_checkstring(L, 2); + + if (strcasecmp(index, "Enable") == 0) { + s->trace_enabled = lua_toboolean(L, 3); + } else { + return error_unknown_field(L, index, TableName()); + } + return NO_RETURNVAL; + } +}; + struct OwnerMetaTable : public EmptyMetaTable { static std::string TableName() {return "Owner";} static std::string Name() {return TableName() + "Meta";} @@ -781,6 +812,8 @@ struct RequestMetaTable : public EmptyMetaTable { } else { create_metatable(L, false, const_cast(&(s->user->get_id()))); } + } else if (strcasecmp(index, "Trace") == 0) { + create_metatable(L, false, s); } else { return error_unknown_field(L, index, TableName()); } diff --git a/src/rgw/rgw_op.cc b/src/rgw/rgw_op.cc index 961d3aec8e0..37541eef405 100644 --- a/src/rgw/rgw_op.cc +++ b/src/rgw/rgw_op.cc @@ -6174,6 +6174,7 @@ void RGWInitMultipart::pre_exec() void RGWInitMultipart::execute(optional_yield y) { + multipart_trace = tracing::rgw::tracer.start_trace(tracing::rgw::MULTIPART, s->trace_enabled); bufferlist aclbl, tracebl; rgw::sal::Attrs attrs; diff --git a/src/rgw/rgw_op.h b/src/rgw/rgw_op.h index f584d78a718..92827be5a09 100644 --- a/src/rgw/rgw_op.h +++ b/src/rgw/rgw_op.h @@ -1841,7 +1841,6 @@ public: RGWInitMultipart() {} void init(rgw::sal::Store* store, struct req_state *s, RGWHandler *h) override { - multipart_trace = tracing::rgw::tracer.start_trace(tracing::rgw::MULTIPART); RGWOp::init(store, s, h); policy.set_ctx(s->cct); } diff --git a/src/rgw/rgw_process.cc b/src/rgw/rgw_process.cc index 77ef102d30c..a4d44267bfb 100644 --- a/src/rgw/rgw_process.cc +++ b/src/rgw/rgw_process.cc @@ -328,6 +328,7 @@ int process_request(rgw::sal::Store* const store, goto done; } { + s->trace_enabled = tracing::rgw::tracer.is_enabled(); std::string script; auto rc = rgw::lua::read_script(s, store, s->bucket_tenant, s->yield, rgw::lua::context::preRequest, script); if (rc == -ENOENT) { @@ -383,8 +384,9 @@ int process_request(rgw::sal::Store* const store, goto done; } + const auto trace_name = std::string(op->name()) + " " + s->trans_id; - s->trace = tracing::rgw::tracer.start_trace(trace_name); + s->trace = tracing::rgw::tracer.start_trace(trace_name, s->trace_enabled); s->trace->SetAttribute(tracing::rgw::OP, op->name()); s->trace->SetAttribute(tracing::rgw::TYPE, tracing::rgw::REQUEST);