Line data Source code
1 : #include "source/extensions/access_loggers/wasm/config.h" 2 : 3 : #include "envoy/extensions/access_loggers/wasm/v3/wasm.pb.validate.h" 4 : #include "envoy/registry/registry.h" 5 : #include "envoy/server/filter_config.h" 6 : 7 : #include "source/common/common/logger.h" 8 : #include "source/common/protobuf/protobuf.h" 9 : #include "source/extensions/access_loggers/wasm/wasm_access_log_impl.h" 10 : #include "source/extensions/common/wasm/wasm.h" 11 : 12 : namespace Envoy { 13 : namespace Extensions { 14 : namespace AccessLoggers { 15 : namespace Wasm { 16 : 17 : using Common::Wasm::PluginHandleSharedPtrThreadLocal; 18 : 19 : AccessLog::InstanceSharedPtr 20 : WasmAccessLogFactory::createAccessLogInstance(const Protobuf::Message& proto_config, 21 : AccessLog::FilterPtr&& filter, 22 0 : Server::Configuration::FactoryContext& context) { 23 0 : const auto& config = MessageUtil::downcastAndValidate< 24 0 : const envoy::extensions::access_loggers::wasm::v3::WasmAccessLog&>( 25 0 : proto_config, context.messageValidationVisitor()); 26 : 27 0 : auto plugin = std::make_shared<Common::Wasm::Plugin>( 28 0 : config.config(), envoy::config::core::v3::TrafficDirection::UNSPECIFIED, 29 0 : context.serverFactoryContext().localInfo(), nullptr /* listener_metadata */); 30 : 31 0 : auto access_log = std::make_shared<WasmAccessLog>(plugin, nullptr, std::move(filter)); 32 : 33 0 : auto callback = [access_log, &context, plugin](Common::Wasm::WasmHandleSharedPtr base_wasm) { 34 : // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. 35 0 : auto tls_slot = ThreadLocal::TypedSlot<PluginHandleSharedPtrThreadLocal>::makeUnique( 36 0 : context.serverFactoryContext().threadLocal()); 37 0 : tls_slot->set([base_wasm, plugin](Event::Dispatcher& dispatcher) { 38 0 : return std::make_shared<PluginHandleSharedPtrThreadLocal>( 39 0 : Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); 40 0 : }); 41 0 : access_log->setTlsSlot(std::move(tls_slot)); 42 0 : }; 43 : 44 0 : if (!Common::Wasm::createWasm( 45 0 : plugin, context.scope().createScope(""), context.serverFactoryContext().clusterManager(), 46 0 : context.initManager(), context.serverFactoryContext().mainThreadDispatcher(), 47 0 : context.serverFactoryContext().api(), context.serverFactoryContext().lifecycleNotifier(), 48 0 : remote_data_provider_, std::move(callback))) { 49 0 : throw Common::Wasm::WasmException( 50 0 : fmt::format("Unable to create Wasm access log {}", plugin->name_)); 51 0 : } 52 : 53 0 : context.serverFactoryContext().api().customStatNamespaces().registerStatNamespace( 54 0 : Extensions::Common::Wasm::CustomStatNamespace); 55 0 : return access_log; 56 0 : } 57 : 58 2 : ProtobufTypes::MessagePtr WasmAccessLogFactory::createEmptyConfigProto() { 59 2 : return ProtobufTypes::MessagePtr{ 60 2 : new envoy::extensions::access_loggers::wasm::v3::WasmAccessLog()}; 61 2 : } 62 : 63 42 : std::string WasmAccessLogFactory::name() const { return "envoy.access_loggers.wasm"; } 64 : 65 : /** 66 : * Static registration for the wasm access log. @see RegisterFactory. 67 : */ 68 : LEGACY_REGISTER_FACTORY(WasmAccessLogFactory, Envoy::AccessLog::AccessLogInstanceFactory, 69 : "envoy.wasm_access_log"); 70 : 71 : } // namespace Wasm 72 : } // namespace AccessLoggers 73 : } // namespace Extensions 74 : } // namespace Envoy