From f1b5fbdedd5beb7e7ca6aacfb3f430686d694d9c Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Sat, 23 Nov 2024 00:33:45 +0200 Subject: [PATCH] [TEST] add a test for ElasticSearchRecordable (#3154) --- .../test/es_log_record_exporter_test.cc | 92 ++++++++++++++----- 1 file changed, 69 insertions(+), 23 deletions(-) diff --git a/exporters/elasticsearch/test/es_log_record_exporter_test.cc b/exporters/elasticsearch/test/es_log_record_exporter_test.cc index 8a1c81da48..6bdd5248a5 100644 --- a/exporters/elasticsearch/test/es_log_record_exporter_test.cc +++ b/exporters/elasticsearch/test/es_log_record_exporter_test.cc @@ -2,50 +2,48 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/elasticsearch/es_log_record_exporter.h" -#include "opentelemetry/ext/http/server/http_server.h" -#include "opentelemetry/logs/provider.h" +#include "opentelemetry/common/timestamp.h" +#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h" +#include "opentelemetry/logs/severity.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" #include "opentelemetry/sdk/logs/exporter.h" -#include "opentelemetry/sdk/logs/logger_provider.h" -#include "opentelemetry/sdk/logs/simple_log_record_processor.h" +#include "opentelemetry/sdk/resource/resource.h" #include -#include +#include +#include namespace sdklogs = opentelemetry::sdk::logs; namespace logs_api = opentelemetry::logs; namespace nostd = opentelemetry::nostd; namespace logs_exporter = opentelemetry::exporter::logs; -TEST(ElasticsearchLogsExporterTests, Dummy) -{ - // to enable linking -} - -#if 0 // Attempt to write a log to an invalid host/port, test that the Export() returns failure -TEST(ElasticsearchLogsExporterTests, InvalidEndpoint) +TEST(DISABLED_ElasticsearchLogsExporterTests, InvalidEndpoint) { // Create invalid connection options for the elasticsearch exporter logs_exporter::ElasticsearchExporterOptions options("localhost", -1); // Create an elasticsearch exporter - auto exporter = - std::unique_ptr(new logs_exporter::ElasticsearchLogRecordExporter(options)); + auto exporter = std::unique_ptr( + new logs_exporter::ElasticsearchLogRecordExporter(options)); // Create a log record and send to the exporter auto record = exporter->MakeRecordable(); auto result = exporter->Export(nostd::span>(&record, 1)); // Ensure the return value is failure - ASSERT_EQ(result, sdk::common::ExportResult::kFailure); + ASSERT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure); } // Test that when the exporter is shutdown, any call to Export should return failure -TEST(ElasticsearchLogsExporterTests, Shutdown) +TEST(DISABLED_ElasticsearchLogsExporterTests, Shutdown) { // Create an elasticsearch exporter and immediately shut it down - auto exporter = - std::unique_ptr(new logs_exporter::ElasticsearchLogRecordExporter); + auto exporter = std::unique_ptr( + new logs_exporter::ElasticsearchLogRecordExporter); bool shutdownResult = exporter->Shutdown(); ASSERT_TRUE(shutdownResult); @@ -54,15 +52,15 @@ TEST(ElasticsearchLogsExporterTests, Shutdown) auto result = exporter->Export(nostd::span>(&record, 1)); // Ensure the return value is failure - ASSERT_EQ(result, sdk::common::ExportResult::kFailure); + ASSERT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure); } // Test the elasticsearch recordable object -TEST(ElasticsearchLogsExporterTests, RecordableCreation) +TEST(DISABLED_ElasticsearchLogsExporterTests, RecordableCreation) { // Create an elasticsearch exporter - auto exporter = - std::unique_ptr(new logs_exporter::ElasticsearchLogRecordExporter); + auto exporter = std::unique_ptr( + new logs_exporter::ElasticsearchLogRecordExporter); // Create a recordable auto record = exporter->MakeRecordable(); @@ -79,4 +77,52 @@ TEST(ElasticsearchLogsExporterTests, RecordableCreation) exporter->Export(nostd::span>(&record, 1)); } -#endif + +TEST(ElasticsearchLogRecordableTests, BasicTests) +{ + const auto severity = logs_api::Severity::kFatal; + const std::array stringlist{ + {nostd::string_view("string1"), nostd::string_view("string2")}}; + + const std::int64_t expected_observed_ts = 1732063944999647774LL; + const std::string expected_timestamp("2024-11-20T00:52:24.999647Z"); + const std::string expected_severity( + opentelemetry::logs::SeverityNumToText[static_cast(severity)]); + const std::string expected_body("Body of the log message"); + const std::string expected_scope_name("scope_name"); + const bool expected_boolean = false; + const int expected_int = 1; + const double expected_double = 2.0; + + const nlohmann::json expected{ + {"@timestamp", expected_timestamp}, + {"boolean", expected_boolean}, + {"double", expected_double}, + {"ecs", {{"version", "8.11.0"}}}, + {"int", expected_int}, + {"log", {{"level", expected_severity}, {"logger", expected_scope_name}}}, + {"message", expected_body}, + {"observedtimestamp", expected_observed_ts}, + {"stringlist", {stringlist[0], stringlist[1]}}}; + + const opentelemetry::common::SystemTimestamp now{std::chrono::nanoseconds(expected_observed_ts)}; + + const auto scope = + opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(expected_scope_name); + + opentelemetry::exporter::logs::ElasticSearchRecordable recordable; + recordable.SetTimestamp(now); + recordable.SetObservedTimestamp(now); + recordable.SetSeverity(severity); + recordable.SetBody(expected_body); + recordable.SetInstrumentationScope(*scope); + + recordable.SetAttribute("boolean", expected_boolean); + recordable.SetAttribute("int", expected_int); + recordable.SetAttribute("double", expected_double); + recordable.SetAttribute("stringlist", stringlist); + + const auto actual = recordable.GetJSON(); + + EXPECT_EQ(actual, expected); +}