etw_logger_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #ifdef _WIN32
  4. # include <gtest/gtest.h>
  5. # include <map>
  6. # include <set>
  7. # include <string>
  8. # define OPENTELEMETRY_ATTRIBUTE_TIMESTAMP_PREVIEW
  9. # include "opentelemetry/exporters/etw/etw_logger_exporter.h"
  10. # include "opentelemetry/sdk/trace/simple_processor.h"
  11. using namespace OPENTELEMETRY_NAMESPACE;
  12. using namespace opentelemetry::exporter::etw;
  13. // The ETW provider ID is {4533CB59-77E2-54E9-E340-F0F0549058B7}
  14. const char *kGlobalProviderName = "OpenTelemetry-ETW-TLD";
  15. /**
  16. * @brief Logger test with name and unstructured text
  17. * {
  18. * "Timestamp": "2021-09-30T16:40:40.0820563-07:00",
  19. * "ProviderName": "OpenTelemetry-ETW-TLD",
  20. * "Id": 2,
  21. * "Message": null,
  22. * "ProcessId": 23180,
  23. * "Level": "Always",
  24. * "Keywords": "0x0000000000000000",
  25. * "EventName": "Log",
  26. * "ActivityID": null,
  27. * "RelatedActivityID": null,
  28. * "Payload": {
  29. * "Name": "test",
  30. * "SpanId": "0000000000000000",
  31. * "Timestamp": "2021-09-30T23:40:40.066411500Z",
  32. * "TraceId": "00000000000000000000000000000000",
  33. * "_name": "Log",
  34. * "body": "This is test message",
  35. * "severityNumber": 5,
  36. * "severityText": "DEBUG"
  37. * }
  38. * }
  39. */
  40. TEST(ETWLogger, LoggerCheckWithBody)
  41. {
  42. std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
  43. exporter::etw::LoggerProvider lp;
  44. const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
  45. auto logger = lp.GetLogger(providerName, schema_url);
  46. Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
  47. EXPECT_NO_THROW(
  48. logger->EmitLogRecord(opentelemetry::logs::Severity::kDebug, "This is test log body"));
  49. }
  50. /**
  51. * @brief Logger Test with structured attributes
  52. *
  53. * Example Event for below test:
  54. * {
  55. * "Timestamp": "2021-09-30T15:04:15.4227815-07:00",
  56. * "ProviderName": "OpenTelemetry-ETW-TLD",
  57. * "Id": 1,
  58. * "Message": null,
  59. * "ProcessId": 33544,
  60. * "Level": "Always",
  61. * "Keywords": "0x0000000000000000",
  62. * "EventName": "Log",
  63. * "ActivityID": null,
  64. * "RelatedActivityID": null,
  65. * "Payload": {
  66. * "Name": "test",
  67. * "SpanId": "0000000000000000",
  68. * "Timestamp": "2021-09-30T22:04:15.066411500Z",
  69. * "TraceId": "00000000000000000000000000000000",
  70. * "_name": "Log",
  71. * "attrib1": 1,
  72. * "attrib2": 2,
  73. * "body": "",
  74. * "severityNumber": 5,
  75. * "severityText": "DEBUG"
  76. * }
  77. * }
  78. *
  79. */
  80. TEST(ETWLogger, LoggerCheckWithAttributes)
  81. {
  82. std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
  83. exporter::etw::LoggerProvider lp;
  84. const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
  85. auto logger = lp.GetLogger(providerName, schema_url);
  86. // Log attributes
  87. Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
  88. EXPECT_NO_THROW(logger->EmitLogRecord(opentelemetry::logs::Severity::kDebug,
  89. opentelemetry::common::MakeAttributes(attribs)));
  90. }
  91. /**
  92. * @brief Logger Test with structured attributes
  93. *
  94. * Example Event for below test:
  95. * {
  96. * "Timestamp": "2024-06-02T15:04:15.4227815-07:00",
  97. * "ProviderName": "OpenTelemetry-ETW-TLD",
  98. * "Id": 1,
  99. * "Message": null,
  100. * "ProcessId": 37696,
  101. * "Level": "Always",
  102. * "Keywords": "0x0000000000000000",
  103. * "EventName": "table1",
  104. * "ActivityID": null,
  105. * "RelatedActivityID": null,
  106. * "Payload": {
  107. * "SpanId": "0000000000000000",
  108. * "Timestamp": "2021-09-30T22:04:15.066411500Z",
  109. * "TraceId": "00000000000000000000000000000000",
  110. * "_name": "table1",
  111. * "attrib1": 1,
  112. * "attrib2": "value2",
  113. * "body": "This is a debug log body",
  114. * "severityNumber": 5,
  115. * "severityText": "DEBUG"
  116. * }
  117. * }
  118. *
  119. */
  120. TEST(ETWLogger, LoggerCheckWithTableNameMappings)
  121. {
  122. std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
  123. std::map<std::string, std::string> tableNameMappings = {{"name1", "table1"}, {"name2", "table2"}};
  124. exporter::etw::TelemetryProviderOptions options = {{"enableTableNameMappings", true},
  125. {"tableNameMappings", tableNameMappings}};
  126. exporter::etw::LoggerProvider lp{options};
  127. auto logger = lp.GetLogger(providerName, "name1");
  128. // Log attributes
  129. Properties attribs = {{"attrib1", 1}, {"attrib2", "value2"}};
  130. EXPECT_NO_THROW(
  131. logger->Debug("This is a debug log body", opentelemetry::common::MakeAttributes(attribs)));
  132. }
  133. /**
  134. * @brief Logger Test with structured attributes
  135. *
  136. * Example Event for below test:
  137. * {
  138. * "Timestamp": "2024-06-02T15:04:15.4227815-07:00",
  139. * "ProviderName": "OpenTelemetry-ETW-TLD",
  140. * "Id": 1,
  141. * "Message": null,
  142. * "ProcessId": 37696,
  143. * "Level": "Always",
  144. * "Keywords": "0x0000000000000000",
  145. * "EventName": "table1",
  146. * "ActivityID": null,
  147. * "RelatedActivityID": null,
  148. * "Payload": {
  149. * "SpanId": "0000000000000000",
  150. * "Timestamp": "2021-09-30T22:04:15.066411500Z",
  151. * "TraceId": "00000000000000000000000000000000",
  152. * "_name": "table1",
  153. * "tiemstamp1": "2025-02-20T19:18:11.048166700Z",
  154. * "attrib2": "value2",
  155. * "body": "This is a debug log body",
  156. * "severityNumber": 5,
  157. * "severityText": "DEBUG"
  158. * }
  159. * }
  160. *
  161. */
  162. TEST(ETWLogger, LoggerCheckWithTimestampAttributes)
  163. {
  164. std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
  165. std::set<std::string> timestampAttributes = {{"timestamp1"}};
  166. exporter::etw::TelemetryProviderOptions options = {{"timestampAttributes", timestampAttributes}};
  167. exporter::etw::LoggerProvider lp{options};
  168. auto logger = lp.GetLogger(providerName, "name1");
  169. // Log attributes
  170. Properties attribs = {{"timestamp1", 133845526910481667ULL}, {"attrib2", "value2"}};
  171. EXPECT_NO_THROW(
  172. logger->Debug("This is a debug log body", opentelemetry::common::MakeAttributes(attribs)));
  173. }
  174. #endif // _WIN32