Skip to content

Commit c33b884

Browse files
Add support for proxy_log_destination ABI
Signed-off-by: Vikas Choudhary <[email protected]>
1 parent 489a41f commit c33b884

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

api/envoy/extensions/wasm/v3/wasm.proto

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ message SanitizationConfig {
4040
}
4141

4242
// Configuration for a Wasm VM.
43-
// [#next-free-field: 8]
43+
// [#next-free-field: 9]
4444
message VmConfig {
4545
// An ID which will be used along with a hash of the wasm code (or the name of the registered Null
4646
// VM plugin) to determine which VM will be used for the plugin. All plugins which use the same
@@ -106,6 +106,19 @@ message VmConfig {
106106
// vars just like when you do on native platforms.
107107
// Warning: Envoy rejects the configuration if there's conflict of key space.
108108
EnvironmentVariables environment_variables = 7;
109+
110+
// Specifies the log destination for the plugin.
111+
// If not specified, the plugin will log to the default Envoy log
112+
// example: "audit-logs": { file_path: "/var/log/audit.log" }
113+
map<string, LogDestination> log_destination = 8;
114+
}
115+
116+
message LogDestination {
117+
// Target destination for the log messages from the plugin.
118+
oneof target {
119+
string file_path = 1;
120+
// TODO: Remote logging service
121+
}
109122
}
110123

111124
message EnvironmentVariables {

source/extensions/common/wasm/plugin.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ WasmConfig::WasmConfig(const envoy::extensions::wasm::v3::PluginConfig& config)
5252
}
5353
}
5454
}
55+
56+
if (config.vm_config().runtime() == "envoy.wasm.runtime.null" &&
57+
!config_.vm_config().log_destination().empty()) {
58+
throw EnvoyException("envoy.extensions.wasm.v3.VmConfig.log_destination must "
59+
"not be set for NullVm.");
60+
}
61+
// Check key duplication.
62+
absl::flat_hash_set<std::string> keys;
63+
for (const auto& ld : config_.vm_config().log_destination()) {
64+
keys.insert(ld.first);
65+
}
66+
for (const auto& ld : config_.vm_config().log_destination()) {
67+
if (!keys.insert(ld.first).second) {
68+
throw EnvoyException(fmt::format("Key {} is duplicated in "
69+
"envoy.extensions.wasm.v3.VmConfig.log_destination for {}. "
70+
"All the keys must be unique.",
71+
ld.first, config_.name()));
72+
}
73+
}
74+
// Construct merged key-value pairs. Also check for boundary conditions
75+
// (e.g. empty file path).
76+
for (const auto& ld : config_.vm_config().log_destination()) {
77+
if (ld.second.file_path().empty()) {
78+
throw EnvoyException(
79+
fmt::format("Key {} value envoy.extensions.wasm.v3.VmConfig.LogDestination.file_path "
80+
"must not be empty for {}.",
81+
ld.first, config_.name()));
82+
}
83+
log_destinations_[ld.first] = ld.second.file_path();
84+
}
5585
}
5686

5787
} // namespace Wasm

source/extensions/common/wasm/plugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Wasm {
1818

1919
// clang-format off
2020
using EnvironmentVariableMap = std::unordered_map<std::string, std::string>;
21+
using LogDestinationMap = std::unordered_map<std::string, std::string>;
2122
// clang-format on
2223

2324
class WasmConfig {
@@ -26,11 +27,13 @@ class WasmConfig {
2627
const envoy::extensions::wasm::v3::PluginConfig& config() { return config_; }
2728
proxy_wasm::AllowedCapabilitiesMap& allowedCapabilities() { return allowed_capabilities_; }
2829
EnvironmentVariableMap& environmentVariables() { return envs_; }
30+
LogDestinationMap& logDestinations() { return log_destinations_; }
2931

3032
private:
3133
const envoy::extensions::wasm::v3::PluginConfig config_;
3234
proxy_wasm::AllowedCapabilitiesMap allowed_capabilities_{};
3335
EnvironmentVariableMap envs_;
36+
LogDestinationMap log_destinations_;
3437
};
3538

3639
using WasmConfigPtr = std::unique_ptr<WasmConfig>;

source/extensions/common/wasm/wasm.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ void Wasm::initializeLifecycle(Server::ServerLifecycleNotifier& lifecycle_notifi
7575

7676
Wasm::Wasm(WasmConfig& config, absl::string_view vm_key, const Stats::ScopeSharedPtr& scope,
7777
Api::Api& api, Upstream::ClusterManager& cluster_manager, Event::Dispatcher& dispatcher)
78-
: WasmBase(
79-
createWasmVm(config.config().vm_config().runtime()), config.config().vm_config().vm_id(),
80-
MessageUtil::anyToBytes(config.config().vm_config().configuration()),
81-
toStdStringView(vm_key), config.environmentVariables(), config.allowedCapabilities()),
78+
: WasmBase(createWasmVm(config.config().vm_config().runtime()),
79+
config.config().vm_config().vm_id(),
80+
MessageUtil::anyToBytes(config.config().vm_config().configuration()),
81+
toStdStringView(vm_key), config.environmentVariables(), config.logDestinations(),
82+
config.allowedCapabilities()),
8283
scope_(scope), api_(api), stat_name_pool_(scope_->symbolTable()),
8384
custom_stat_namespace_(stat_name_pool_.add(CustomStatNamespace)),
8485
cluster_manager_(cluster_manager), dispatcher_(dispatcher),
@@ -330,7 +331,8 @@ bool createWasm(const PluginSharedPtr& plugin, const Stats::ScopeSharedPtr& scop
330331
code_cache = new std::remove_reference<decltype(*code_cache)>::type;
331332
}
332333
Stats::ScopeSharedPtr create_wasm_stats_scope = stats_handler.lockAndCreateStats(scope);
333-
// Remove entries older than CODE_CACHE_SECONDS_CACHING_TTL except for our target.
334+
// Remove entries older than CODE_CACHE_SECONDS_CACHING_TTL except for our
335+
// target.
334336
for (auto it = code_cache->begin(); it != code_cache->end();) {
335337
if (now - it->second.use_time > std::chrono::seconds(CODE_CACHE_SECONDS_CACHING_TTL) &&
336338
it->first != vm_config.code().remote().sha256()) {
@@ -423,8 +425,8 @@ bool createWasm(const PluginSharedPtr& plugin, const Stats::ScopeSharedPtr& scop
423425
}
424426
stats_handler.onRemoteCacheEntriesChanged(code_cache->size());
425427
}
426-
// NB: xDS currently does not support failing asynchronously, so we fail immediately
427-
// if remote Wasm code is not cached and do a background fill.
428+
// NB: xDS currently does not support failing asynchronously, so we fail
429+
// immediately if remote Wasm code is not cached and do a background fill.
428430
if (!vm_config.nack_on_code_cache_miss()) {
429431
if (code.empty()) {
430432
ENVOY_LOG_TO_LOGGER(Envoy::Logger::Registry::getLog(Envoy::Logger::Id::wasm), trace,

0 commit comments

Comments
 (0)