Skip to content

Commit

Permalink
grpc: add support for templated header() values
Browse files Browse the repository at this point in the history
It must be enabled in the derived class, and
prepare_context_dynamic() must be used if so.

This is not part of this commit, enabling it will be
done in subsequent commits.

Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Oct 12, 2024
1 parent 090118e commit 1237e18
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
35 changes: 34 additions & 1 deletion modules/grpc/common/grpc-dest-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

#include "grpc-dest-worker.hpp"

#include "compat/cpp-start.h"
#include "scratch-buffers.h"
#include "compat/cpp-end.h"

using namespace syslogng::grpc;

/* C++ Implementations */
Expand Down Expand Up @@ -95,8 +99,37 @@ DestWorker::disconnect()
void
DestWorker::prepare_context(::grpc::ClientContext &context)
{
g_assert(!this->owner.dynamic_headers_enabled);

for (auto nv : owner.headers)
context.AddMetadata(nv.name, nv.value->template_str);
}

void
DestWorker::prepare_context_dynamic(::grpc::ClientContext &context, LogMessage *msg)
{
g_assert(this->owner.dynamic_headers_enabled);

LogTemplateEvalOptions options = {&this->owner.template_options, LTZ_SEND, this->super->super.seq_num, NULL,
LM_VT_STRING
};

ScratchBuffersMarker marker;
GString *buf = scratch_buffers_alloc_and_mark(&marker);

for (auto nv : owner.headers)
context.AddMetadata(nv.first, nv.second);
{
if (log_template_is_literal_string(nv.value))
{
context.AddMetadata(nv.name, log_template_get_literal_value(nv.value, NULL));
continue;
}

log_template_format(nv.value, msg, &options, buf);
context.AddMetadata(nv.name, buf->str);
}

scratch_buffers_reclaim_marked(marker);
}

/* C Wrappers */
Expand Down
1 change: 1 addition & 0 deletions modules/grpc/common/grpc-dest-worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class DestWorker

protected:
void prepare_context(::grpc::ClientContext &context);
void prepare_context_dynamic(::grpc::ClientContext &context, LogMessage *msg);
std::shared_ptr<::grpc::ChannelCredentials> create_credentials();
::grpc::ChannelArguments create_channel_args();

Expand Down
8 changes: 4 additions & 4 deletions modules/grpc/common/grpc-dest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace syslogng::grpc;
DestDriver::DestDriver(GrpcDestDriver *s)
: super(s), compression(false), batch_bytes(4 * 1000 * 1000),
keepalive_time(-1), keepalive_timeout(-1), keepalive_max_pings_without_data(-1),
flush_on_key_change(false)
flush_on_key_change(false), dynamic_headers_enabled(false)
{
log_template_options_defaults(&this->template_options);
credentials_builder_wrapper.self = &credentials_builder;
Expand Down Expand Up @@ -169,11 +169,11 @@ grpc_dd_add_string_channel_arg(LogDriver *s, const gchar *name, const gchar *val
self->cpp->add_extra_channel_arg(name, value);
}

void
grpc_dd_add_header(LogDriver *s, const gchar *name, const gchar *value)
gboolean
grpc_dd_add_header(LogDriver *s, const gchar *name, LogTemplate *value)
{
GrpcDestDriver *self = (GrpcDestDriver *) s;
self->cpp->add_header(name, value);
return self->cpp->add_header(name, value);
}

LogTemplateOptions *
Expand Down
2 changes: 1 addition & 1 deletion modules/grpc/common/grpc-dest.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void grpc_dd_set_keepalive_timeout(LogDriver *s, gint t);
void grpc_dd_set_keepalive_max_pings(LogDriver *s, gint p);
void grpc_dd_add_int_channel_arg(LogDriver *s, const gchar *name, glong value);
void grpc_dd_add_string_channel_arg(LogDriver *s, const gchar *name, const gchar *value);
void grpc_dd_add_header(LogDriver *s, const gchar *name, const gchar *value);
gboolean grpc_dd_add_header(LogDriver *s, const gchar *name, LogTemplate *value);
LogTemplateOptions *grpc_dd_get_template_options(LogDriver *d);
GrpcClientCredentialsBuilderW *grpc_dd_get_credentials_builder(LogDriver *s);

Expand Down
23 changes: 20 additions & 3 deletions modules/grpc/common/grpc-dest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,24 @@ class DestDriver
this->string_extra_channel_args.push_back(std::make_pair(name, value));
}

void add_header(std::string name, std::string value)
bool add_header(std::string name, LogTemplate *value)
{
bool is_literal_string = log_template_is_literal_string(value);

if (!this->dynamic_headers_enabled && !is_literal_string)
return false;

std::transform(name.begin(), name.end(), name.begin(),
[](auto c)
{
return ::tolower(c);
});
this->headers.push_back(std::make_pair(name, value));
this->headers.push_back(NameValueTemplatePair{name, value});

if (!is_literal_string)
this->extend_worker_partition_key(value->template_str);

return true;
}

void extend_worker_partition_key(const std::string &extension)
Expand All @@ -153,6 +163,12 @@ class DestDriver
this->worker_partition_key << extension;
}

void enable_dynamic_headers()
{
this->dynamic_headers_enabled = true;
this->flush_on_key_change = true;
}

LogTemplateOptions &get_template_options()
{
return this->template_options;
Expand Down Expand Up @@ -188,7 +204,8 @@ class DestDriver
std::list<std::pair<std::string, long>> int_extra_channel_args;
std::list<std::pair<std::string, std::string>> string_extra_channel_args;

std::list<std::pair<std::string, std::string>> headers;
std::list<NameValueTemplatePair> headers;
bool dynamic_headers_enabled;

LogTemplateOptions template_options;

Expand Down
7 changes: 6 additions & 1 deletion modules/grpc/common/grpc-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ grpc_dest_headers
;

grpc_dest_header
: string LL_ARROW string { grpc_dd_add_header(last_driver, $1, $3); free($1); free($3); }
: string LL_ARROW template_name_or_content
{
CHECK_ERROR(grpc_dd_add_header(last_driver, $1, $3), @3, "Failed to set header: templating is not supported");
free($1);
log_template_unref($3);
}
;

grpc_server_credentials_builder_option
Expand Down

0 comments on commit 1237e18

Please sign in to comment.