Skip to content
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

[TEST] add a test for ElasticSearchRecordable #3154

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 69 additions & 23 deletions exporters/elasticsearch/test/es_log_record_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gtest/gtest.h>
#include <iostream>
#include <chrono>
#include <string>

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
}
marcalff marked this conversation as resolved.
Show resolved Hide resolved

#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)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
{
// Create invalid connection options for the elasticsearch exporter
logs_exporter::ElasticsearchExporterOptions options("localhost", -1);

// Create an elasticsearch exporter
auto exporter =
std::unique_ptr<sdklogs::LogRecordExporter>(new logs_exporter::ElasticsearchLogRecordExporter(options));
auto exporter = std::unique_ptr<sdklogs::LogRecordExporter>(
new logs_exporter::ElasticsearchLogRecordExporter(options));

// Create a log record and send to the exporter
auto record = exporter->MakeRecordable();
auto result = exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&record, 1));

// Ensure the return value is failure
ASSERT_EQ(result, sdk::common::ExportResult::kFailure);
ASSERT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure);
marcalff marked this conversation as resolved.
Show resolved Hide resolved
}

// 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<sdklogs::LogRecordExporter>(new logs_exporter::ElasticsearchLogRecordExporter);
auto exporter = std::unique_ptr<sdklogs::LogRecordExporter>(
new logs_exporter::ElasticsearchLogRecordExporter);
bool shutdownResult = exporter->Shutdown();
ASSERT_TRUE(shutdownResult);

Expand All @@ -54,15 +52,15 @@ TEST(ElasticsearchLogsExporterTests, Shutdown)
auto result = exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&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<sdklogs::LogRecordExporter>(new logs_exporter::ElasticsearchLogRecordExporter);
auto exporter = std::unique_ptr<sdklogs::LogRecordExporter>(
new logs_exporter::ElasticsearchLogRecordExporter);

// Create a recordable
auto record = exporter->MakeRecordable();
Expand All @@ -79,4 +77,52 @@ TEST(ElasticsearchLogsExporterTests, RecordableCreation)

exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&record, 1));
}
#endif

TEST(ElasticsearchLogRecordableTests, BasicTests)
{
const auto severity = logs_api::Severity::kFatal;
const std::array<nostd::string_view, 2> 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<std::size_t>(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);
}