-
Notifications
You must be signed in to change notification settings - Fork 479
[EXPORTER] Refactor ElasticSearchRecordable
#3164
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
Conversation
✅ Deploy Preview for opentelemetry-cpp-api-docs canceled.
|
{ | ||
static void to_json(json &j, const opentelemetry::sdk::common::OwnedAttributeValue &v) | ||
{ | ||
opentelemetry::nostd::visit([&j](const auto &value) { j = value; }, v); |
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.
A possible breaking change here is that the original WriteValue()
did not handle spans. If this is important, I can add an if
condition to follow the old behavior.
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.
I just bumped into your changes and I was wondering if this could help putting json objects within the root json ?
The modifications I did was kind of a workaround so we could add keys like user.id
that would be visible within ELK like the documentation asks for but the key was still user.id
and not an object user
under root
that would have a key id
My hope would be to be able to be able to create an attribute or resource that contains a json object for some keys:
auto attr = { "user": R"({"id": 1234}"_json};
char trace_buf[32]; | ||
char trace_buf[opentelemetry::trace::TraceId::kSize * 2]; |
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.
This approach is safer: should kSize
change someday, the buffer's size will be automatically updated.
29b912b
to
737c785
Compare
|
||
std::strcat(bufferDate, bufferMilliseconds); | ||
std::strcat(bufferDate, "Z"); | ||
std::ostringstream oss; |
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.
Why change it to put_time
and ostringstream
? In my understandarding, using strftime
and snprintf
with just one static buffer block will have better performence than using ostringstream
and put_time
with dynamic memory allocation. And there are also some compatiblity problems with put_time
and old compiler's STL.
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.
A newer version of gcc complained:
warning: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
47 | std::snprintf(bufferMilliseconds, sizeof(bufferMilliseconds), ".%06ld",
| ^
In function ‘int snprintf(char*, size_t, const char*, ...)’,
/usr/include/x86_64-linux-gnu/bits/stdio2.h:54:35: note: ‘__builtin___snprintf_chk’ output between 8 and 9 bytes into a destination of size 8
54 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 | __glibc_objsize (__s), __fmt,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 | __va_arg_pack ());
| ~~~~~~~~~~~~~~~~~
so I tried to come up with something safe that does not involve hardcoded sizes, compiles cleanly, does not have a potential for buffer overflows, and is as fast as std::format("{:%FT%T%Ez}", timePoint)
.
For example, in
const static int dateToSecondsSize = 19;
const static int millisecondsSize = 8;
it was not immediately evident that millisecondsSize
reserves one byte for the null terminator (.123456
is seven bytes, not eight), and it felt like dateSize = dateToSecondsSize + millisecondsSize + timeZoneSize; char bufferDate[dateSize];
could lead to a buffer overflow.
Anyway, I think I have found a faster solution not involving strcat
and hardcoded buffer sizes.
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.
This can be fixed by declaring bufferMilliseconds
as char bufferMilliseconds[millisecondsSize + 1];
. And change the snprinf calling to std::snprintf(bufferMilliseconds, sizeof(bufferMilliseconds) - 1, ".%06ld", static_cast<long>(microseconds.count()));
. Thera are a little different between the bebaviour of snprinf in MSVC and GCC.
Anyway, I think I have found a faster solution not involving strcat and hardcoded buffer sizes.
The new way seems better, and thanks.
7a27ac3
to
9d4cc32
Compare
9d4cc32
to
b72c41b
Compare
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.
LGTM and thanks.
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.
LGTM, thanks for the fix.
Changes
ElasticSearchRecordable::WriteValue()
by using the ADL Serializer;ElasticSearchRecordable::SetTimestamp()
;ElasticSearchRecordable::SetTraceId()
andElasticSearchRecordable::SetSpanId()
by using constants for array sizes instead of hardcoded values.For significant contributions please make sure you have completed the following items:
CHANGELOG.md
updated for non-trivial changes