Skip to content

Commit

Permalink
Remove metrics and collector (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 authored Mar 16, 2022
1 parent 113c29a commit 4bb1c08
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 541 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ set(LIBDDWAF_SOURCE
${libddwaf_SOURCE_DIR}/src/utils.cpp
${libddwaf_SOURCE_DIR}/src/log.cpp
${libddwaf_SOURCE_DIR}/src/validator.cpp
${libddwaf_SOURCE_DIR}/src/metrics.cpp
${libddwaf_SOURCE_DIR}/src/parser/parser.cpp
${libddwaf_SOURCE_DIR}/src/parser/parser_v1.cpp
${libddwaf_SOURCE_DIR}/src/parser/parser_v2.cpp
Expand Down
2 changes: 1 addition & 1 deletion examples/verify_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool runVectors(YAML::Node rule, ddwaf_handle handle, bool runPositiveMatches)
ddwaf_object root = vector->as<ddwaf_object>();
if(root.type != DDWAF_OBJ_INVALID) {
ddwaf_context ctx = ddwaf_context_init(handle, ddwaf_object_free);
DDWAF_RET_CODE ret = ddwaf_run(ctx, &root, nullptr, nullptr, LONG_TIME);
DDWAF_RET_CODE ret = ddwaf_run(ctx, &root, nullptr, LONG_TIME);

bool hadError = ret < DDWAF_GOOD;
bool hadMatch = !hadError && ret != DDWAF_GOOD;
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/src/ddwaf_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void run_waf(ddwaf_handle handle, ddwaf_object args, size_t timeLeftInUs)
}

ddwaf_result res;
auto code = ddwaf_run(context, &args, nullptr, &res, timeLeftInUs);
auto code = ddwaf_run(context, &args, &res, timeLeftInUs);

// TODO split input in several ddwaf_object, and call ddwaf_run on the same context

Expand Down
67 changes: 3 additions & 64 deletions include/ddwaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,18 @@ typedef enum
#ifdef __cplusplus
class PowerWAF;
class PWAdditive;
namespace ddwaf {
class metrics_collector;
}
using ddwaf_handle = PowerWAF *;
using ddwaf_context = PWAdditive *;
using ddwaf_metrics_collector = ddwaf::metrics_collector *;
#else
typedef struct _ddwaf_handle* ddwaf_handle;
typedef struct _ddwaf_context* ddwaf_context;
typedef struct _ddwaf_metrics_collector* ddwaf_metrics_collector;
#endif

typedef struct _ddwaf_object ddwaf_object;
typedef struct _ddwaf_config ddwaf_config;
typedef struct _ddwaf_result ddwaf_result;
typedef struct _ddwaf_version ddwaf_version;
typedef struct _ddwaf_ruleset_info ddwaf_ruleset_info;
typedef struct _ddwaf_metrics ddwaf_metrics;
/**
* @struct ddwaf_object
*
Expand Down Expand Up @@ -137,6 +131,8 @@ struct _ddwaf_result
bool timeout;
/** Run result in JSON format **/
const char* data;
/** Total WAF runtime in nanoseconds **/
uint64_t total_runtime;
};

/**
Expand Down Expand Up @@ -169,19 +165,6 @@ struct _ddwaf_ruleset_info
const char *version;
};

/**
* @ddwaf_metrics
*
* Structure containing total and per-rule runtimes
**/
struct _ddwaf_metrics
{
/** Total WAF runtime in nanoseconds **/
uint64_t total_runtime;
/** Map containing runtime in nanoseconds per rule **/
ddwaf_object rule_runtime;
};

/**
* @typedef ddwaf_object_free_fn
*
Expand Down Expand Up @@ -278,7 +261,6 @@ ddwaf_context ddwaf_context_init(const ddwaf_handle handle, ddwaf_object_free_fn
* function will be used to free the data provided. Note that the
* data passed must be valid until the destruction of the context.
* (nonull)
* @param collector Metrics collector for this run. (nullable)
* @param result Structure containing the result of the operation. (nullable)
* @param timeout Maximum time budget in microseconds.
*
Expand All @@ -296,8 +278,7 @@ ddwaf_context ddwaf_context_init(const ddwaf_handle handle, ddwaf_object_free_fn
* data is unknown. The result structure will not be
* filled if this error occurs.
**/
DDWAF_RET_CODE ddwaf_run(ddwaf_context context, ddwaf_object *data,
ddwaf_metrics_collector collector,
DDWAF_RET_CODE ddwaf_run(ddwaf_context context, ddwaf_object *data,
ddwaf_result *result, uint64_t timeout);

/**
Expand All @@ -319,48 +300,6 @@ void ddwaf_context_destroy(ddwaf_context context);
**/
void ddwaf_result_free(ddwaf_result *result);

/**
* ddwaf_metrics_collector_init
*
* Metrics collection object.
*
* @param handle Handle of the WAF instance containing the ruleset definition. (nonnull)
*
* @return Handle to the context instance.
*
* @note The WAF instance needs to be valid for the lifetime of the context.
**/

ddwaf_metrics_collector ddwaf_metrics_collector_init(const ddwaf_handle handle);

/**
* ddwaf_get_metrics
*
* Obtains the metrics collected by the metrics collector
*
* @param collector Metrics collector from which to request metrics.
* @param metrics Output structure containing the collected metrics.
**/
void ddwaf_get_metrics(ddwaf_metrics_collector collector, ddwaf_metrics *metrics);

/**
* ddwaf_metrics_collector_destroy
*
* Performs the destruction of the metrics_collector.
*
* @param collector Metrics collector to destroy. (nonnull)
**/
void ddwaf_metrics_collector_destroy(ddwaf_metrics_collector collector);

/**
* ddwaf_metrics_free
*
* Free a ddwaf_metrics object
*
* @param metrics Object to free
**/
void ddwaf_metrics_free(ddwaf_metrics *metrics);

/**
* ddwaf_object_invalid
*
Expand Down
4 changes: 0 additions & 4 deletions libddwaf.def
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ EXPORTS
ddwaf_run
ddwaf_context_destroy
ddwaf_result_free
ddwaf_metrics_collector_init
ddwaf_get_metrics
ddwaf_metrics_collector_destroy
ddwaf_metrics_free
ddwaf_object_invalid
ddwaf_object_string
ddwaf_object_stringl
Expand Down
2 changes: 1 addition & 1 deletion smoketest/smoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int main() {
ddwaf_object_map_add(&data, "key", DDSTR("Arachni"));

ddwaf_result result = {0};
ddwaf_run(ctx, &data, NULL, &result, (uint32_t)-1);
ddwaf_run(ctx, &data, &result, (uint32_t)-1);

if (!result.data) {
puts("result data is null");
Expand Down
10 changes: 2 additions & 8 deletions src/PWAdditive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ PWAdditive::~PWAdditive()
}

DDWAF_RET_CODE PWAdditive::run(ddwaf_object newParameters,
optional_ref<ddwaf::metrics_collector> collector,
optional_ref<ddwaf_result> res, uint64_t timeLeft)
{
if (!object_validator.validate(newParameters))
Expand Down Expand Up @@ -90,7 +89,7 @@ DDWAF_RET_CODE PWAdditive::run(ddwaf_object newParameters,
PWRetManager retManager(processor.getGlobalAllocator());
for (const auto& [key, flow] : wafHandle->flows)
{
if (!processor.runFlow(key, flow, collector, retManager))
if (!processor.runFlow(key, flow, retManager))
{
break;
}
Expand All @@ -101,12 +100,7 @@ DDWAF_RET_CODE PWAdditive::run(ddwaf_object newParameters,
{
ddwaf_result& output = *res;
retManager.synthetize(output);

if (collector)
{
ddwaf::metrics_collector& mc = *collector;
mc.record_total(ddwaf::monotonic_clock::now() - start);
}
output.total_runtime = (ddwaf::monotonic_clock::now() - start).count();
}

return code;
Expand Down
5 changes: 1 addition & 4 deletions src/PWAdditive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <PWProcessor.hpp>
#include <PowerWAF.hpp>
#include <ddwaf.h>
#include <metrics.hpp>
#include <optional>
#include <utils.h>
#include <validator.hpp>
Expand All @@ -27,9 +26,7 @@ class PWAdditive

~PWAdditive();

DDWAF_RET_CODE run(ddwaf_object,
optional_ref<ddwaf::metrics_collector> collector,
optional_ref<ddwaf_result> res, uint64_t);
DDWAF_RET_CODE run(ddwaf_object, optional_ref<ddwaf_result> res, uint64_t);

#ifdef TESTING
FRIEND_TEST(TestPWProcessor, TestCache);
Expand Down
19 changes: 3 additions & 16 deletions src/PWProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ bool PWProcessor::shouldIgnoreCacheHit(const std::vector<ddwaf::condition>& cond

bool PWProcessor::runFlow(const std::string& name,
const ddwaf::rule_ref_vector& flow,
optional_ref<ddwaf::metrics_collector> collector,
PWRetManager& retManager)
{
/*
Expand All @@ -70,7 +69,6 @@ bool PWProcessor::runFlow(const std::string& name,
return false;
}

ddwaf::monotonic_clock::time_point start;
match_status status = match_status::invalid;
//Process each rule we have to run for this step of the flow
for (ddwaf::rule& rule : flow)
Expand All @@ -87,8 +85,6 @@ bool PWProcessor::runFlow(const std::string& name,

DDWAF_DEBUG("Running the WAF on rule %s", rule.id.c_str());

retManager.startRule();

bool cachedNegativeMatch = cache_status == match_status::no_match;

// If we had a negative match in the past, let's check if we have a reason to run again
Expand All @@ -97,9 +93,7 @@ bool PWProcessor::runFlow(const std::string& name,
continue;
}

if (collector) {
start = ddwaf::monotonic_clock::now();
}
retManager.startRule();

// Actually execute the rule
// We tell the PWRetriever to skip old parameters if this is safe to do so
Expand Down Expand Up @@ -130,17 +124,10 @@ bool PWProcessor::runFlow(const std::string& name,
}
}

auto now = ddwaf::monotonic_clock::now();
//Store the result of the rule in the cache
if (status != match_status::missing_arg)
{
ranCache.insert_or_assign(index, status);

if (collector)
{
ddwaf::metrics_collector& mc = *collector;
mc.record_rule(index, now - start);
}
}

// Update the time measurement, and check the deadline while we're at it
Expand All @@ -154,11 +141,11 @@ bool PWProcessor::runFlow(const std::string& name,
break;
}

if (deadline <= now)
if (deadline <= ddwaf::monotonic_clock::now())
{
DDWAF_INFO("Ran out of time while running flow %s and rule %s", name.c_str(), rule.id.c_str());
retManager.recordTimeout();
break;
return false;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/PWProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ struct PWProcessor;
#include <PWRet.hpp>
#include <PWRetriever.hpp>
#include <clock.hpp>
#include <metrics.hpp>
#include <rule.hpp>
#include <utils.h>

Expand All @@ -38,7 +37,6 @@ struct PWProcessor
void startNewRun(const ddwaf::monotonic_clock::time_point& _deadline);
bool runFlow(const std::string& name,
const ddwaf::rule_ref_vector& flow,
optional_ref<ddwaf::metrics_collector> collector,
PWRetManager& manager);

bool isFirstRun() const;
Expand Down
11 changes: 1 addition & 10 deletions src/PowerWAFInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <PWRet.hpp>
#include <exception.hpp>
#include <memory>
#include <metrics.hpp>
#include <mutex>
#include <ruleset_info.hpp>
#include <shared_mutex>
Expand Down Expand Up @@ -133,7 +132,6 @@ extern "C"
}

DDWAF_RET_CODE ddwaf_run(ddwaf_context context, ddwaf_object* data,
ddwaf_metrics_collector collector,
ddwaf_result* result, uint64_t timeout)
{
if (result != nullptr)
Expand All @@ -148,20 +146,13 @@ extern "C"
}
try
{
// TODO: make the collector optional
optional_ref<ddwaf::metrics_collector> mc { std::nullopt };
if (collector != nullptr)
{
mc = *collector;
}

optional_ref<ddwaf_result> res { std::nullopt };
if (result != nullptr)
{
res = *result;
}

return context->run(*data, mc, res, timeout);
return context->run(*data, res, timeout);
}
catch (const std::exception& e)
{
Expand Down
Loading

0 comments on commit 4bb1c08

Please sign in to comment.