Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions source/extensions/filters/http/wasm/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ Http::FilterFactoryCb WasmFilterConfig::createFilterFactoryFromProtoTyped(
/**
* Static registration for the Wasm filter. @see RegisterFactory.
*/
static Registry::RegisterFactory<WasmFilterConfig,
Server::Configuration::NamedHttpFilterConfigFactory>
register_;
REGISTER_FACTORY(WasmFilterConfig, Server::Configuration::NamedHttpFilterConfigFactory);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And all the other filters used this REGISTER_FACTORY macro so added that too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL


} // namespace Wasm
} // namespace HttpFilters
Expand Down
18 changes: 18 additions & 0 deletions test/extensions/filters/http/wasm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ envoy_extension_cc_test(
"//test/test_common:environment_lib",
],
)

envoy_extension_cc_test(
name = "wasm_filter_stress_test",
srcs = envoy_select_wasm(["wasm_filter_stress_test.cc"]),
data = [
"//test/extensions/filters/http/wasm/test_data:modules",
],
extension_name = "envoy.filters.http.wasm",
external_deps = ["abseil_optional"],
deps = [
"//source/extensions/common/wasm:wasm_lib",
"//source/extensions/filters/http/wasm:config",
"//source/extensions/wasm:config",
"//test/proto:helloworld_proto_cc",
"//test/stress:stress_test_lib",
"//test/test_common:environment_lib",
],
)
2 changes: 1 addition & 1 deletion test/extensions/filters/http/wasm/test_data/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm
all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm http_callout_cpp.wasm grpc_callout_cpp.wasm

include ../../../../../../api/wasm/cpp/Makefile.base_lite
82 changes: 82 additions & 0 deletions test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// NOLINT(namespace-envoy)
#include <string>
#include <unordered_map>
#include "proxy_wasm_intrinsics.h"
#include "proxy_wasm_intrinsics_lite.pb.h"

class RequestContext : public Context {
public:
explicit RequestContext(uint32_t id, RootContext *root) : Context(id, root) {}

FilterHeadersStatus onRequestHeaders() override;
};

class ServiceContext : public RootContext {
public:
explicit ServiceContext(uint32_t id, StringView root_id) : RootContext(id, root_id) {}

void onStart() override {
callout_success_counter_ = defineMetric(MetricType::Counter, "test_callout_successes");
callout_failure_counter_ = defineMetric(MetricType::Counter, "test_callout_failures");
}

void incrementCalloutSuccesses(uint32_t inc_amount = 1U) {
incrementMetric(callout_success_counter_, inc_amount);
}

void incrementCalloutFailures(uint32_t inc_amount = 1U) {
incrementMetric(callout_failure_counter_, inc_amount);
}

private:
uint32_t callout_success_counter_;
uint32_t callout_failure_counter_;
};

static RegisterContextFactory
register_ExampleContext(CONTEXT_FACTORY(RequestContext), ROOT_FACTORY(ServiceContext));

class CalloutResponseHandler : public GrpcCallHandler<google::protobuf::Value> {
public:
CalloutResponseHandler(RequestContext *context)
: GrpcCallHandler<google::protobuf::Value>(context), context_(context) {}
void onCreateInitialMetadata() override {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we convert these from pure virtuals to having a default implementation of {} so that we can avoid requiring handlers which are used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, would you like me to do that in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing, just wondering if you thought that was a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm pretty neutral on that. Most people will copy the boilerplate anyways and then hack it up.

void onSuccess(google::protobuf::Value &&response) override {
logDebug(response.string_value());

serviceContext()->incrementCalloutSuccesses();

continueRequest();
}
void onFailure(GrpcStatus status,
std::unique_ptr<WasmData> error_message) override {
logInfo(std::string("failure ") + std::to_string(static_cast<int>(status)) +
std::string(error_message->view()));

serviceContext()->incrementCalloutFailures();

// TODO wasm engine must support fail closed: expose abortRequest() or similar
continueRequest();
}

private:
ServiceContext *serviceContext() {
return static_cast<ServiceContext *>(context_->root());
}

RequestContext *context_;
};

FilterHeadersStatus RequestContext::onRequestHeaders() {
GrpcService grpc_service;
grpc_service.mutable_envoy_grpc()->set_cluster_name("callout_cluster");
std::string grpc_service_string;
grpc_service.SerializeToString(&grpc_service_string);

google::protobuf::Value value;
value.set_string_value("request");

grpcCallHandler(grpc_service_string, "service", "method", value, 1000,
std::unique_ptr<GrpcCallHandlerBase>(new CalloutResponseHandler(this)));
return FilterHeadersStatus::StopIteration;
}
Binary file not shown.
87,448 changes: 87,448 additions & 0 deletions test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.wat

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions test/extensions/filters/http/wasm/test_data/http_callout_cpp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// NOLINT(namespace-envoy)
#include <string>
#include <unordered_map>

#include "proxy_wasm_intrinsics.h"

class ExampleContext : public Context {
public:
explicit ExampleContext(uint32_t id, RootContext *root) : Context(id, root) {}

FilterHeadersStatus onRequestHeaders() override;
};
static RegisterContextFactory
register_ExampleContext(CONTEXT_FACTORY(ExampleContext));

FilterHeadersStatus ExampleContext::onRequestHeaders() {
try {
WasmDataPtr data = getRequestHeader("x-callout-url");

if (!data) {
logWarn("Missing x-callout-url header, cannot forward");
return FilterHeadersStatus::Continue;
}

StringView callout_url{data->view()};

logInfo("Forwarding to: " + std::string(callout_url));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jplevyak take note. After 20 or so iterations the callout_url will log something that looks corrupted

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we can use fmt::format here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is disable for the exception issue.


auto callback = [](std::unique_ptr<WasmData> response_headers,
std::unique_ptr<WasmData> body,
std::unique_ptr<WasmData> response_trailers) {
logInfo("Got response");
};
httpCall(callout_url,
{{":method", "POST"}, {":path", "/"}, {":authority", "foo"}},
"hello world",
{{"trail", "cow"}},
1000,
callback);
} catch (const std::exception &ex) {
logError("Caught exception: " + std::string{ex.what()});
} catch (const std::string &ex) {
logError("Caught exception: " + ex);
} catch (...) {
logError("Caught exception");
}

return FilterHeadersStatus::StopIteration;
}

Binary file not shown.
Loading