-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of support for custom resource attributes #54
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,11 @@ | |
#include "trace_context.hpp" | ||
#include "trace_service_client.hpp" | ||
|
||
struct ResourceAttr { | ||
ngx_str_t name; | ||
ngx_http_complex_value_t value; | ||
}; | ||
|
||
class BatchExporter { | ||
public: | ||
typedef TraceServiceClient::Request Request; | ||
|
@@ -112,7 +117,7 @@ class BatchExporter { | |
}; | ||
|
||
BatchExporter(StrView target, | ||
size_t batchSize, size_t batchCount, StrView serviceName) : | ||
size_t batchSize, size_t batchCount, StrView serviceName, ngx_array_t customResourceAttrs) : | ||
batchSize(batchSize), client(std::string(target)) | ||
{ | ||
free.reserve(batchCount); | ||
|
@@ -124,6 +129,15 @@ class BatchExporter { | |
attr->set_key("service.name"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have explicit |
||
attr->mutable_value()->set_string_value(std::string(serviceName)); | ||
|
||
auto attrs = (ResourceAttr*)customResourceAttrs.elts; | ||
for (ngx_uint_t i = 0; i < customResourceAttrs.nelts; i++) { | ||
attr = resourceSpans->mutable_resource()->add_attributes(); | ||
StrView value = StrView((char*)attrs[i].value.value.data, attrs[i].value.value.len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not how complex values work. We can't really use complex value here, as those require client request to be evaluated. |
||
StrView name = StrView((char*)attrs[i].name.data, attrs[i].name.len); | ||
attr->set_key(std::string(name)); | ||
attr->mutable_value()->set_string_value(std::string(value)); | ||
} | ||
|
||
auto scopeSpans = resourceSpans->add_scope_spans(); | ||
scopeSpans->mutable_scope()->set_name("nginx"); | ||
scopeSpans->mutable_scope()->set_version(NGINX_VERSION); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ struct MainConf { | |
size_t batchCount; | ||
|
||
ngx_str_t serviceName; | ||
ngx_array_t resourceAttrs; | ||
}; | ||
|
||
struct SpanAttr { | ||
|
@@ -39,6 +40,7 @@ struct LocationConf { | |
|
||
char* setExporter(ngx_conf_t* cf, ngx_command_t* cmd, void* conf); | ||
char* addSpanAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf); | ||
char* addResourceAttrs(ngx_conf_t* cf, ngx_command_t* cmd, void* conf); | ||
|
||
namespace Propagation { | ||
|
||
|
@@ -67,6 +69,12 @@ ngx_command_t gCommands[] = { | |
ngx_conf_set_str_slot, | ||
NGX_HTTP_MAIN_CONF_OFFSET, | ||
offsetof(MainConf, serviceName) }, | ||
|
||
{ ngx_string("otel_resource_attr"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was to call the block "otel_resource" and, perhaps, add some other resource related options to it later on (or even support multiple resources). "otel_resource_attr" name makes more sense for single attribute directive, like |
||
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | ||
addResourceAttrs, | ||
NGX_HTTP_MAIN_CONF_OFFSET, | ||
offsetof(MainConf, resourceAttrs) }, | ||
|
||
{ ngx_string("otel_trace"), | ||
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
|
@@ -455,6 +463,7 @@ void addCustomAttrs(BatchExporter::Span& span, ngx_http_request_t* r) | |
} | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accidental edit? |
||
ngx_int_t onRequestEnd(ngx_http_request_t* r) | ||
{ | ||
auto ctx = getOtelCtx(r); | ||
|
@@ -540,7 +549,9 @@ ngx_int_t initWorkerProcess(ngx_cycle_t* cycle) | |
toStrView(mcf->endpoint), | ||
mcf->batchSize, | ||
mcf->batchCount, | ||
toStrView(mcf->serviceName))); | ||
toStrView(mcf->serviceName), | ||
mcf->resourceAttrs | ||
)); | ||
} catch (const std::exception& e) { | ||
ngx_log_error(NGX_LOG_CRIT, cycle->log, 0, | ||
"OTel worker init error: %s", e.what()); | ||
|
@@ -705,6 +716,58 @@ char* addSpanAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) | |
return NGX_CONF_OK; | ||
} | ||
|
||
char* addResourceAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) { | ||
auto mcf = (MainConf*)conf; | ||
|
||
if (mcf->resourceAttrs.elts == NULL && ngx_array_init(&mcf->resourceAttrs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a similar code below. Is that intentional? |
||
cf->pool, 4, sizeof(ResourceAttr)) != NGX_OK) { | ||
return (char*)NGX_CONF_ERROR; | ||
} | ||
|
||
auto attr = (ResourceAttr*)ngx_array_push(&mcf->resourceAttrs); | ||
if (attr == NULL) { | ||
return (char*)NGX_CONF_ERROR; | ||
} | ||
|
||
auto args = (ngx_str_t*)cf->args->elts; | ||
|
||
attr->name = args[0]; | ||
|
||
ngx_http_compile_complex_value_t ccv = { cf, &args[1], &attr->value }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if there is only one arg? |
||
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
return (char*)NGX_CONF_ERROR; | ||
} | ||
|
||
return NGX_CONF_OK; | ||
} | ||
|
||
char* addResourceAttrs(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) | ||
{ | ||
auto mcf = (MainConf*)conf; | ||
|
||
if (mcf->resourceAttrs.elts == NULL && ngx_array_init(&mcf->resourceAttrs, | ||
cf->pool, 4, sizeof(ResourceAttr)) != NGX_OK) { | ||
return (char*)NGX_CONF_ERROR; | ||
} | ||
|
||
auto cfCopy = *cf; | ||
|
||
cfCopy.handler = addResourceAttr; | ||
cfCopy.handler_conf = mcf; | ||
auto rv = ngx_conf_parse(&cfCopy, NULL); | ||
if (rv != NGX_CONF_OK) { | ||
return rv; | ||
} | ||
|
||
if (mcf->endpoint.len == 0) { | ||
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
"\"otel_exporter\" requires \"endpoint\""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this related? |
||
return (char*)NGX_CONF_ERROR; | ||
} | ||
|
||
return NGX_CONF_OK; | ||
} | ||
|
||
template <class Id> | ||
ngx_int_t hexIdVar(ngx_http_request_t* r, ngx_http_variable_value_t* v, | ||
uintptr_t data) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,10 @@ http { | |
batch_count 1; | ||
} | ||
|
||
otel_resource_attr { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given, these attributes are not HTTP version related, I don't think we need to add them into h2/h3 tests. |
||
service.namespace test; | ||
} | ||
|
||
otel_service_name test_server; | ||
otel_trace on; | ||
|
||
|
@@ -159,7 +163,7 @@ foreach my $name ('localhost') { | |
or die "Can't create certificate for $name: $!\n"; | ||
} | ||
|
||
$t->try_run('no OTel module')->plan(69); | ||
$t->try_run('no OTel module')->plan(70); | ||
|
||
############################################################################### | ||
|
||
|
@@ -211,6 +215,9 @@ is(scalar keys %{$$batch1{scope_spans}}, 5, 'batch1 size - trace on'); | |
is(get_attr("service.name", "string_value", | ||
$$batch0{resource}), | ||
'test_server', 'service.name - trace on'); | ||
is(get_attr("service.namespace", "string_value", | ||
$$batch0{resource}), | ||
'test', 'service.namespace - trace on'); | ||
is($$spans{span0}{name}, '"default_location"', 'span.name - trace on'); | ||
|
||
#validate http metrics | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,10 @@ http { | |
batch_count 2; | ||
} | ||
|
||
otel_resource_attr { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note indent. |
||
service.namespace test; | ||
} | ||
|
||
otel_service_name test_server; | ||
otel_trace on; | ||
|
||
|
@@ -156,7 +160,7 @@ foreach my $name ('localhost') { | |
or die "Can't create certificate for $name: $!\n"; | ||
} | ||
|
||
$t->try_run('no OTel module')->plan(69); | ||
$t->try_run('no OTel module')->plan(70); | ||
|
||
############################################################################### | ||
|
||
|
@@ -203,6 +207,9 @@ is(scalar keys %{$$batch1{scope_spans}}, 5, 'batch1 size - trace on'); | |
#validate general attributes | ||
is(get_attr("service.name", "string_value", | ||
$$batch0{resource}), 'test_server', 'service.name - trace on'); | ||
is(get_attr("service.namespace", "string_value", | ||
$$batch0{resource}), | ||
'test', 'service.namespace - trace on'); | ||
is($$spans{span0}{name}, '"default_location"', 'span.name - trace on'); | ||
|
||
#validate http metrics | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For most part, we try to avoid "ngx_*" types in this file and use C++ types. Looks like, for attributes we can use
const std::map<StrView, StrView>& attrs
.