Skip to content

Commit

Permalink
[EXPORTER] Fix forward protocol encoding for ETW exporter (#2473)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Jan 17, 2024
1 parent 423ecac commit f3132e5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
22 changes: 14 additions & 8 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
#include "opentelemetry/exporters/etw/etw_fields.h"
#include "opentelemetry/exporters/etw/utils.h"

#ifdef HAVE_MSGPACK
# include "nlohmann/json.hpp"
#endif

#include "opentelemetry/exporters/etw/etw_traceloggingdynamic.h"

#include <map>
Expand Down Expand Up @@ -241,7 +237,7 @@ class ETWProvider
}

unsigned long writeMsgPack(Handle &providerData,
exporter::etw::Properties &eventData,
Properties &eventData,
LPCGUID ActivityId = nullptr,
LPCGUID RelatedActivityId = nullptr,
uint8_t Opcode = 0)
Expand Down Expand Up @@ -286,7 +282,6 @@ class ETWProvider
/* clang-format off */
nlohmann::json jObj =
{
{ ETW_FIELD_NAME, eventName },
{ ETW_FIELD_OPCODE, Opcode }
};
/* clang-format on */
Expand Down Expand Up @@ -367,7 +362,18 @@ class ETWProvider
}
}

std::vector<uint8_t> v = nlohmann::json::to_msgpack(jObj);
// forwardMessage.push_back(nameField);
nlohmann::json payloadPair = nlohmann::json::array();

payloadPair.push_back(
utils::GetMsgPackEventTimeFromSystemTimestamp(std::chrono::system_clock::now()));
payloadPair.push_back(jObj);

nlohmann::json payloadArray = nlohmann::json::array({payloadPair});

nlohmann::json forwardMessage = nlohmann::json::array({eventName, payloadArray});

std::vector<uint8_t> v = nlohmann::json::to_msgpack(forwardMessage);

EVENT_DESCRIPTOR evtDescriptor;
// TODO: event descriptor may be populated with additional values as follows:
Expand All @@ -377,7 +383,7 @@ class ETWProvider
// Level - verbosity level
// Task - TaskId
// Opcode - described in evntprov.h:259 : 0 - info, 1 - activity start, 2 - activity stop.
EventDescCreate(&evtDescriptor, 0, 0x1, 0, 0, 0, Opcode, 0);
EventDescCreate(&evtDescriptor, 100, 0x1, 0, 0, 0, Opcode, 0);
EVENT_DATA_DESCRIPTOR evtData[1];
EventDataDescCreate(&evtData[0], v.data(), static_cast<ULONG>(v.size()));
ULONG writeResponse = 0;
Expand Down
49 changes: 46 additions & 3 deletions exporters/etw/include/opentelemetry/exporters/etw/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>

#include "opentelemetry/common/macros.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/exporters/etw/uuid.h"
#include "opentelemetry/version.h"

Expand All @@ -27,11 +28,12 @@
# include <codecvt>
#endif

#if defined(ENABLE_ENV_PROPERTIES)

#if defined(ENABLE_ENV_PROPERTIES) || defined(HAVE_MSGPACK)
# include <nlohmann/json.hpp>
# include "etw_properties.h"
#endif

#if defined(ENABLE_ENV_PROPERTIES)
# include "etw_properties.h"
#endif

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -383,6 +385,47 @@ static inline void PopulateAttribute(nlohmann::json &attribute,

#endif // defined(ENABLE_ENV_PROPERTIES)

#if defined(HAVE_MSGPACK)

static inline nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>
get_msgpack_eventtimeext(int32_t seconds = 0, int32_t nanoseconds = 0)
{
if ((seconds == 0) && (nanoseconds == 0))
{
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
auto duration = tp.time_since_epoch();
seconds =
static_cast<int32_t>(std::chrono::duration_cast<std::chrono::seconds>(duration).count());
nanoseconds = static_cast<int32_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000);
}

uint64_t timestamp =
((seconds / 100) & ((1ull << 34) - 1)) |
(((seconds % 100 * 10000000 + nanoseconds / 100) & ((1ull << 30) - 1)) << 34);

nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> ts{std::vector<uint8_t>(8)};

*reinterpret_cast<uint64_t *>(ts.data()) = timestamp;
ts.set_subtype(0x00);

return ts;
}

static inline nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>
GetMsgPackEventTimeFromSystemTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept
{
return get_msgpack_eventtimeext(
// Add all whole seconds to the event time
static_cast<int32_t>(
std::chrono::duration_cast<std::chrono::seconds>(timestamp.time_since_epoch()).count()),
// Add any remaining nanoseconds past the last whole second
std::chrono::duration_cast<std::chrono::nanoseconds>(timestamp.time_since_epoch()).count() %
1000000000);
}

#endif // defined(HAVE_MSGPACK)

}; // namespace utils

OPENTELEMETRY_END_NAMESPACE

2 comments on commit f3132e5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: f3132e5 Previous: 423ecac Ratio
BM_SpinLockThrashing/1/process_time/real_time 8.329852152678926 ms/iter 3.518400192260742 ms/iter 2.37
BM_ThreadYieldSpinLockThrashing/1/process_time/real_time 26.12626552581787 ms/iter 7.326991934525339 ms/iter 3.57

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: f3132e5 Previous: 423ecac Ratio
BM_AttributseHashMap 67485332.48901367 ns/iter 32294205.256870814 ns/iter 2.09

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.